Skip to content

Commit 9413364

Browse files
committed
Start on FoodEntries
1 parent b6904db commit 9413364

9 files changed

Lines changed: 345 additions & 11 deletions

File tree

EatSomewhere/Data/Assembly.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ public class Assembly
1616
public string Description { get; set; }
1717
public List<User> Admins { get; set; }= new();
1818
[JsonIgnore] public List<Food> Foods { get; set; } = new();
19+
[JsonIgnore] public List<FoodEntry> FoodEntries { get; set; }
20+
1921
[JsonIgnore] public List<Ingredient> Ingredients = new();
2022
}

EatSomewhere/Data/FoodEntry.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel.DataAnnotations.Schema;
2+
using System.Text.Json.Serialization;
23
using EatSomewhere.Users;
34

45
namespace EatSomewhere.Data;
@@ -18,4 +19,8 @@ public class FoodEntry
1819
public int PersonCount => Participants.Sum(x => x.AdditionalPersons + 1);
1920
public List<FoodParticipant> Participants { get; set; }
2021
public User PayedBy { get; set; }
22+
[JsonIgnore]
23+
public Assembly Assembly { get; set; }
24+
25+
public User CreatedBy { get; set; }
2126
}

EatSomewhere/Data/FoodParticipant.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel.DataAnnotations.Schema;
2+
using System.Text.Json.Serialization;
23
using EatSomewhere.Users;
34

45
namespace EatSomewhere.Data;
@@ -8,7 +9,9 @@ public class FoodParticipant
89
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
910
public string Id;
1011
public User User;
12+
public int AdditionalPersons;
13+
[JsonIgnore]
1114
public Food Food;
15+
[JsonIgnore]
1216
public Assembly Assembly;
13-
public int AdditionalPersons;
1417
}

EatSomewhere/Database/AppDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
5050
modelBuilder.Entity<FoodEntry>().Navigation(x => x.Participants).AutoInclude();
5151
modelBuilder.Entity<FoodEntry>().HasMany(x => x.Participants).WithMany();
5252
modelBuilder.Entity<FoodEntry>().Navigation(x => x.PayedBy).AutoInclude();
53+
modelBuilder.Entity<FoodEntry>().Navigation(x => x.CreatedBy).AutoInclude();
54+
modelBuilder.Entity<FoodEntry>().HasOne(x => x.Assembly).WithMany(x => x.FoodEntries);
5355

5456
modelBuilder.Entity<FoodParticipant>().HasKey(x => x.Id);
5557

EatSomewhere/Manager/FoodManager.cs

Lines changed: 169 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@ namespace EatSomewhere.Manager;
99

