예제 #1
0
        public async Task AddProduct(ProductModel product)
        {
            if (product.ContainsNullStrings())
            {
                throw new InvalidOperationException(ServiceErrorsConstants.RequiredFields);
            }

            if (!_categoriesRepository.All().Any(c => c.Id == product.CategoryId))
            {
                throw new InvalidOperationException(string.Format(ServiceErrorsConstants.CategoryDoesNotExist, product.CategoryId));
            }

            var dbEntity = new Product
            {
                CategoryId   = product.CategoryId,
                Manufacturer = product.Manufacturer,
                Model        = product.Model,
                Title        = product.Title,
                Description  = product.Description,
                Quantity     = product.Quantity,
                Price        = product.Price,
                Discount     = product.Discount,
                PurchaseId   = 1,
            };

            _productRepository.Add(dbEntity);
            await _productRepository.SaveChangesAsync();

            var ent = _productRepository.All().ToList();
        }