-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
350 lines (272 loc) · 9.49 KB
/
Program.cs
File metadata and controls
350 lines (272 loc) · 9.49 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using Microsoft.OpenApi.Models;
using Microsoft.EntityFrameworkCore;
using TodoStore.Models;
using System.Net;
using System.Text;
using Newtonsoft.Json;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("TodosDb") ?? "Data Source=/app/data/Todos.db";
// var connectionString = "Data Source=Todos.db";
builder.Services.AddSqlite<TodoDb>(connectionString);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Todo API", Description = "Minimal API for Todos", Version = "v1" });
});
builder.Services.AddCors();
// builder.Services.AddDbContext<TodoDb>(options => options.UseInMemoryDatabase("items"));
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Todo API V1");
c.RoutePrefix = string.Empty;
});
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
static bool IsDateToday(DateTime? inputDate)
{
if (!inputDate.HasValue)
{
return false;
}
// Get today's date
DateTime today = DateTime.Now.Date;
// Compare the year, month, and day of both dates
return inputDate.Value.Year == today.Year &&
inputDate.Value.Month == today.Month &&
inputDate.Value.Day == today.Day;
}
static bool IsNotPastToday(DateTime? inputDate)
{
if (!inputDate.HasValue)
{
return false;
}
// Get today's date
DateTime today = DateTime.Now.Date;
// Compare the input date with today
return inputDate.Value <= today;
}
app.MapPost("/RemindTodos", async (TodoDb db) =>
{
var todos = await db.Todos.Where(x => (x.Category == Category.Todos || x.Category == Category.TodaysTodos) && x.Status == 1).ToListAsync();
var newTodos = todos.Where(x => IsNotPastToday(x.DueDate)).OrderByDescending(x => x.Priority).ToList();
if (newTodos.Any())
{
var body = "Hi John, here are your Todos for today: \n";
foreach (var todo in newTodos)
{
if (todo.DueDate is not null)
{
body += $"Priority: {todo.Priority}\n";
body += $"{todo.Name}\n";
var formattedDate = todo.DueDate.Value.ToString("MM/dd/yyyy");
body += $"Due Date: {formattedDate}\n\n";
}
else
{
body += $"{todo.Name}\n\n";
}
}
await SendSmsViaByteFlow(body);
}
return newTodos;
});
static async Task SendSmsViaByteFlow(string body)
{
string baseUrl = "http://host.docker.internal:8005/sms/john";
Console.WriteLine($"here in fn");
// Create a new instance of HttpClient
using (HttpClient client = new HttpClient())
{
try
{
Console.WriteLine($"here before");
var postData = new
{
message = body
};
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
// Convert the object to a JSON string
string jsonData = JsonConvert.SerializeObject(postData);
HttpContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
var response = await client.PostAsync(baseUrl, content);
}
catch (HttpRequestException e)
{
// Log.Error($"API Request Error: {e.Message}");
Console.WriteLine(e.Message);
}
}
}
static DateTime ConvertToCurrentTimeZone(DateTime utcDateTime)
{
// Get the Central Time zone
TimeZoneInfo centralTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
// Convert the UTC DateTime to the Central Time zone
DateTime centralDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, centralTimeZone);
return centralDateTime;
}
static DateTime ConvertIsoToLocalDateTime(string isoDateString)
{
// Parse the ISO date string to a DateTime object
DateTime utcDateTime = DateTime.Parse(isoDateString, null, System.Globalization.DateTimeStyles.RoundtripKind);
// Get the Central Time zone
TimeZoneInfo centralTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
// Convert the UTC DateTime to the Central Time zone
DateTime centralDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, centralTimeZone);
return centralDateTime;
}
app.MapPost("/SendReminder", async (CheckChoresReminder request) =>
{
var body = request.Action == "Create" ? "Good morning John, \n Reminder to use vault and set your todos for the day" : "Hi John \n Reminder to review your todos and update their status";
await SendSmsViaByteFlow(body);
});
app.MapGet("/Todo", async (TodoDb db) => await db.Todos.OrderBy(x => x.Priority).ToListAsync());
app.MapGet("/Todo/{id}", async (TodoDb db, int id) => await db.Todos.FindAsync(id));
app.MapPost("/Todo", async (TodoDb db, Todo Todo) =>
{
Todo.DateCreated = DateTime.Now;
Todo.DateUpdated = DateTime.Now;
Todo.Status = 1;
await db.Todos.AddAsync(Todo);
await db.SaveChangesAsync();
return Results.Created($"/Todo/{Todo.Id}", Todo);
});
app.MapPost("/Todo/MassInsert", async (TodoDb db, List<Todo> todos) =>
{
var todoList = new List<Todo>();
DateTime utcDateTime = DateTime.UtcNow;
DateTime localDateTime = ConvertToCurrentTimeZone(utcDateTime);
var toAdd = todos.Where(x => x.Category == Category.Info || x.Category == Category.ToLearn).ToList();
foreach (var todo in toAdd)
{
var newTodo = new Todo
{
Id = 0,
Name = todo.Name,
Category = todo.Category,
DateCreated = localDateTime,
DateUpdated = localDateTime,
Status = 1,
};
todoList.Add(newTodo);
}
await db.Todos.AddRangeAsync(todoList);
await db.SaveChangesAsync();
return Results.Ok();
});
app.MapPost("/Todo/MassUpdate", async (TodoDb db, BatchActionsRequest request) =>
{
var todoList = new List<Todo>();
DateTime utcDateTime = DateTime.UtcNow;
// Convert to the current time zone
DateTime localDateTime = ConvertToCurrentTimeZone(utcDateTime);
var todos = await db.Todos.Where(x => request.Ids.Contains(x.Id)).ToListAsync();
switch (request.Action)
{
case "COMPLETE":
foreach (var todo in todos)
{
todo.Status = 2;
todo.DateUpdated = localDateTime;
}
break;
case "TODAY":
foreach (var todo in todos)
{
todo.Status = 1;
todo.DateUpdated = localDateTime;
todo.DueDate = localDateTime;
}
break;
case "FUTURE":
foreach (var todo in todos)
{
todo.Status = 1;
todo.DateUpdated = localDateTime;
todo.Category = Category.Future;
}
break;
case "DELETE":
db.Todos.RemoveRange(todos);
break;
case "RAISE-PRIORITY":
foreach (var todo in todos)
{
todo.Priority = "High";
todo.DateUpdated = localDateTime;
}
break;
default:
break;
}
await db.SaveChangesAsync();
return Results.Ok();
});
app.MapPost("/Todo/UpdateFromGrid", async (TodoDb db, BatchChangesRequest request) =>
{
var todoList = new List<Todo>();
DateTime utcDateTime = DateTime.UtcNow;
// Convert to the current time zone
DateTime localDateTime = ConvertToCurrentTimeZone(utcDateTime);
foreach (var change in request.Changes)
{
switch (change.Type)
{
case BatchChangeType.Insert:
var todo = new Todo
{
Id = 0,
Name = change.Name,
Category = change.Category,
DateCreated = DateTime.Now,
DateUpdated = DateTime.Now,
Status = 1,
DueDate = change.Category == Category.TodaysTodos ? DateTime.Now : null
};
todoList.Add(todo);
break;
case BatchChangeType.Update:
var toUpdate = await db.Todos.FindAsync(change.Id);
if (toUpdate is null) return Results.NotFound();
toUpdate.Name = change.Name;
toUpdate.DateUpdated = DateTime.Now;
toUpdate.DueDate = change.DueDate;
toUpdate.Status = change.Status;
toUpdate.Priority = !string.IsNullOrEmpty(change?.Priority) ? change.Priority : toUpdate.Priority;
break;
default:
break;
}
}
await db.Todos.AddRangeAsync(todoList);
await db.SaveChangesAsync();
return Results.Ok();
});
app.MapPut("/Todo/{id}", async (TodoDb db, Todo updateTodo, int id) =>
{
var Todo = await db.Todos.FindAsync(id);
if (Todo is null) return Results.NotFound();
Todo.Name = updateTodo.Name;
Todo.DateUpdated = DateTime.Now;
Todo.Status = updateTodo.Status;
await db.SaveChangesAsync();
return Results.NoContent();
});
app.MapDelete("/Todo/{id}", async (TodoDb db, int id) =>
{
var Todo = await db.Todos.FindAsync(id);
if (Todo is null)
{
return Results.NotFound();
}
db.Todos.Remove(Todo);
await db.SaveChangesAsync();
return Results.Ok();
});
app.Run();