diff --git a/Program.cs b/Program.cs index fe7ee71..831f2cd 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,7 @@  using System.Text.Json; +using System.Text.Json.Serialization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -72,17 +73,31 @@ public void Say(string msg) { } } + +public record PersonResult(string FirstName, string LastName, List? Vaccines); + public record Person(string FirstName, string LastName) { - public List? Vaccinations {get;set;} + + public List VaccineIds {get;set;} = new List(); + + // [JsonIgnore] + // public virtual List? Vaccinations {get;set;} } public record Vaccine(string Name); public class PersonRepo { - Dictionary dict = new() { + List _savedData = new List(); + + Dictionary people = new() { {0,new ("Ryan", "Anderson")}, - {1,new ("Dani", "Trugilo")}, + {1,new ("Dani", "Trugilo")} + }; + + Dictionary vaccines = new() { + {0,new ("Covid")}, + {1,new ("Flu")} }; public PersonRepo() @@ -90,20 +105,32 @@ public PersonRepo() LoadData(); } - public Person GetPerson(int id) + public PersonResult GetPerson(int id) { - return dict[id]; + return new(people[id].FirstName, people[id].LastName, + vaccines.Where(v => people[id].VaccineIds.Contains(v.Key)).Select(i => i.Value).ToList()); } public Person AddPerson(Person person) { - dict.Add(dict.Keys.Max() + 1, person); + people.Add(people.Keys.Max() + 1, person); return person; } - public Person AddVaccine(int id, string vaccineName) { - if(dict[id].Vaccinations == null) dict[id].Vaccinations = new List(); - dict[id].Vaccinations?.Add(new Vaccine(vaccineName)); - return dict[id]; + public (Person?, bool) AddVaccineToPerson(int id, string vaccineName) { + try { + var vaccine = vaccines.First(vaccine => vaccine.Value.Name == vaccineName); + if(people[id].VaccineIds == null) people[id].VaccineIds = new List(); + people[id].VaccineIds?.Add(vaccine.Key); + return (people[id], true); + } + catch(InvalidOperationException) { + return (null, false); + } + } + + public Vaccine AddVaccine(Vaccine vaccine) { + vaccines.Add(vaccines.Keys.Max() + 1, vaccine); + return vaccine; } public ConsoleColor GetColor(int id) @@ -115,22 +142,45 @@ public ConsoleColor GetColor(int id) return dict[id]; } - public List GetPeople() + + /// + /// Convert List into a List + /// Check how it's done in GetPerson and do the same here. it needs to see the vaccine list. + /// + /// + public List GetPeople() { - return dict.Values.ToList(); + return people.Values.ToList(); } public void PersistData() { - File.WriteAllText("data.db", JsonSerializer.Serialize(dict)); + _savedData = new List{people,vaccines}; + File.WriteAllText("data.json", JsonSerializer.Serialize(_savedData)); } public void InitializeData() { - File.WriteAllText("data.db", JsonSerializer.Serialize(dict)); + File.WriteAllText("data.json", JsonSerializer.Serialize(_savedData)); } public void LoadData() { - dict = JsonSerializer.Deserialize>(File.ReadAllText("data.db")) ?? dict; //if null reassign to itself; + _savedData = new List{people,vaccines}; + if(File.Exists("data.json")) { + _savedData = JsonSerializer.Deserialize>(File.ReadAllText("data.json")) ?? _savedData; + people = ((JsonElement)_savedData[0]).Deserialize>(); + vaccines = ((JsonElement)_savedData[1]).Deserialize>(); + } + else { + InitializeData(); + people = (Dictionary)_savedData[0]; + vaccines = (Dictionary)_savedData[1]; + } + } + + public List GetVaccines() + { + return vaccines.Values.ToList(); } + } public interface ISystemTalker @@ -162,7 +212,7 @@ public string GoodBye(int personId) { } [HttpGet("People")] - public List People() { + public List People() { return personRepo.GetPeople(); } @@ -173,11 +223,28 @@ public string AddPerson(Person person) { return $"{person.FirstName} Added!!"; } - [HttpGet("AddVaccine/{personId}/{vaccineName}")] - public Person AddPerson(int personId, string vaccineName) { - var person = personRepo.AddVaccine(personId, vaccineName); - personRepo.PersistData(); - return person; + [HttpGet("AddVaccineToPerson/{personId}/{vaccineName}")] + public IActionResult AddVaccineToPerson(int personId, string vaccineName) { + var (person,result) = personRepo.AddVaccineToPerson(personId, vaccineName); + if(result) { + personRepo.PersistData(); + return new OkObjectResult(person); + } + else { + return new BadRequestObjectResult($"No vaccine found with name {vaccineName}"); + } + } + + [HttpGet("AddVaccine/{vaccineName}")] + public Vaccine AddVaccine(string vaccineName) { + Vaccine vaccine = new (vaccineName); + personRepo.AddVaccine(vaccine); + return vaccine; + } + + [HttpGet("Vaccines")] + public List Vaccines() { + return personRepo.GetVaccines(); } } diff --git a/api.http b/api.http index 568d1e1..56d987d 100644 --- a/api.http +++ b/api.http @@ -12,4 +12,8 @@ content-type: application/json ### -GET https://localhost:5001/addvaccine/3/flu +GET https://localhost:5001/addvaccinetoperson/0/Flu + +### + +GET https://localhost:5001/vaccines \ No newline at end of file diff --git a/data.db b/data.db deleted file mode 100644 index 24908e5..0000000 --- a/data.db +++ /dev/null @@ -1 +0,0 @@ -{"0":{"FirstName":"Ryan","LastName":"Anderson","Vaccinations":[{"Name":"covid"}]},"1":{"FirstName":"Dani","LastName":"Trugilo","Vaccinations":[{"Name":"flu"}]},"2":{"FirstName":"Leah","LastName":"Locatelli","Vaccinations":[{"Name":"covid"}]},"3":{"FirstName":"Lucas","LastName":"Locatelli","Vaccinations":[{"Name":"flu"}]}} \ No newline at end of file