1010
public class FoodManager
1111
{
12+
public static List<FoodEntry> GetFoodEntries(User user, string assemblyId, int skip, int count)
13+
{
14+
using var d = new AppDbContext();
15+
d.Attach(user);
16+
Assembly? assembly = d.Assemblies.Where(x => x.Id == assemblyId)
17+
.Include(x => x.Users)
18+
.Include(x => x.FoodEntries)
19+
.ThenInclude(x => x.Participants)
20+
.ThenInclude(x => x.User)
21+
.FirstOrDefault();
22+
if (assembly == null)
23+
{
24+
return new List<FoodEntry>();
25+
}
26+
if (!assembly.Users.Contains(user))
27+
{
28+
return new List<FoodEntry>();
29+
}
30+
return assembly.FoodEntries
31+
.OrderByDescending(x => x.Date)
32+
.Skip(skip)
33+
.Take(count)
34+
.ToList();
35+
}
1236
public static List<Ingredient> GetIngredients(User user)
1337
{
1438
using var d = new AppDbContext();
@@ -121,6 +145,94 @@ public static ApiResponse<Food> CreateFood(User user, Food food)
121145
Data = food
122146
};
123147
}
148+
149+
public static ApiResponse<FoodEntry> CreateFoodEntry(User user, FoodEntry food)
150+
{
151+
using var d = new AppDbContext();
152+
d.Attach(user);
153+
Assembly? a = d.Assemblies.FirstOrDefault(x => x.Id == food.Assembly.Id);
154+
if (a == null)
155+
{
156+
return new ApiResponse<FoodEntry>
157+
{
158+
Success = false,
159+
Error = "Assembly not found"
160+
};
161+
}
162+
163+
foreach (FoodParticipant p in food.Participants)
164+
{
165+
User? foundUser = a.Users.FirstOrDefault(x => x.Id == p.User.Id);
166+
if (foundUser == null)
167+
{
168+
return new ApiResponse<FoodEntry>
169+
{
170+
Success = false,
171+
Error = "Participant user not found"
172+
};
173+
}
174+
p.User = foundUser;
175+
}
176+
177+
Food? foundFood = d.Foods.FirstOrDefault(x => x.Id == food.Food.Id);
178+
if (foundFood == null)
179+
{
180+
return new ApiResponse<FoodEntry>
181+
{
182+
Success = false,
183+
Error = "Food not found"
184+
};
185+
}
186+
food.Food = foundFood;
187+
User? foundPayedBy = a.Users.FirstOrDefault(x => x.Id == food.PayedBy.Id);
188+
if (foundPayedBy == null)
189+
{
190+
return new ApiResponse<FoodEntry>
191+
{
192+
Success = false,
193+
Error = "PayedBy user not found"
194+
};
195+
}
196+
food.PayedBy = foundPayedBy;
197+
food.CreatedBy = user;
198+
food.Assembly = a;
199+
200+
// Check if food already exists and if yes, update it instead of creating it
201+
FoodEntry? existingEntry = d.FoodEntries.FirstOrDefault(x => x.Id == food.Id);
202+
if (existingEntry != null)
203+
{
204+
if (existingEntry.CreatedBy.Id != user.Id && !CanAdministrateAssembly(user, a))
205+
{
206+
return new ApiResponse<FoodEntry>
207+
{
208+
Success = false,
209+
Error = "You are not allowed to edit this food"
210+
};
211+
}
212+
// Replace existing database entry with new one
213+
existingEntry.Date = food.Date;
214+
existingEntry.Comment = food.Comment;
215+
existingEntry.Food = food.Food;
216+
existingEntry.Cost = food.Cost;
217+
existingEntry.Participants = food.Participants;
218+
existingEntry.PayedBy = food.PayedBy;
219+
existingEntry.Assembly = food.Assembly;
220+
existingEntry.CreatedBy = food.CreatedBy;
221+
}
222+
else
223+
{
224+
d.FoodEntries.Add(food);
225+
}
226+
227+
d.SaveChanges();
228+
229+
return new ApiResponse<FoodEntry>
230+
{
231+
CreatedId = food.Id,
232+
Success = true,
233+
Data = food
234+
};
235+
}
124236

