public async Task <ActionResult> AddNewShift([FromBody] CompletedShifts newShiftdata) { IsItMyTurnContext context = new IsItMyTurnContext(); try { if (newShiftdata != null) { // Object for database CompletedShifts shift = new CompletedShifts() { ApartmentId = newShiftdata.ApartmentId, Date = newShiftdata.Date }; context.CompletedShifts.Add(shift); int successCount = context.SaveChanges(); // Notifications bool notificationSuccess = await HandleNotification(); if (notificationSuccess == true && successCount > 0) { // Everything is OK return(Ok("New shift has added successfully!")); } else if (notificationSuccess == false && successCount > 0) { // Problems with notifications. Addition to database is OK return(StatusCode(201)); } else { // Nothing is OK return(BadRequest("Problems with notification and adding the row to database!")); } } else { return(NotFound("New shift data is missing!")); } } catch (Exception ex) { return(BadRequest("Problem detected while adding a new shift. Error message: " + ex.Message)); } finally { context.Dispose(); } }
public async Task <ActionResult> UpdateShiftData(int id, [FromBody] CompletedShifts newShiftData) { IsItMyTurnContext context = new IsItMyTurnContext(); if (id != 0) { CompletedShifts shift = (from cf in context.CompletedShifts where cf.ShiftId == id select cf).FirstOrDefault(); try { if (shift != null) { bool hasApartmentChanged = shift.ApartmentId != newShiftData.ApartmentId; shift.ApartmentId = newShiftData.ApartmentId; shift.Date = newShiftData.Date; int successCount = context.SaveChanges(); // When the apartment ID has changed, the push notifications will be sent if (hasApartmentChanged) { // Notifications bool notificationSuccess = await HandleNotification(); if (notificationSuccess == true && successCount > 0) { // Everything is OK return(Ok("A shift has updated successfully!")); } else if (notificationSuccess == false && successCount > 0) { // Problems with notifications. Database update is OK return(StatusCode(201)); } else { // Nothing is OK return(BadRequest("Problems with notification and updating the row to database!")); } } else { return(Ok("A shift has updated successfully!")); } } else { return(NotFound("Shift with ID: " + id + " not found")); } } catch (Exception ex) { return(BadRequest("Problem detected while adding a new shift. Error message: " + ex.Message)); } finally { context.Dispose(); } } else { return(BadRequest("Problems")); } }