Example #1
0
        public async Task <ActionResult <int> > CheckSlots([FromForm] string testingDay = "2020-10-31T00:00:00+00:00", [FromForm] int from = 9, [FromForm] int until = 20)
        {
            try
            {
                if (!User.IsAdmin(userRepository))
                {
                    throw new Exception(localizer[Resources.Controllers_AdminController.Only_admin_is_allowed_to_manage_time].Value);
                }

                var ret = 0;
                foreach (var item in await placeRepository.ListAll())
                {
                    var time = $"{TimeSpan.FromHours(from)}-{TimeSpan.FromHours(until)}";
                    ret += await slotRepository.CheckSlots(DateTimeOffset.Parse(testingDay, CultureInfo.InvariantCulture).Ticks, item.Id, time);
                }
                return(Ok(ret));
            }
            catch (Exception exc)
            {
                logger.LogError(exc, exc.Message);

                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
        }
Example #2
0
        public async Task <ActionResult <int> > ScheduleOpenningHours([FromBody] TimeUpdate[] actions = null)
        {
            try
            {
                if (actions is null)
                {
                    throw new ArgumentNullException(nameof(actions));
                }
                var isGlobalAdmin = User.IsAdmin(userRepository);
                if (!await User.IsPlaceProviderAdmin(userRepository, placeProviderRepository))
                {
                    throw new Exception("You must be place provider admin to be able to set openning hours");
                }

                var list = (await placeRepository.ListAll());
                if (!isGlobalAdmin)
                {
                    list = list.Where(p => p.PlaceProviderId == User.GetPlaceProvider());
                }
                var ids = list.Select(i => i.Id).Distinct().ToHashSet();
                if (actions.Any(a => string.IsNullOrEmpty(a.PlaceId) || (a.PlaceId != "__ALL__" && !ids.Contains(a.PlaceId))))
                {
                    throw new Exception("You cannot manage all places you have selected");
                }
                var places = new Dictionary <string, Place>();
                foreach (var placeId in ids)
                {
                    places[placeId] = await placeRepository.GetPlace(placeId);

                    if (places[placeId] == null)
                    {
                        throw new Exception($"Invalid place with id {placeId}");
                    }

                    foreach (var action in actions.Where(a => a.PlaceId == placeId))
                    {
                        if (action.Type == "set")
                        {
                            if (action.OpeningHoursTemplateId == 1)
                            {
                                if (!places[placeId].OpeningHoursWorkDay.ValidateOpeningHours())
                                {
                                    throw new Exception($"Place {places[placeId].Name} does not have valid opening hours: {places[placeId].OpeningHoursWorkDay}");
                                }
                            }
                            if (action.OpeningHoursTemplateId == 2)
                            {
                                if (!places[placeId].OpeningHoursOther1.ValidateOpeningHours())
                                {
                                    throw new Exception($"Place {places[placeId].Name} does not have valid opening hours: {places[placeId].OpeningHoursOther1}");
                                }
                            }
                            if (action.OpeningHoursTemplateId == 3)
                            {
                                if (!places[placeId].OpeningHoursOther2.ValidateOpeningHours())
                                {
                                    throw new Exception($"Place {places[placeId].Name} does not have valid opening hours: {places[placeId].OpeningHoursOther2}");
                                }
                            }
                        }
                    }
                }
                var ret = 0;

                foreach (var action in actions)
                {
                    foreach (var placeId in ids)
                    {
                        if (action.PlaceId != "__ALL__" && action.PlaceId != placeId)
                        {
                            continue;
                        }

                        var hours    = "";
                        var dayTicks = action.Date.RoundDay();// DateTimeOffset.Parse(action.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), CultureInfo.InvariantCulture).UtcTicks;
                        if (action.Type == "set")
                        {
                            if (action.OpeningHoursTemplateId == 1)
                            {
                                hours = places[placeId].OpeningHoursWorkDay;
                            }
                            if (action.OpeningHoursTemplateId == 2)
                            {
                                hours = places[placeId].OpeningHoursOther1;
                            }
                            if (action.OpeningHoursTemplateId == 3)
                            {
                                hours = places[placeId].OpeningHoursOther2;
                            }
                        }
                        else if (action.Type == "delete")
                        {
                            hours = "";
                        }

                        ret += await slotRepository.CheckSlots(dayTicks, placeId, hours, action.OpeningHoursTemplateId);
                    }
                }

                return(Ok(ret));
            }
            catch (ArgumentException exc)
            {
                logger.LogError(exc.Message);
                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
            catch (Exception exc)
            {
                logger.LogError(exc, exc.Message);
                return(BadRequest(new ProblemDetails()
                {
                    Detail = exc.Message
                }));
            }
        }