Facebook logic, IsDefaultName, GetFiles update and
HardCoded tests
This commit is contained in:
232
Person/Person.cs
Normal file
232
Person/Person.cs
Normal file
@ -0,0 +1,232 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Phares.Shared;
|
||||
using Serilog;
|
||||
using System.Text.Json;
|
||||
using View_by_Distance.Person.Models;
|
||||
using View_by_Distance.Shared.Models;
|
||||
using View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
|
||||
namespace View_by_Distance.Person;
|
||||
|
||||
public class Person
|
||||
{
|
||||
|
||||
private readonly AppSettings _AppSettings;
|
||||
private readonly string _WorkingDirectory;
|
||||
private readonly IsEnvironment _IsEnvironment;
|
||||
private readonly Configuration _Configuration;
|
||||
private readonly IConfigurationRoot _ConfigurationRoot;
|
||||
private readonly Property.Models.Configuration _PropertyConfiguration;
|
||||
|
||||
public Person(List<string> args, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, Shared.Models.Methods.IConsole console)
|
||||
{
|
||||
if (isSilent)
|
||||
{ }
|
||||
if (console is null)
|
||||
{ }
|
||||
_AppSettings = appSettings;
|
||||
_IsEnvironment = isEnvironment;
|
||||
long ticks = DateTime.Now.Ticks;
|
||||
_WorkingDirectory = workingDirectory;
|
||||
_ConfigurationRoot = configurationRoot;
|
||||
ILogger? log = Log.ForContext<Person>();
|
||||
Property.Models.Configuration propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
|
||||
Configuration configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration);
|
||||
_PropertyConfiguration = propertyConfiguration;
|
||||
_Configuration = configuration;
|
||||
propertyConfiguration.Update();
|
||||
string? comparePathRoot = Path.GetDirectoryName(appSettings.SaveDirectory);
|
||||
if (comparePathRoot is null || comparePathRoot == propertyConfiguration.RootDirectory)
|
||||
throw new Exception("Nested isn't allowed!");
|
||||
if (!Directory.Exists(appSettings.SaveDirectory))
|
||||
_ = Directory.CreateDirectory(appSettings.SaveDirectory);
|
||||
log.Information(propertyConfiguration.RootDirectory);
|
||||
Verify();
|
||||
Loop(ticks, log);
|
||||
}
|
||||
|
||||
private void Verify()
|
||||
{
|
||||
if (_AppSettings is null)
|
||||
{ }
|
||||
if (_IsEnvironment is null)
|
||||
{ }
|
||||
if (_Configuration is null)
|
||||
{ }
|
||||
if (_ConfigurationRoot is null)
|
||||
{ }
|
||||
if (_WorkingDirectory is null)
|
||||
{ }
|
||||
if (_PropertyConfiguration is null)
|
||||
{ }
|
||||
}
|
||||
|
||||
private void Loop(long ticks, ILogger log)
|
||||
{
|
||||
int age;
|
||||
string json;
|
||||
string code;
|
||||
string? day;
|
||||
string alias;
|
||||
string? line;
|
||||
string? year;
|
||||
string? month;
|
||||
bool deceased;
|
||||
ConsoleKey sex;
|
||||
long personKey;
|
||||
string? lastName;
|
||||
string middleName;
|
||||
string? firstName;
|
||||
DateTime? dateTime;
|
||||
PersonName personName;
|
||||
string checkDirectory;
|
||||
ConsoleKey? consoleKey;
|
||||
string? approximateYears;
|
||||
string personKeyFormatted;
|
||||
string personDisplayDirectory;
|
||||
log.Information($"Ready to create / update a person? [{ticks}]");
|
||||
for (int y = 0; y < int.MaxValue; y++)
|
||||
{
|
||||
log.Information("Press \"Y\" key to continue, \"N\" key exit or close console");
|
||||
consoleKey = System.Console.ReadKey().Key;
|
||||
log.Information(". . .");
|
||||
if (consoleKey is not ConsoleKey.Y and not ConsoleKey.N)
|
||||
break;
|
||||
firstName = null;
|
||||
for (int f = 0; f < 5; f++)
|
||||
{
|
||||
log.Information("Enter persons first name (minimum length of two characters)");
|
||||
line = System.Console.ReadLine();
|
||||
log.Information(". . .");
|
||||
if (string.IsNullOrEmpty(line) || line.Length < 2)
|
||||
continue;
|
||||
firstName = line;
|
||||
break;
|
||||
}
|
||||
if (firstName is null)
|
||||
continue;
|
||||
lastName = null;
|
||||
for (int f = 0; f < 5; f++)
|
||||
{
|
||||
log.Information("Enter persons last name (minimum length of two characters)");
|
||||
line = System.Console.ReadLine();
|
||||
log.Information(". . .");
|
||||
if (string.IsNullOrEmpty(line) || line.Length < 2)
|
||||
continue;
|
||||
lastName = line;
|
||||
break;
|
||||
}
|
||||
if (lastName is null)
|
||||
continue;
|
||||
log.Information("Enter persons middle name (press enter if they don't have a middle name)");
|
||||
line = System.Console.ReadLine();
|
||||
log.Information(". . .");
|
||||
middleName = string.IsNullOrEmpty(line) ? string.Empty : line;
|
||||
log.Information("Enter persons alias (press enter if they don't have a alias)");
|
||||
line = System.Console.ReadLine();
|
||||
log.Information(". . .");
|
||||
alias = string.IsNullOrEmpty(line) ? string.Empty : line;
|
||||
personName = new(new(firstName), new(middleName), new(lastName), new(alias));
|
||||
json = JsonSerializer.Serialize(personName, new JsonSerializerOptions { WriteIndented = true });
|
||||
log.Information("Is the person \"M\" (Male), \"F\" (Female) or \"U\" (Unknown)");
|
||||
consoleKey = System.Console.ReadKey().Key;
|
||||
log.Information(". . .");
|
||||
if (consoleKey is not ConsoleKey.M and not ConsoleKey.F and not ConsoleKey.U)
|
||||
continue;
|
||||
sex = consoleKey.Value;
|
||||
log.Information("Is the person deceased \"Y\" or \"N\"");
|
||||
consoleKey = System.Console.ReadKey().Key;
|
||||
log.Information(". . .");
|
||||
if (consoleKey is not ConsoleKey.Y and not ConsoleKey.N)
|
||||
continue;
|
||||
deceased = consoleKey == ConsoleKey.Y;
|
||||
dateTime = null;
|
||||
approximateYears = null;
|
||||
for (int f = 0; f < 5; f++)
|
||||
{
|
||||
day = null;
|
||||
year = null;
|
||||
month = null;
|
||||
approximateYears = null;
|
||||
log.Information("Enter persons birthday month (press enter if not known) [MMMM || MMM || MM || M]");
|
||||
line = System.Console.ReadLine();
|
||||
log.Information(". . .");
|
||||
if (!string.IsNullOrEmpty(line))
|
||||
month = line;
|
||||
else
|
||||
{
|
||||
log.Information("Enter persons approximate age");
|
||||
line = System.Console.ReadLine();
|
||||
log.Information(". . .");
|
||||
if (string.IsNullOrEmpty(line) || !int.TryParse(line, out _))
|
||||
continue;
|
||||
approximateYears = line;
|
||||
}
|
||||
log.Information("Enter persons birthday day (press enter if not known)");
|
||||
line = System.Console.ReadLine();
|
||||
log.Information(". . .");
|
||||
if (!string.IsNullOrEmpty(line))
|
||||
day = line;
|
||||
else
|
||||
{
|
||||
log.Information("Enter persons approximate age");
|
||||
line = System.Console.ReadLine();
|
||||
log.Information(". . .");
|
||||
if (string.IsNullOrEmpty(line) || !int.TryParse(line, out _))
|
||||
continue;
|
||||
approximateYears = line;
|
||||
}
|
||||
log.Information("Enter persons birthday year (press enter if not known)");
|
||||
line = System.Console.ReadLine();
|
||||
log.Information(". . .");
|
||||
if (!string.IsNullOrEmpty(line))
|
||||
year = line;
|
||||
else
|
||||
{
|
||||
log.Information("Enter persons approximate age");
|
||||
line = System.Console.ReadLine();
|
||||
log.Information(". . .");
|
||||
if (string.IsNullOrEmpty(line) || !int.TryParse(line, out _))
|
||||
continue;
|
||||
approximateYears = line;
|
||||
}
|
||||
if (month is null || day is null || year is null)
|
||||
dateTime = null;
|
||||
else
|
||||
{
|
||||
dateTime = IPersonBirthday.GetDate(month, day, year);
|
||||
if (dateTime is not null)
|
||||
{
|
||||
(age, _) = IAge.GetAge(new DateTime(ticks).Ticks, dateTime.Value);
|
||||
approximateYears = age.ToString();
|
||||
}
|
||||
}
|
||||
if (approximateYears is null && (dateTime is null || dateTime == DateTime.MinValue))
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
if (approximateYears is null)
|
||||
continue;
|
||||
personDisplayDirectory = Path.Combine(_AppSettings.SaveDirectory, ticks.ToString(), $"{personName.First.Value} {personName.Last.Value}~{approximateYears}");
|
||||
if (!Directory.Exists(personDisplayDirectory))
|
||||
_ = Directory.CreateDirectory(personDisplayDirectory);
|
||||
if (dateTime is null)
|
||||
personKeyFormatted = "2";
|
||||
else
|
||||
{
|
||||
personKey = dateTime.Value.Ticks;
|
||||
if (deceased)
|
||||
code = sex is ConsoleKey.M ? "05" : sex is ConsoleKey.F ? "04" : sex is ConsoleKey.U ? "02" : throw new NotImplementedException();
|
||||
else
|
||||
code = sex is ConsoleKey.M ? "15" : sex is ConsoleKey.F ? "14" : sex is ConsoleKey.U ? "03" : throw new NotImplementedException();
|
||||
personKeyFormatted = $"{IPersonBirthday.GetFormatted(_Configuration.PersonBirthdayFormat, personKey)[..^2]}{code}";
|
||||
}
|
||||
checkDirectory = Path.Combine(personDisplayDirectory, personKeyFormatted);
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
_ = Directory.CreateDirectory(checkDirectory);
|
||||
_ = IPath.WriteAllText(Path.Combine(checkDirectory, $"{personKeyFormatted}.json"), json, updateDateWhenMatches: false, compareBeforeWrite: true);
|
||||
}
|
||||
log.Information(". . .");
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user