Exemple #1
0
        public async Task <IActionResult> Create(ShiftVM shiftModel)
        {
            // Check that the data is present
            if (!ModelState.IsValid || shiftModel.LocationId == 0)
            {
                return(BadRequest());
            }

            // Check that the location exists
            Location location = await _locationService.GetLocation(shiftModel.LocationId);

            if (location == null)
            {
                return(BadRequest());
            }

            // Check that the user has management access
            string userId = _userManager.GetUserId(User);
            Member member = await _memberService.GetMember(location.GroupId, userId);

            if (member == null || !member.Approved || !member.CanManageShifts)
            {
                return(BadRequest());
            }

            // Make sure the system is not overloaded with shifts
            if (shiftModel.CreateCopies &&
                shiftModel.CopyUntil.Date >= shiftModel.Start.Date.AddYears(1))
            {
                return(BadRequest());
            }

            var newShifts = new List <Shift>();

            newShifts.Add(new Shift()
            {
                LocationId      = shiftModel.LocationId,
                Start           = shiftModel.Start,
                End             = shiftModel.End,
                MinParticipants = shiftModel.MinParticipants,
                MaxParticipants = shiftModel.MaxParticipants,
                Instructions    = shiftModel.Instructions,
                Enabled         = shiftModel.Enabled
            });

            if (shiftModel.CreateCopies)
            {
                var totalDays = shiftModel.CopyUntil.Date - shiftModel.Start.Date;

                for (int day = 1; day <= totalDays.Days; day++)
                {
                    DayOfWeek newDayOfWeek = shiftModel.Start.AddDays(day).DayOfWeek;

                    if ((newDayOfWeek == DayOfWeek.Monday && shiftModel.CopyMonday) ||
                        (newDayOfWeek == DayOfWeek.Tuesday && shiftModel.CopyTuesday) ||
                        (newDayOfWeek == DayOfWeek.Wednesday && shiftModel.CopyWednesday) ||
                        (newDayOfWeek == DayOfWeek.Thursday && shiftModel.CopyThursday) ||
                        (newDayOfWeek == DayOfWeek.Friday && shiftModel.CopyFriday) ||
                        (newDayOfWeek == DayOfWeek.Saturday && shiftModel.CopySaturday) ||
                        (newDayOfWeek == DayOfWeek.Sunday && shiftModel.CopySunday))
                    {
                        var newStartDate = shiftModel.Start.AddDays(day);
                        var newEndDate   = shiftModel.End.AddDays(day);

                        newShifts.Add(new Shift()
                        {
                            LocationId      = shiftModel.LocationId,
                            Start           = newStartDate,
                            End             = newEndDate,
                            MinParticipants = shiftModel.MinParticipants,
                            MaxParticipants = shiftModel.MaxParticipants,
                            Instructions    = shiftModel.Instructions,
                            Enabled         = shiftModel.Enabled
                        });
                    }
                }
            }

            await _shiftService.CreateShifts(newShifts);

            return(Ok());
        }