public ActionResult GetCompletedShifts() { IsItMyTurnContext context = new IsItMyTurnContext(); try { var shifts = (from cf in context.CompletedShifts select new { cf.ShiftId, cf.ApartmentId, cf.Apartment.ApartmentName, cf.Date }).ToList(); return(Ok(shifts)); } catch (Exception ex) { return(BadRequest("Problem detected while getting shifts. Error message: " + ex.Message)); } finally { context.Dispose(); } }
public ActionResult GetApartments() { IsItMyTurnContext context = new IsItMyTurnContext(); try { // All apartments to list var apartments = (from a in context.Apartments select new { a.ApartmentId, a.ApartmentName }).ToList(); return(Ok(apartments)); } catch (Exception ex) { return(BadRequest("Problem detected while getting apartments. Error message: " + ex.Message)); } finally { context.Dispose(); } }
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> DeleteShift(int id) { IsItMyTurnContext context = new IsItMyTurnContext(); try { if (id != 0) { // Searching right shift with ID var shift = context.CompletedShifts.Find(id); context.Remove(shift); int successCount = context.SaveChanges(); // Notifications bool notificationSuccess = await HandleNotification(); if (notificationSuccess == true && successCount > 0) { // Everything is OK return(Ok("New shift has deleted successfully!")); } else if (notificationSuccess == false && successCount > 0) { // Problems with notifications. Deletion from database is OK return(StatusCode(201)); } else { // Nothing is OK return(BadRequest("Problems with notification and deleting the row from database!")); } } else { return(BadRequest("Shift ID is null!")); } } catch (Exception ex) { return(BadRequest("Problem detected while deleting a shift. Error message: " + ex.InnerException)); } finally { context.Dispose(); } }
// Build notification public async Task <bool> HandleNotification() { IsItMyTurnContext context = new IsItMyTurnContext(); string nextApartmentInShift = GetApartmentForNextShift(); // Title and body for notification FCMNotification notification = new FCMNotification(); notification.title = "Leikkuuvuoro vaihtui!"; notification.body = "Seuraavana vuorossa: " + nextApartmentInShift; notification.sound = "default"; // Tokens to array from database which will get the notification string[] fcmTokens = (from ft in context.FcmTokens select ft.Token).ToArray(); List <bool> successList = new List <bool>(); // The notification is sent to device and the result will be added to list of successes foreach (var token in fcmTokens) { FCMBody body = new FCMBody(); body.registration_ids = new string[] { token }; body.notification = notification; bool result = await SendNotification(body); successList.Add(result); } // If all the notification send responses are OK, return true bool allTrue = successList.All(s => Equals(true, s)); if (allTrue) { return(true); } else { return(false); } }
public Task <bool> DeleteOldToken(string oldToken) { IsItMyTurnContext context = new IsItMyTurnContext(); try { FcmTokens tokenObj = (from ft in context.FcmTokens where ft.Token == oldToken select ft).FirstOrDefault(); context.FcmTokens.Remove(tokenObj); context.SaveChanges(); return(Task.FromResult(true)); } catch (Exception) { throw new Exception(); } }
public ActionResult GetApartmentForCurrentShift() { IsItMyTurnContext context = new IsItMyTurnContext(); try { // Id of the apartment which made the last shift int lastApartmentId = (from cf in context.CompletedShifts orderby cf.ShiftId descending select cf.ApartmentId).FirstOrDefault(); int currentApartmentId; // If the apartment ID is 5, next in shift will be an apartment 1 if (lastApartmentId < 6) { currentApartmentId = lastApartmentId + 1; } else { currentApartmentId = 1; } // Get the apartment number based on currentApartmentId string currentApartment = (from a in context.Apartments where a.ApartmentId == currentApartmentId select a.ApartmentName).FirstOrDefault(); return(Ok(currentApartment)); } catch (Exception ex) { return(BadRequest("Problem detected while getting an apartment for current shift. Error message: " + ex.Message)); } finally { context.Dispose(); } }
// Apartment number for notification public string GetApartmentForNextShift() { IsItMyTurnContext context = new IsItMyTurnContext(); try { // Id of the apartment which made the last shift int lastApartmentId = (from cf in context.CompletedShifts orderby cf.ShiftId descending select cf.ApartmentId).FirstOrDefault(); int currentApartmentId; // If the apartment ID is 6, next in shift will be an apartment 1 if (lastApartmentId < 6) { currentApartmentId = lastApartmentId + 1; } else { currentApartmentId = 1; } // Get the apartment number based on currentApartmentId string currentApartment = (from a in context.Apartments where a.ApartmentId == currentApartmentId select a.ApartmentName).FirstOrDefault(); return(currentApartment); } catch (Exception ex) { return($"Error: {ex.Message}"); } finally { context.Dispose(); } }
public async Task <ActionResult> HandleDevicesAndTokens([FromBody] OtherModels.Identifier identifier) { IsItMyTurnContext context = new IsItMyTurnContext(); var deviceCheck = (from d in context.Devices where d.UniqueIdentifier == identifier.UniqueIdentifier select d).FirstOrDefault(); // Check if device exists in database if (deviceCheck == null) { Devices device = new Devices() { UniqueIdentifier = identifier.UniqueIdentifier }; context.Devices.Add(device); int success = await context.SaveChangesAsync(); // When device has added successfully, a token will be added to device if (success > 0) { int deviceId = (from d in context.Devices where d.UniqueIdentifier == identifier.UniqueIdentifier select d.DeviceId).FirstOrDefault(); FcmTokens token = new FcmTokens() { DeviceId = deviceId, Token = identifier.Token }; context.FcmTokens.Add(token); context.SaveChanges(); return(Ok("A new device and a token has added successfully!")); } else { return(BadRequest("Problem detected while adding a device identifier to database.")); } } else { // If device exists, check if the device has a token var tokenCheck = (from ft in context.FcmTokens where ft.DeviceId == deviceCheck.DeviceId select ft).FirstOrDefault(); // If a token is not exist, it will be added to database. Otherwise the old one will be updated if (tokenCheck == null) { FcmTokens token = new FcmTokens() { DeviceId = deviceCheck.DeviceId, Token = identifier.Token }; context.FcmTokens.Add(token); int success = context.SaveChanges(); if (success > 0) { return(Ok("Token has added successfully!")); } else { return(BadRequest("Problem detected while adding a token for device to database.")); } } else { tokenCheck.Token = identifier.Token; int success = context.SaveChanges(); if (success > 0) { return(Ok("Token updated successfully!")); } else { return(BadRequest("Problem detected while updating a token for device to database.")); } } } }
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")); } }