public async Task <IHttpActionResult> Put(int feedingId, FeedingModel feedingModel) { if (!ModelState.IsValid) { return(BadRequest("Feeding Model is not valid")); } try { using (var context = new BeeAppContext()) { var feeding = await context.Beekeepers .Where(x => x.ApplicationUserId == _applicationUserId) .Include(x => x.Apiaries) .SelectMany(x => x.Apiaries) .Include(x => x.Hives) .SelectMany(x => x.Hives) .Include(x => x.Feedings) .SelectMany(x => x.Feedings) .Where(x => x.Id == feedingId) .FirstOrDefaultAsync(); if (feeding != null) { feeding.Name = feedingModel.Name; feeding.Date = feedingModel.Date; feeding.Product = feedingModel.Product; feeding.Quantity = feedingModel.Quantity; feeding.Unit = feedingModel.Unit; feeding.Note = feedingModel.Note; // Save context.SaveChanges(); // Return return(StatusCode(HttpStatusCode.NoContent)); } // Return return(BadRequest("Feeding could not be found")); } } catch (Exception ex) { // Return return(BadRequest(ex.Message)); } }
public async Task <IHttpActionResult> Post(int apiaryId, int hiveId, FeedingModel feedingModel) { if (!ModelState.IsValid) { return(BadRequest("Feeding Model is not valid")); } try { using (var context = new BeeAppContext()) { if (await _ensurer.EnsureHiveBelongsToApiary(context, apiaryId, hiveId, _applicationUserId)) { context.Feedings.Add(new Feeding { Name = feedingModel.Name, Date = feedingModel.Date, Product = feedingModel.Product, Quantity = feedingModel.Quantity, Unit = feedingModel.Unit, Note = feedingModel.Note, HiveId = hiveId }); // Save context.SaveChanges(); } } } catch (Exception ex) { return(BadRequest(ex.Message)); } // Return return(Ok(feedingModel)); }