Beispiel #1
0
        /// <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));
        }