Esempio n. 1
0
        public async Task <ApiResult <string> > CreatePromotionAsync(PromotionForCreationDto creationDto)
        {
            if (creationDto.FromDate <= DateTime.Now || creationDto.ToDate <= DateTime.Now || creationDto.FromDate > creationDto.ToDate)
            {
                return(new ApiResult <string>(HttpStatusCode.BadRequest, $"Thời gian diễn ra sự kiện không hợp lệ"));
            }
            var promotion = new Promotion()
            {
                Id            = Guid.NewGuid().ToString("D"),
                Description   = creationDto.Description,
                Name          = creationDto.Name,
                CategoryId    = string.IsNullOrEmpty(creationDto.CategoryId)?null: creationDto.CategoryId,
                DiscountType  = creationDto.DiscountType,
                DiscountValue = creationDto.DiscountValue,
                FromDate      = creationDto.FromDate,
                ToDate        = creationDto.ToDate.AddHours(23).AddMinutes(59).AddSeconds(59),
            };
            var promotionDetails = new List <PromotionDetail>();

            if (creationDto.Products != null && creationDto.Products.Count() > 0)
            {
                foreach (var item in creationDto.Products)
                {
                    if (await CheckPromotionDetail(item.ProductId, promotion.FromDate, promotion.ToDate) == false)
                    {
                        promotionDetails.Add(new PromotionDetail()
                        {
                            Id            = Guid.NewGuid().ToString("D"),
                            DiscountType  = item.DiscountType,
                            DiscountValue = item.DiscountValue,
                            ProductId     = item.ProductId,
                            PromotionId   = promotion.Id
                        });
                    }
                    else
                    {
                        return(new ApiResult <string>(HttpStatusCode.BadRequest, $"Sản phẩm có mã {item.ProductId} đang trong một chương trình giảm giá khác"));
                    }
                }
            }
            promotion.PromotionDetails = promotionDetails;
            await _context.Promotions.AddAsync(promotion);

            await _context.SaveChangesAsync();

            return(new ApiResult <string>(HttpStatusCode.OK, $"Tạo mới chương trình khuyến mãi thành công")
            {
                ResultObj = promotion.Id
            });
        }
Esempio n. 2
0
        public async Task <IActionResult> CreatePromotion(PromotionForCreationDto creationDto)
        {
            var result = await _productService.CreatePromotionAsync(creationDto);

            return(StatusCode((int)result.Code, result));
        }