コード例 #1
0
        public int Create(CreateProductOfferInputModel input, string currentUserId)
        {
            if (_offersRepository.ExistsByName(input.Name))
            {
                throw new ArgumentException("Offer with this name exists");
            }

            IEnumerable <Product> selectedProducts = _productsRepository.GetByIds(input.Products);

            Offer offer = new Offer
            {
                Name       = input.Name,
                TotalPrice = decimal.Round(input.TotalPrice),
                Discount   = input.Discount,
                ExpiryDate = DateTime.ParseExact(input.ExpiryDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                Image      = input.ImageUrl,
                AddedById  = currentUserId,
                IsActive   = input.IsActive
            };

            _offersRepository.Add(offer);
            _offersRepository.SaveChanges();

            foreach (var selectedProduct in selectedProducts)
            {
                ProductOffer po = new ProductOffer
                {
                    OfferId   = offer.Id,
                    ProductId = selectedProduct.Id
                };

                _productOffersRepository.Add(po);
                _productOffersRepository.SaveChanges();
            }

            return(offer.Id);
        }