Exemplo n.º 1
0
        private List <SalesArea> GetSalesAreasFromDatabase(List <string> salesAreaNames)
        {
            var salesAreas = salesAreaNames == null || !salesAreaNames.Any()
                ? _salesAreaRepository.GetAll().ToList()
                : _salesAreaRepository.FindByNames(salesAreaNames);

            if (salesAreas == null || !salesAreas.Any())
            {
                throw new DataSyncException(DataSyncErrorCode.SalesAreaNotFound, "SalesArea not found");
            }
            return(salesAreas);
        }
Exemplo n.º 2
0
        public IHttpActionResult Post([FromBody] CreateHoliday command)
        {
            if (!ModelState.IsValid)
            {
                return(this.Error().InvalidParameters());
            }

            List <SalesArea> salesAreas;

            if (command.SalesAreaNames == null || !command.SalesAreaNames.Any())
            {
                salesAreas = _salesAreaRepository.GetAll().ToList(); // Apply to all
            }
            else
            {
                salesAreas = _salesAreaRepository.FindByNames(command.SalesAreaNames);
            }

            if (salesAreas == null || !salesAreas.Any())
            {
                return(NotFound());
            }

            ValidateHolidays(command.HolidayDateRanges);
            salesAreas.ForEach(salesArea =>
            {
                switch (command.HolidayType)
                {
                case HolidayType.PublicHoliday:
                    salesArea.PublicHolidays = Add(salesArea.PublicHolidays, command.HolidayDateRanges);
                    break;

                case HolidayType.SchoolHoliday:
                    salesArea.SchoolHolidays = Add(salesArea.SchoolHolidays, command.HolidayDateRanges);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(command.HolidayType), command.HolidayType,
                                                          "Invalid holiday type");
                }
            });
            _salesAreaRepository.Update(salesAreas);
            return(Ok());
        }
Exemplo n.º 3
0
        public static void ValidateSalesArea(this ISalesAreaRepository repository, IReadOnlyCollection <string> salesAreaNames)
        {
            if (repository is null)
            {
                throw new ArgumentNullException(nameof(repository));
            }

            if (!salesAreaNames.Any())
            {
                throw new DataSyncException(DataSyncErrorCode.SalesAreaNotFound, "Empty Sales area names list");
            }

            var sanitizedNames = salesAreaNames.Distinct().Trim();
            IReadOnlyCollection <string> existingSalesAreas =
                repository.FindByNames(sanitizedNames)
                .Select(r => r.Name)
                .ToArray();

            ValidateSalesArea(existingSalesAreas, salesAreaNames);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get sales ares id by names
        /// </summary>
        /// <param name="salesAreaNames"></param>
        /// <returns></returns>
        public static void ValidateSaleArea(
            this ISalesAreaRepository repository,
            List <string> salesAreaNames)
        {
            if (repository is null)
            {
                throw new ArgumentNullException(nameof(repository));
            }

            if (salesAreaNames?.Count == 0)
            {
                throw new InvalidDataException("No sales area names to validate");
            }

            var names = salesAreaNames.Distinct().Trim();
            List <SalesArea> salesAreas = repository.FindByNames(names);

            if (salesAreas is null)
            {
                throw new InvalidDataException(
                          "Invalid Sales Area Names: " + string.Join(",", names)
                          );
            }

            var invalidNames = names
                               .Except(salesAreas.Select(s => s.Name))
                               .ToList();

            if (invalidNames.Count == 0)
            {
                return;
            }

            throw new InvalidDataException(
                      "Invalid Sales Area Names: " + string.Join(",", invalidNames)
                      );
        }