using System.Text.Json; using View_by_Distance.Shared.Models; namespace View_by_Distance.Instance.Models; /// // ? /// internal class A2_People { private readonly Serilog.ILogger? _Log; private readonly Configuration _Configuration; private readonly JsonSerializerOptions _WriteIndentedJsonSerializerOptions; internal A2_People(Configuration configuration) { _Configuration = configuration; _Log = Serilog.Log.ForContext(); _WriteIndentedJsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true }; } public override string ToString() { string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); return result; } internal void WriteAllText(Property.Models.Configuration configuration, string outputResolution, List identifiedCollection) { string key; string json; string jsonFile; FileInfo fileInfo; string[] segments; string directoryFullName; Dictionary> keyValuePairs = new(); string hPeopleCollectionDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(A2_People), "[]"); string aPropertySingletonDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(Property.Models.A_Property), "{}"); foreach (G2_Identify identified in identifiedCollection) { fileInfo = new FileInfo(string.Concat(aPropertySingletonDirectory, identified.RelativePath)); if (fileInfo?.Directory is null || !fileInfo.Directory.Exists || fileInfo.Exists) continue; key = string.Concat(identified.ParentDirectoryName, '|', identified.Person); if (!keyValuePairs.ContainsKey(key)) keyValuePairs.Add(key, new List()); keyValuePairs[key].Add(identified); } foreach (KeyValuePair> keyValuePair in keyValuePairs) { segments = keyValuePair.Key.Split('|'); directoryFullName = Path.Combine(hPeopleCollectionDirectory, segments[0]); if (!Directory.Exists(directoryFullName)) _ = Directory.CreateDirectory(directoryFullName); jsonFile = Path.Combine(directoryFullName, $"{segments[1]}.json"); json = JsonSerializer.Serialize(keyValuePair.Value, _WriteIndentedJsonSerializerOptions); if (!Shared.Models.Stateless.Methods.IPath.WriteAllText(jsonFile, json, updateDateWhenMatches: true, compareBeforeWrite: true)) continue; } } internal Person[] GetPeople(Property.Models.Configuration configuration) { Person[] results; string rootDirectory = _Configuration.PropertyConfiguration.RootDirectory; string peopleRootDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(A2_People)); string? rootResultsDirectory = Path.GetDirectoryName(Path.GetDirectoryName(peopleRootDirectory)); if (rootResultsDirectory is null) throw new Exception(); Storage storage = new(rootDirectory, rootResultsDirectory, peopleRootDirectory); results = Shared.Models.Stateless.Methods.IPerson.GetPeople(storage, _Configuration.PersonBirthdayFirstYear, _Configuration.PersonBirthdayFormat, _Configuration.PersonKeyFormat, _Configuration.PersonRequirePeopleFile); return results.ToArray(); } }