@@ -9,6 +9,30 @@ namespace EatSomewhere.Manager;
99
1010public 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 >
0 commit comments