public async Task <IHttpActionResult> Delete(int workId) { try { using (var context = new BeeAppContext()) { var work = await context.Beekeepers .Where(x => x.ApplicationUserId == _applicationUserId) .Include(x => x.Works) .SelectMany(x => x.Works) .Where(x => x.Id == workId) .FirstOrDefaultAsync(); if (work != null) { // Remove context.Works.Remove(work); // Save context.SaveChanges(); // Return return(StatusCode(HttpStatusCode.NoContent)); } // Return return(BadRequest("Work could not be found")); } } catch (Exception ex) { // Return return(BadRequest(ex.Message)); } }
public async Task <IHttpActionResult> UpdateInfo(BeekeeperUpdateInfo beekeeperUpdateInfo) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Get current User Id var applicationUserId = User.Identity.GetUserId(); using (var context = new BeeAppContext()) { var beekeeper = await context.Beekeepers .FirstOrDefaultAsync(x => x.ApplicationUserId == applicationUserId); if (beekeeper != null) { beekeeper.FirstName = beekeeperUpdateInfo.FirstName ?? beekeeper.FirstName; beekeeper.LastName = beekeeperUpdateInfo.LastName ?? beekeeper.LastName; beekeeper.PhoneNumber = beekeeperUpdateInfo.Phone ?? beekeeper.PhoneNumber; beekeeper.Number = beekeeperUpdateInfo.Number ?? beekeeper.Number; // Save context.SaveChanges(); // Return return(StatusCode(HttpStatusCode.NoContent)); } } // Return return(Ok()); }
public async Task <IHttpActionResult> Delete(int apiaryId) { try { using (var context = new BeeAppContext()) { // Get the apiary var apiary = await context.Beekeepers .Where(x => x.ApplicationUserId == _applicationUserId) .Include(x => x.Apiaries) .SelectMany(x => x.Apiaries) .FirstOrDefaultAsync(x => x.Id == apiaryId); if (apiary != null) { // Remove context.Apiaries.Remove(apiary); // Save context.SaveChanges(); // Return return(StatusCode(HttpStatusCode.NoContent)); } // Return return(BadRequest("Apiary could not be found")); } } catch (Exception ex) { // Return return(BadRequest(ex.Message)); } }
public async Task <IHttpActionResult> Post(BeekeeperModel beekeeperModel) { var user = new ApplicationUser { UserName = beekeeperModel.Email, Email = beekeeperModel.Email, FirstName = beekeeperModel.FirstName, LastName = beekeeperModel.LastName, PhoneNumber = beekeeperModel.Phone, Number = beekeeperModel.Number }; try { await _ensurer.EnsureEmailIsUnique(ModelState, UserManager, beekeeperModel.Email); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var addUserResult = await UserManager.CreateAsync(user, beekeeperModel.Password); if (!addUserResult.Succeeded) { return(GetErrorResult(addUserResult)); } using (var context = new BeeAppContext()) { context.Beekeepers.Add(new Beekeeper { FirstName = beekeeperModel.FirstName, LastName = beekeeperModel.LastName, Email = beekeeperModel.Email, PhoneNumber = beekeeperModel.Phone, Number = beekeeperModel.Number, ApplicationUserId = user.Id }); // Save context.SaveChanges(); } } catch (Exception ex) { return(BadRequest(ex.Message)); } // Return return(Ok(new { user.Id, user.Email })); }
public async Task <IHttpActionResult> Put(int inspectionId, InspectionModel inspectionModel) { if (!ModelState.IsValid) { return(BadRequest("Inspection Model is not valid")); } try { using (var context = new BeeAppContext()) { var inspection = 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.Inspections) .SelectMany(x => x.Inspections) .Where(x => x.Id == inspectionId) .FirstOrDefaultAsync(); if (inspection != null) { inspection.Name = inspectionModel.Name; inspection.Date = inspectionModel.Date; inspection.Strength = inspectionModel.Strength; inspection.Temper = inspectionModel.Temper; inspection.Disease = inspectionModel.Disease; inspection.FramesBees = inspectionModel.FramesBees; inspection.FramesHoney = inspectionModel.FramesHoney; inspection.FramesHoneySupers = inspectionModel.FramesHoneySupers; inspection.Drones = inspectionModel.Drones; inspection.DroneCells = inspectionModel.DroneCells; // Save context.SaveChanges(); // Return return(StatusCode(HttpStatusCode.NoContent)); } // Return return(BadRequest("Inspection could not be found")); } } catch (Exception ex) { // Return return(BadRequest(ex.Message)); } }
public async Task <IHttpActionResult> Put(int queenId, QueenModel queenModel) { if (!ModelState.IsValid) { return(BadRequest("Queen Model is not valid")); } try { using (var context = new BeeAppContext()) { var queen = 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.Queens) .SelectMany(x => x.Queens) .Where(x => x.Id == queenId) .FirstOrDefaultAsync(); if (queen != null) { queen.Name = queenModel.Name; queen.Date = queenModel.Date; queen.Breed = queenModel.Breed; queen.Colour = queenModel.Colour; queen.State = queenModel.State; queen.Status = queenModel.Status; // Save context.SaveChanges(); // Return return(StatusCode(HttpStatusCode.NoContent)); } // Return return(BadRequest("Queen could not be found")); } } catch (Exception ex) { // Return return(BadRequest(ex.Message)); } }
public async Task <IHttpActionResult> Put(int treatmentId, TreatmentModel treatmentModel) { if (!ModelState.IsValid) { return(BadRequest("Treatment Model is not valid")); } try { using (var context = new BeeAppContext()) { var treatment = 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.Treatments) .SelectMany(x => x.Treatments) .Where(x => x.Id == treatmentId) .FirstOrDefaultAsync(); if (treatment != null) { treatment.Name = treatmentModel.Name; treatment.Date = treatmentModel.Date; treatment.Product = treatmentModel.Product; treatment.Quantity = treatmentModel.Quantity; treatment.Unit = treatmentModel.Unit; treatment.Note = treatmentModel.Note; // Save context.SaveChanges(); // Return return(StatusCode(HttpStatusCode.NoContent)); } // Return return(BadRequest("Treatment could not be found")); } } catch (Exception ex) { // Return return(BadRequest(ex.Message)); } }
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> Put(int hiveId, HiveModel hiveModel) { if (!ModelState.IsValid) { return(BadRequest("Hive Model is not valid")); } try { using (var context = new BeeAppContext()) { var hive = await context.Beekeepers .Where(x => x.ApplicationUserId == _applicationUserId) .Include(x => x.Apiaries) .SelectMany(x => x.Apiaries) .Include(x => x.Hives) .SelectMany(x => x.Hives) .Where(x => x.Id == hiveId) .FirstOrDefaultAsync(); if (hive != null) { hive.Name = hiveModel.Name; hive.Date = hiveModel.Date; hive.Status = hiveModel.Status; hive.Type = hiveModel.Type; hive.Note = hiveModel.Note; hive.Family = hiveModel.Family; hive.FamilyOrigin = hiveModel.FamilyOrigin; // Save context.SaveChanges(); // Return return(StatusCode(HttpStatusCode.NoContent)); } // Return return(BadRequest("Hive could not be found")); } } catch (Exception ex) { // Return return(BadRequest(ex.Message)); } }
public async Task <IHttpActionResult> Put(int workId, WorkModel workModel) { if (!ModelState.IsValid) { return(BadRequest("Work Model is not valid")); } try { using (var context = new BeeAppContext()) { var work = await context.Beekeepers .Where(x => x.ApplicationUserId == _applicationUserId) .Include(x => x.Works) .SelectMany(x => x.Works) .FirstOrDefaultAsync(x => x.Id == workId); if (work != null) { work.Name = workModel.Name; work.Date = workModel.Date; work.Note = workModel.Note; work.IsCompleted = workModel.IsCompleted; // Save context.SaveChanges(); // Return return(StatusCode(HttpStatusCode.NoContent)); } // Return return(BadRequest("Work could not be found")); } } catch (Exception ex) { // Return return(BadRequest(ex.Message)); } }
public async Task <IHttpActionResult> Put(int apiaryId, ApiaryModel apiaryModel) { if (!ModelState.IsValid) { return(BadRequest("Apiary Model is not valid")); } try { using (var context = new BeeAppContext()) { var apiary = await context.Beekeepers .Where(x => x.ApplicationUserId == _applicationUserId) .Include(x => x.Apiaries) .SelectMany(x => x.Apiaries) .FirstOrDefaultAsync(x => x.Id == apiaryId); if (apiary != null) { apiary.Name = apiaryModel.Name; apiary.Place = apiaryModel.Place; apiary.Longtitude = apiaryModel.Longtitude; apiary.Latitude = apiaryModel.Latitude; // Save context.SaveChanges(); // Return return(StatusCode(HttpStatusCode.NoContent)); } // Return return(BadRequest("Apiary could not be found")); } } catch (Exception ex) { // Return return(BadRequest(ex.Message)); } }
public async Task <IHttpActionResult> Post(int apiaryId, int hiveId, InspectionModel inspectionModel) { if (!ModelState.IsValid) { return(BadRequest("Inspection Model is not valid")); } try { using (var context = new BeeAppContext()) { if (await _ensurer.EnsureHiveBelongsToApiary(context, apiaryId, hiveId, _applicationUserId)) { context.Inspections.Add(new Inspection { Date = inspectionModel.Date, Name = inspectionModel.Name, Strength = inspectionModel.Strength, Temper = inspectionModel.Temper, Disease = inspectionModel.Disease, FramesBees = inspectionModel.FramesBees, FramesHoney = inspectionModel.FramesHoney, FramesHoneySupers = inspectionModel.FramesHoneySupers, Drones = inspectionModel.Drones, DroneCells = inspectionModel.DroneCells, HiveId = hiveId }); // Save context.SaveChanges(); } } } catch (Exception ex) { return(BadRequest(ex.Message)); } // Return return(Ok(inspectionModel)); }
public IHttpActionResult Post(int apiaryId, HiveModel hiveModel) { if (!ModelState.IsValid) { return(BadRequest("Hive Model is not valid")); } try { using (var context = new BeeAppContext()) { if (!EnsureCreatingInOwnApiary(context, apiaryId)) { return(BadRequest("The apiary could not be found.")); } context.Hives.Add(new Hive { Name = hiveModel.Name, Date = hiveModel.Date, Status = hiveModel.Status, Type = hiveModel.Type, Note = hiveModel.Note, Family = hiveModel.Family, FamilyOrigin = hiveModel.FamilyOrigin, ApiaryId = apiaryId }); // Save context.SaveChanges(); } } catch (Exception ex) { return(BadRequest(ex.Message)); } // Return return(Ok(hiveModel)); }
public async Task <IHttpActionResult> Post(WorkModel workModel) { if (!ModelState.IsValid) { return(BadRequest("Work Model is not valid")); } try { using (var context = new BeeAppContext()) { var beekeeper = await context.Beekeepers .FirstOrDefaultAsync(x => x.ApplicationUserId == _applicationUserId); if (beekeeper == null) { return(BadRequest()); } // Add work to a beekeeper beekeeper.Works.Add(new Work { Name = workModel.Name, Date = workModel.Date, Note = workModel.Note, IsCompleted = workModel.IsCompleted }); // Save context.SaveChanges(); } } catch (Exception ex) { return(BadRequest(ex.Message)); } // Return return(Ok(workModel)); }
public async Task <IHttpActionResult> Post(ApiaryModel apiaryModel) { if (!ModelState.IsValid) { return(BadRequest("Apiary Model is not valid")); } try { using (var context = new BeeAppContext()) { var beekeeper = await context.Beekeepers .FirstOrDefaultAsync(x => x.ApplicationUserId == _applicationUserId); if (beekeeper == null) { return(BadRequest("The application user could not be found.")); } beekeeper.Apiaries.Add(new Apiary { Name = apiaryModel.Name, Place = apiaryModel.Place, Longtitude = apiaryModel.Longtitude, Latitude = apiaryModel.Latitude }); // Save context.SaveChanges(); } } catch (Exception ex) { return(BadRequest(ex.Message)); } // Return return(Ok(apiaryModel)); }
public async Task <IHttpActionResult> Post(int apiaryId, int hiveId, QueenModel queenModel) { if (!ModelState.IsValid) { return(BadRequest("Queen Model is not valid")); } try { using (var context = new BeeAppContext()) { if (await _ensurer.EnsureHiveBelongsToApiary(context, apiaryId, hiveId, _applicationUserId)) { context.Queens.Add(new Queen { Name = queenModel.Name, Date = queenModel.Date, Breed = queenModel.Breed, Colour = queenModel.Colour, State = queenModel.State, Status = queenModel.Status, HiveId = hiveId }); // Save context.SaveChanges(); } } } catch (Exception ex) { return(BadRequest(ex.Message)); } // Return return(Ok(queenModel)); }
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)); }