public IHttpActionResult SaveResidentShift(int residentId, [FromBody] ResidentShiftModel shift) { try { HttpRequires.IsNotNull(shift, "Shift Data Required"); var response = _residentSvc.SaveShift(shift); HttpAssert.Success(response); // NOTE: THIS SECTION CONTAINS CHANGES ADDED AFTER THE // DEADLINE, TO RESOLVE BUG PERSISTING TIME ENTRIES return(Ok(new { Shift = response.Result.Item1, Shifts = response.Result.Item2 })); } catch (ShiftConflictException ex) { return(this.BadRequest(new { Type = "ShiftConflict", Data = ex.Conflicts })); } catch (Exception ex) { if (_logger != null) { _logger.Write(ex); } return(InternalServerError()); } }
/// <summary> /// Method to save a resident's shift details. /// Will perform intsert for new records and update existing. /// </summary> /// <param name="model"></param> /// <returns></returns> public ResponseModel <ResidentShiftModel> Save(ResidentShiftModel model) { Func <ResidentShiftModel> resolver = () => { var data = Mapper.Map(model); // NOTE: THIS SECTION CONTAINS CHANGES ADDED AFTER THE // DEADLINE, TO RESOLVE BUG PERSISTING TIME ENTRIES if (data.Id > 0) { var dataEntity = DhDataContext.ResidentShifts.FirstOrDefault(e => e.Id == data.Id); dataEntity.StartDateTimeUtc = data.StartDateTimeUtc; dataEntity.EndDateTimeUtc = data.EndDateTimeUtc; DhDataContext.Entry(dataEntity).State = EntityState.Modified; } else { DhDataContext.ResidentShifts.Add(data); } DhDataContext.SaveChanges(); var affectedRecords = DhDataContext.SaveChanges(); return(Mapper.Map(data)); }; return(Persist(resolver)); }
/// <summary> /// Method to remove a resident shift. /// </summary> /// <param name="model"></param> /// <returns></returns> public ResponseModel Delete(ResidentShiftModel model) { Action resolver = () => { var data = Mapper.Map(model); DhDataContext.ResidentShifts.Remove(data); DhDataContext.SaveChanges(); }; return(Persist(resolver)); }
public ResidentShift Map(ResidentShiftModel model) { var resShift = new ResidentShift { InstitutionResidentId = model.InstitutionResidentId, //Ensure that the client times are mapped to UTC before persistence EndDateTimeUtc = model.EndDateTime.HasValue ? model.EndDateTime.Value.ToUniversalTime() : (DateTime?)null, StartDateTimeUtc = model.StartDateTime.ToUniversalTime(), EntryDateTimeUtc = model.EntryDateTime.ToUniversalTime() }; if (model.Id > 0) { resShift.Id = model.Id; } return(resShift); }
/// <summary> /// Method to evalute if an existing shift's times conflict with the unsaved one. /// </summary> /// <param name="existing"></param> /// <param name="unsaved"></param> /// <returns></returns> private bool DoesShiftConflict(ResidentShiftModel existing, ResidentShiftModel unsaved) { if (unsaved.Id == existing.Id) { return(false); } if (unsaved.StartDateTime >= existing.StartDateTime && (!existing.EndDateTime.HasValue || unsaved.StartDateTime <= existing.EndDateTime.Value)) { return(true); } if (unsaved.EndDateTime.HasValue && unsaved.EndDateTime.Value >= existing.StartDateTime && existing.EndDateTime.HasValue && unsaved.EndDateTime.Value <= existing.EndDateTime.Value) { return(true); } return(false); }
/// <summary> /// Method to save a resident's shift /// </summary> /// <param name="shift"></param> /// <param name="overrideAcknowledged">Indication the user has chosen to overwrite existing shifts</param> /// <returns></returns> public ResponseModel <Tuple <ResidentShiftModel, IEnumerable <ResidentShiftModel> > > SaveShift(ResidentShiftModel shift) { // NOTE: THIS SECTION CONTAINS CHANGES ADDED AFTER THE // DEADLINE, TO RESOLVE BUG PERSISTING TIME ENTRIES Func <Tuple <ResidentShiftModel, IEnumerable <ResidentShiftModel> > > func = () => { var shifts = _residentRepo.FindShiftsByResidentId(shift.InstitutionResidentId).Result.ToList(); var conflicts = shifts.Where(s => DoesShiftConflict(s, shift)).ToList(); if (conflicts.Any() && !shift.OverrideAcknowleded) { throw new ShiftConflictException() { Conflicts = conflicts }; } using (var scope = new TransactionScope()) { conflicts.ForEach(c => _residentRepo.Delete(c)); var response = _residentRepo.Save(shift); if (!response.HasError) { shift = response.Result; } scope.Complete(); } shifts = _residentRepo.FindShiftsByResidentId(shift.InstitutionResidentId).Result.ToList(); return(new Tuple <ResidentShiftModel, IEnumerable <ResidentShiftModel> >(shift, shifts)); }; return(Execute(func)); }