public async Task <IHttpActionResult> Put(int harvestId, HarvestModel harvestModel) { if (!ModelState.IsValid) { return(BadRequest("Harvest Model is not valid")); } try { using (var context = new BeeAppContext()) { var harvest = 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.Harvests) .SelectMany(x => x.Harvests) .Where(x => x.Id == harvestId) .FirstOrDefaultAsync(); if (harvest != null) { harvest.Name = harvestModel.Name; harvest.Date = harvestModel.Date; harvest.Product = harvestModel.Product; harvest.Quantity = harvestModel.Quantity; harvest.Unit = harvestModel.Unit; harvest.Note = harvestModel.Note; // Save context.SaveChanges(); // Return return(StatusCode(HttpStatusCode.NoContent)); } // Return return(BadRequest("Harvest could not be found")); } } catch (Exception ex) { // Return return(BadRequest(ex.Message)); } }
public async Task <IHttpActionResult> Post(int apiaryId, int hiveId, HarvestModel harvestModel) { if (!ModelState.IsValid) { return(BadRequest("Harvest Model is not valid")); } try { using (var context = new BeeAppContext()) { if (await _ensurer.EnsureHiveBelongsToApiary(context, apiaryId, hiveId, _applicationUserId)) { context.Harvests.Add(new Harvest { Name = harvestModel.Name, Date = harvestModel.Date, Product = harvestModel.Product, Quantity = harvestModel.Quantity, Unit = harvestModel.Unit, Note = harvestModel.Note, HiveId = hiveId }); // Save context.SaveChanges(); } } } catch (Exception ex) { return(BadRequest(ex.Message)); } // Return return(Ok(harvestModel)); }