125237
public static ApiResponse<Ingredient> CreateIngredient(User user, Ingredient ingredient)
126238
{
@@ -217,7 +329,7 @@ static bool CanAdministrateAssembly(User user, Assembly assembly)
217329
public static ApiResponse DeleteIngredient(User user, string id)
218330
{
219331
using var d = new AppDbContext();
220-
var ingredient = d.Ingredients.FirstOrDefault(a => a.Id == id);
332+
var ingredient = d.Ingredients.Where(a => a.Id == id).Include(x => x.Assembly).FirstOrDefault();
221333
if (ingredient == null)
222334
{
223335
return new ApiResponse
@@ -244,6 +356,38 @@ public static ApiResponse DeleteIngredient(User user, string id)
244356
Success = true
245357
};
246358
}
359+
360+
public static ApiResponse DeleteFoodEntry(User user, string id)
361+
{
362+
using var d = new AppDbContext();
363+
var foodEntry = d.FoodEntries.Where(a => a.Id == id).Include(x => x.Assembly).FirstOrDefault();
364+
if (foodEntry == null)
365+
{
366+
return new ApiResponse
367+
{
368+
Success = false,
369+
Error = "FoodEntry not found"
370+
};
371+
}
372+
373+
if (!CanAdministrateAssembly(user, foodEntry.Assembly))
374+
{
375+
return new ApiResponse
376+
{
377+
Success = false,
378+
Error = "User is not an admin of this assembly"
379+
};
380+
}
381+
382+
d.Remove(foodEntry);
383+
384+
d.SaveChanges();
385+
386+
return new ApiResponse
387+
{
388+
Success = true
389+
};
390+
}
247391

248392
public static ApiResponse<String> RequestJoinAssembly(User user, string name)
249393
{
@@ -392,11 +536,19 @@ public static ApiResponse<String> RemoveUserFromAssembly(User user, string assem
392536
using var d = new AppDbContext();
393537
return d.Foods.FirstOrDefault(a => a.Id == id && a.Assembly.Users.Contains(user));
394538
}
539+
public static FoodEntry? GetFoodEntry(User user, string id)
540+
{
541+
using var d = new AppDbContext();
542+
return d.FoodEntries.Where(a => a.Id == id && a.Assembly.Users.Contains(user))
543+
.Include(x => x.Participants)
544+
.ThenInclude(x => x.User)
545+
.FirstOrDefault();
546+
}
395547

396548
public static ApiResponse DeleteFood(User user, string id)
397549
{
398550
using var d = new AppDbContext();
399-
var food = d.Foods.FirstOrDefault(a => a.Id == id);
551+
var food = d.Foods.Where(a => a.Id == id).Include(x => x.Assembly).FirstOrDefault();
400552
if (food == null)
401553
{
402554
return new ApiResponse
@@ -429,7 +581,10 @@ public static ApiResponse<String> PromoteUserToAdmin(User user, string assemblyI
429581
{
430582
using var d = new AppDbContext();
431583
d.Attach(user);
432-
Assembly? assembly = GetAssembly(user, assemblyId);
584+
Assembly? assembly = d.Assemblies.Where(x => x.Id == assemblyId)
585+
.Include(x => x.Users)
586+
.Include(x => x.Admins)
587+
.FirstOrDefault();
433588
if (assembly == null)
434589
{
435590
return new ApiResponse<String>
@@ -439,14 +594,12 @@ public static ApiResponse<String> PromoteUserToAdmin(User user, string assemblyI
439594
};
440595
}
441596

442-
d.Attach(assembly);
443-
if (assembly.Admins.Contains(user))
597+
if (!CanAdministrateAssembly(user, assembly))
444598
{
445599
return new ApiResponse<String>
446600
{
447-
Success = true,
448-
Error = "User is already an admin of this assembly",
449-
Data = "User is already an admin of this assembly"
601+
Success = false,
602+
Error = "You cannot administrate this assembly!"
450603
};
451604
}
452605
User? foundUser = assembly.Users.FirstOrDefault(x => x.Id == userId);
@@ -458,6 +611,14 @@ public static ApiResponse<String> PromoteUserToAdmin(User user, string assemblyI
458611
Error = "User not found in assembly"
459612
};
460613
}
614+
if (assembly.Admins.Contains(foundUser))
615+
{
616+
return new ApiResponse<String>
617+
{
618+
Success = true,
619+
Data = "User is already an admin of this assembly"
620+
};
621+
}
461622
assembly.Admins.Add(foundUser);
462623
d.SaveChanges();
463624
return new ApiResponse<String>

EatSomewhere/Server/FoodWebserver.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ public static void RegisterRESTForType<T>(string path, HttpServer server, Func<U
6565
}
6666
public static void AddFoodRoutes(HttpServer server)
6767
{
68-
68+
////// Food Entries //////
69+
/// DELETE archives food entries
70+
//api/v1/foodentries/<assemblyId>?skip=<int>&count=<int>
71+
RegisterPagedListForType("/api/v1/foodentries/", server, FoodManager.GetFoodEntries);
72+
RegisterRESTForType("/api/v1/foodentry/", server, FoodManager.GetFoodEntry, FoodManager.CreateFoodEntry, FoodManager.DeleteFoodEntry);
6973

7074
////// Food //////
7175
/// DELETE archives food
@@ -185,6 +189,46 @@ private static void SendResponse(ServerRequest request, ApiResponse response)
185189
{
186190
request.SendString(JsonSerializer.Serialize(response), "application/json", response.Success ? 200 : 400);
187191
}
192+
193+
private static void RegisterPagedListForType<T>(string apiPath, HttpServer server, Func<User, string, int, int, List<T>> listMethod)
194+
{
195+
server.AddRoute("GET", apiPath, request =>
196+
{
197+
request.allowAllOrigins = true;
198+
User? user = UserManagementServer.GetUserBySession(request);
199+
if (user == null)
200+
{
201+
ApiError.SendUnauthorized(request);
202+
return true;
203+
}
204+
int skip = 0;
205+
int count = 0;
206+
if(request.queryString.Get("skip") != null)
207+
{
208+
try
209+
{
210+
skip = int.Parse(request.queryString.Get("skip") ?? string.Empty);
211+
}
212+
catch
213+
{
214+
// ignored
215+
}
216+
}
217+
if(request.queryString.Get("count") != null)
218+
{
219+
try
220+
{
221+
count = int.Parse(request.queryString.Get("count") ?? string.Empty);
222+
}
223+
catch
224+
{
225+
// ignored
226+
}
227+
}
228+
request.SendString(JsonSerializer.Serialize(listMethod(user, request.pathDiff, skip, count)), "application/json");
229+
return true;
230+
});
231+
}
188232

189233
private static void RegisterListForType<T>(string apiPath, HttpServer server, Func<User, List<T>> listMethod)
190234
{

0 commit comments

Comments
 (0)