-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
254 lines (205 loc) · 6.74 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddAWSTalker();
//builder.Services.AddAzureTalker();
builder.Services.AddSingleton<PersonRepo>();
var app = builder.Build();
app.MapControllers();
// app.Map("Greet/{person}", (string person) => {
// Talker talker = new(new TalkBlue());
// talker.Greet(person);
// return $"Hello {person}";
// });
// app.Map("GoodBye/{person}", (string person) => {
// Talker talker = new(new TalkBlue());
// talker.Greet(person);
// return $"Hello {person}";
// });
app.Run();
public static class TalkerCloudExtentions
{
public static IServiceCollection AddAWSTalker(this IServiceCollection services)
{
return services.AddSingleton<ISystemTalker,TalkRed>();
}
public static IServiceCollection AddAzureTalker(this IServiceCollection services)
{
return services.AddSingleton<ISystemTalker,TalkBlue>();
}
}
public class TalkRed : ISystemTalker
{
public TalkRed(PersonRepo repo)
{
this.repo = repo;
}
int count = 0;
private readonly PersonRepo repo;
public void Say(string msg) {
Console.BackgroundColor = repo.GetColor(1);
Console.WriteLine("{0} {1}", msg, count++);
}
}
public class TalkBlue : ISystemTalker
{
public TalkBlue(PersonRepo repo)
{
this.repo = repo;
}
int count = 0;
private readonly PersonRepo repo;
public void Say(string msg) {
Console.BackgroundColor = repo.GetColor(0);
Console.WriteLine("{0} {1}", msg, count++);
}
}
public record PersonResult(string FirstName, string LastName, List<Vaccine>? Vaccines);
public record Person(string FirstName, string LastName) {
public List<int> VaccineIds {get;set;} = new List<int>();
// [JsonIgnore]
// public virtual List<Vaccine>? Vaccinations {get;set;}
}
public record Vaccine(string Name);
public class PersonRepo
{
List<object> _savedData = new List<object>();
Dictionary<int, Person> people = new() {
{0,new ("Ryan", "Anderson")},
{1,new ("Dani", "Trugilo")}
};
Dictionary<int, Vaccine> vaccines = new() {
{0,new ("Covid")},
{1,new ("Flu")}
};
public PersonRepo()
{
LoadData();
}
public PersonResult GetPerson(int 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) {
people.Add(people.Keys.Max() + 1, person);
return person;
}
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<int>();
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)
{
var dict = new Dictionary<int, ConsoleColor>() {
{0,ConsoleColor.DarkBlue},
{1,ConsoleColor.DarkYellow}
};
return dict[id];
}
/// <summary>
/// Convert List<Person> into a List<PersonResult>
/// Check how it's done in GetPerson and do the same here. it needs to see the vaccine list.
/// </summary>
/// <returns></returns>
public List<PersonResult> GetPeople()
{
return people.Values.ToList();
}
public void PersistData() {
_savedData = new List<object>{people,vaccines};
File.WriteAllText("data.json", JsonSerializer.Serialize(_savedData));
}
public void InitializeData() {
File.WriteAllText("data.json", JsonSerializer.Serialize(_savedData));
}
public void LoadData() {
_savedData = new List<object>{people,vaccines};
if(File.Exists("data.json")) {
_savedData = JsonSerializer.Deserialize<List<object>>(File.ReadAllText("data.json")) ?? _savedData;
people = ((JsonElement)_savedData[0]).Deserialize<Dictionary<int,Person>>();
vaccines = ((JsonElement)_savedData[1]).Deserialize<Dictionary<int,Vaccine>>();
}
else {
InitializeData();
people = (Dictionary<int,Person>)_savedData[0];
vaccines = (Dictionary<int,Vaccine>)_savedData[1];
}
}
public List<Vaccine> GetVaccines()
{
return vaccines.Values.ToList();
}
}
public interface ISystemTalker
{
void Say(string msg);
}
[ApiController]
public class TalkerController {
readonly ISystemTalker talker;
private readonly PersonRepo personRepo;
public TalkerController(ISystemTalker talker,PersonRepo personRepo)
{
this.talker = talker;
this.personRepo = personRepo;
}
[HttpGet("Greet/{personId}")]
public string Greet(int personId) {
talker.Say($"Hi {personRepo.GetPerson(personId).FirstName}");
return $"Hi {personRepo.GetPerson(personId).FirstName}";
}
[HttpGet("GoodBye/{personId}")]
public string GoodBye(int personId) {
talker.Say($"Good Bye {personRepo.GetPerson(personId).FirstName}");
return $"GoodBye {personRepo.GetPerson(personId).FirstName}";
}
[HttpGet("People")]
public List<PersonResult> People() {
return personRepo.GetPeople();
}
[HttpPost("AddPerson")]
public string AddPerson(Person person) {
personRepo.AddPerson(person);
personRepo.PersistData();
return $"{person.FirstName} Added!!";
}
[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<Vaccine> Vaccines() {
return personRepo.GetVaccines();
}
}
// Android APP -> Bussnee Logic -> HW
// Ios -> Bussnee Logic -> HW