@@ -15,9 +15,6 @@ public static List<FoodEntry> GetFoodEntries(User user, string assemblyId, int s
1515 d . Attach ( user ) ;
1616 Assembly ? assembly = d . Assemblies . Where ( x => x . Id == assemblyId )
1717 . Include ( x => x . Users )
18- . Include ( x => x . FoodEntries )
19- . ThenInclude ( x => x . Participants )
20- . ThenInclude ( x => x . User )
2118 . FirstOrDefault ( ) ;
2219 if ( assembly == null )
2320 {
@@ -27,7 +24,9 @@ public static List<FoodEntry> GetFoodEntries(User user, string assemblyId, int s
2724 {
2825 return new List < FoodEntry > ( ) ;
2926 }
30- return assembly . FoodEntries
27+ return d . FoodEntries . Where ( x => x . Assembly . Id == assemblyId )
28+ . Include ( x => x . Participants )
29+ . Include ( x => x . Bills )
3130 . OrderByDescending ( x => x . Date )
3231 . Skip ( skip )
3332 . Take ( count )
@@ -150,7 +149,10 @@ public static ApiResponse<FoodEntry> CreateFoodEntry(User user, FoodEntry food)
150149 {
151150 using var d = new AppDbContext ( ) ;
152151 d . Attach ( user ) ;
153- Assembly ? a = d . Assemblies . FirstOrDefault ( x => x . Id == food . Assembly . Id ) ;
152+ Assembly ? a = d . Assemblies . Where ( x => x . Id == food . Assembly . Id )
153+ . Include ( x => x . Users )
154+ . Include ( x => x . Admins )
155+ . FirstOrDefault ( ) ;
154156 if ( a == null )
155157 {
156158 return new ApiResponse < FoodEntry >
@@ -162,7 +164,9 @@ public static ApiResponse<FoodEntry> CreateFoodEntry(User user, FoodEntry food)
162164
163165 foreach ( FoodParticipant p in food . Participants )
164166 {
165- User ? foundUser = a . Users . FirstOrDefault ( x => x . Id == p . User . Id ) ;
167+ User ? foundUser = a . Users . FirstOrDefault ( x => x . Id == p . User ? . Id ) ;
168+ if ( p . AdditionalPersons < 0 ) p . AdditionalPersons = 0 ;
169+ p . FoodEntry = food ;
166170 if ( foundUser == null )
167171 {
168172 return new ApiResponse < FoodEntry >
@@ -196,9 +200,10 @@ public static ApiResponse<FoodEntry> CreateFoodEntry(User user, FoodEntry food)
196200 food . PayedBy = foundPayedBy ;
197201 food . CreatedBy = user ;
198202 food . Assembly = a ;
203+ food . Bills = food . CalculateBills ( ) ;
199204
200205 // 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 ) ;
206+ FoodEntry ? existingEntry = d . FoodEntries . Where ( x => x . Id == food . Id ) . Include ( x => x . Bills ) . FirstOrDefault ( ) ;
202207 if ( existingEntry != null )
203208 {
204209 if ( existingEntry . CreatedBy . Id != user . Id && ! CanAdministrateAssembly ( user , a ) )
@@ -209,6 +214,12 @@ public static ApiResponse<FoodEntry> CreateFoodEntry(User user, FoodEntry food)
209214 Error = "You are not allowed to edit this food"
210215 } ;
211216 }
217+ // Remove all previous bills
218+ foreach ( Bill bill in existingEntry . Bills )
219+ {
220+ d . Remove ( bill ) ;
221+ }
222+ existingEntry . Bills . Clear ( ) ;
212223 // Replace existing database entry with new one
213224 existingEntry . Date = food . Date ;
214225 existingEntry . Comment = food . Comment ;
@@ -218,11 +229,16 @@ public static ApiResponse<FoodEntry> CreateFoodEntry(User user, FoodEntry food)
218229 existingEntry . PayedBy = food . PayedBy ;
219230 existingEntry . Assembly = food . Assembly ;
220231 existingEntry . CreatedBy = food . CreatedBy ;
232+ existingEntry . Bills = food . Bills ;
221233 }
222234 else
223235 {
224236 d . FoodEntries . Add ( food ) ;
225237 }
238+
239+ // All previous Bills are invalid now
240+
241+
226242
227243 d . SaveChanges ( ) ;
228244
0 commit comments