93 lines
4.1 KiB
C#

using System.Text.Json;
using View_by_Distance.Property.Models;
using View_by_Distance.Shared.Models;
namespace View_by_Distance.Instance.Models;
/// <summary>
// ?
/// </summary>
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<A2_People>();
_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<G2_Identify> identifiedCollection)
{
string key;
string json;
string jsonFile;
FileInfo fileInfo;
string[] segments;
string directoryFullName;
Dictionary<string, List<G2_Identify>> keyValuePairs = new();
string hPeopleCollectionDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(A2_People), "[]");
string aPropertySingletonDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(configuration, nameof(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<G2_Identify>());
keyValuePairs[key].Add(identified);
}
foreach (KeyValuePair<string, List<G2_Identify>> 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 (!Property.Models.Stateless.IPath.WriteAllText(jsonFile, json, updateDateWhenMatches: true, compareBeforeWrite: true))
continue;
}
}
internal Person[] GetPeople(Property.Models.Configuration configuration)
{
Person[] results;
if (_Configuration?.PropertyConfiguration is null)
throw new NullReferenceException(nameof(_Configuration.PropertyConfiguration));
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);
return results.ToArray();
}
internal static Dictionary<string, List<Person>> Convert(Person[] people)
{
Dictionary<string, List<Person>> results = new();
string personKey;
foreach (Person person in people)
{
personKey = Shared.Models.Stateless.Methods.IPersonBirthday.GetFormatted(person.Birthday);
if (!results.ContainsKey(personKey))
results.Add(personKey, new List<Person>());
results[personKey].Add(person);
}
return results;
}
}