Example #1
0
        public async Task <ActionResult <Availability> > SaveAvailabilityAsync(AvailabilityViewModel availabilityViewModel)
        {
            if (availabilityViewModel == null)
            {
                return(BadRequest("No valid availability received"));
            }
            if (availabilityViewModel.ShiftId == Guid.Empty)
            {
                return(BadRequest("No valid shiftId received"));
            }
            if (availabilityViewModel.ParticipationId == Guid.Empty)
            {
                return(BadRequest("No valid participationId received"));
            }
            if (availabilityViewModel.Type == AvailibilityType.Scheduled)
            {
                return(BadRequest("Nice try, You cannot schedule yourself"));
            }

            try
            {
                Availability availability =
                    (await availabilityService.GetAvailability((Guid)availabilityViewModel.ParticipationId,
                                                               availabilityViewModel.ShiftId)).Data;
                if (availability != null)
                {
                    return(UnprocessableEntity(new ErrorViewModel
                    {
                        Type = Type.Error, Message = "Availability already exists"
                    }));
                }

                availability = AvailabilityViewModel.CreateAvailability(availabilityViewModel);
                if (availability == null)
                {
                    return(BadRequest("Unable to convert availabilityViewModel to Availability"));
                }

                if (availability.Participation == null || availability.Shift == null)
                {
                    return(BadRequest("Unable to add Participation and/of shift to Availability"));
                }

                availability.Participation = null;
                availability.Shift         = null;

                string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity);
                availability.LastEditBy = oid;

                TaskResult <Availability> result;
                if (availability.Id == Guid.Empty)
                {
                    result = await availabilityService.AddAvailability(availability);
                }
                else
                {
                    return(BadRequest("Cannot update existing Availability with post method"));
                }

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                return(Ok(AvailabilityViewModel.CreateVm(result.Data)));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(SaveAvailabilityAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Example #2
0
        public async Task <ActionResult <AvailabilityViewModel> > UpdateAvailabilityAsync(
            AvailabilityViewModel availabilityViewModel)
        {
            if (availabilityViewModel == null || availabilityViewModel.Id == Guid.Empty)
            {
                return(BadRequest("No valid availability received"));
            }
            if (availabilityViewModel.ShiftId == Guid.Empty)
            {
                return(BadRequest("No valid shiftId received"));
            }
            if (availabilityViewModel.ParticipationId == Guid.Empty)
            {
                return(BadRequest("No valid participationId received"));
            }
            if (availabilityViewModel.Type == AvailibilityType.Scheduled)
            {
                return(BadRequest("Nice try, You cannot schedule yourself"));
            }
            try
            {
                Availability availability = (await availabilityService.GetAvailability(availabilityViewModel.Id)).Data;
                if (availability == null)
                {
                    return(BadRequest("Unable to convert availabilityViewModel to Availability"));
                }
                if (availability.Type == AvailibilityType.Scheduled)
                {
                    return(BadRequest("Cannot modify availability when user is already scheduled"));
                }
                if (!availability.RowVersion.SequenceEqual(availabilityViewModel.RowVersion))
                {
                    return(BadRequest("Outdated entity received"));
                }

                availability.Preference = availabilityViewModel.Preference;
                availability.Type       = availabilityViewModel.Type;

                string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity);
                availability.LastEditBy = oid;

                TaskResult <Availability> result = await availabilityService.UpdateAvailability(availability);

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                AvailabilityViewModel vm = AvailabilityViewModel.CreateVm(result.Data);

                return(Ok(vm));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(SaveAvailabilityAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Example #3
0
        public async Task <ActionResult <List <ShiftViewModel> > > ExportDataAsync(Guid projectId)
        {
            if (projectId == Guid.Empty)
            {
                return(BadRequest("No valid id."));
            }
            try
            {
                TaskListResult <Shift> result = await shiftService.ExportDataAsync(projectId);

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                if (result.Data == null || result.Data.Count == 0)
                {
                    return(Ok(new List <ShiftViewModel>()));
                }

                List <ShiftViewModel>  shiftVmList      = new List <ShiftViewModel>();
                List <PersonViewModel> personViewModels = new List <PersonViewModel>();
                foreach (Shift shift in result.Data)
                {
                    ShiftViewModel shiftVm = ShiftViewModel.CreateVm(shift);
                    shiftVm.Availabilities = new List <AvailabilityViewModel>();
                    foreach (Availability availability in shift.Availabilities)
                    {
                        PersonViewModel pvm;
                        Guid            id = availability.Participation.PersonId;
                        if (personViewModels.FirstOrDefault(pvms => pvms.Id == id) == null)
                        {
                            TaskResult <User> person = await personService.GetUserAsync(id);

                            pvm = PersonViewModel.CreateVmFromUser(person.Data,
                                                                   Extensions.GetInstance(b2CExtentionApplicationId));
                            personViewModels.Add(pvm);
                        }
                        else
                        {
                            pvm = personViewModels.FirstOrDefault(pvm => pvm.Id == id);
                        }

                        AvailabilityViewModel avm = AvailabilityViewModel.CreateVm(availability);
                        avm.Participation.Person = pvm;
                        shiftVm.Availabilities.Add(avm);
                    }

                    shiftVmList.Add(shiftVm);
                }

                return(Ok(shiftVmList));
            }
            catch (Exception ex)
            {
                string message = GetType()
                                 .Name + "Error in " + nameof(GetShiftAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }