コード例 #1
0
        public async Task <ActionResult <PromoViewModel> > Create(CreatePromoBody createPromoBody)
        {
            try
            {
                Logger.LogInformation("Creating {Discount}% promo for {Id}", createPromoBody.Discount, createPromoBody.EventId);
                var result = await PromoService.Create(createPromoBody);

                return(result);
            }
            catch (Exception e)
            {
                Logger.LogInformation("Create exception {Exception}", e.ToString());
                return(BadRequest(e));
            }
        }
コード例 #2
0
        /// <inheritdoc />
        /// <exception cref="EventNotFoundException">Thrown if the event is not found</exception>
        /// <exception cref="InvalidPromoException">Thrown if the promo dates are not valid</exception>
        public async Task <PromoViewModel> Create(CreatePromoBody createPromoBody)
        {
            var e = await ApplicationContext.Events.Include(x => x.Promos).AsSplitQuery()
                    .FirstOrDefaultAsync(x => x.Id == createPromoBody.EventId);

            if (e == null)
            {
                throw new EventNotFoundException();
            }

            // check if the promo conflicts with existing promos
            if (e.Promos.Any(p => p.Start >= createPromoBody.Start && createPromoBody.End <= e.EndLocal))
            {
                throw new InvalidPromoException();
            }

            Promo promo = new Promo
            {
                Start    = createPromoBody.Start,
                End      = createPromoBody.End,
                Active   = createPromoBody.Active,
                Discount = createPromoBody.Discount,
                Event    = e
            };

            try
            {
                await ApplicationContext.Promos.AddAsync(promo);

                await ApplicationContext.SaveChangesAsync();

                return(Mapper.Map <PromoViewModel>(promo));
            }
            catch (Exception ex)
            {
                Logger.LogInformation("Create exception {Exception}", ex.ToString());
                throw;
            }
        }