public Result CreateProduct(ProductModel productModel) { //validation control if (string.IsNullOrWhiteSpace(productModel.Name) || string.IsNullOrWhiteSpace(productModel.Description) || productModel.Files.Length == 0 || string.IsNullOrWhiteSpace(productModel.Barcode) || productModel.Quantity == 0 || productModel.Quantity == null || productModel.Price == null || productModel.Price == 0) { return(new FailureResult("Please fill all fields.")); } //product save var productEntity = productModel.Adapt <Product>(); productEntity.DateCreated = DateTime.Now; productEntity.UserCreated = productModel.UserCreated; _unitOfWork.BeginTransaction(); _productRepository.Insert(productEntity); _unitOfWork.SaveChanges(); // save images var result = SaveProductPhoto(productModel, productEntity); if (!result.Success) { _unitOfWork.Rollback(); return(new FailureResult()); } else { _unitOfWork.Commit(); } return(new SuccessResult(true, "Success")); }
public virtual TblProducts PrepareTblProducts(ProductModel product) { var result = product.Adapt <TblProducts>(); result.Tags = product.ProductTags?.Select(p => new TblPostTags() { Tag = p }).ToList(); result.Categories = product.ProductCategories?.Select(p => new TblPostCategories() { Id = p }).ToList(); return(result); }
public int Add(ProductModel model) { var newProduct = model.Adapt <ProductEntity>(); if (model.BrandId.HasValue) { if (!brandRepository.FindById(model.BrandId.Value)) { throw new ValidatorException("Unkonwn brand"); } } if (model.Categories.Count > 0) { var categoryIds = model.Categories .Select(x => x.CategoryId) .ToList(); if (!categoryRepository.FindByIds(categoryIds)) { throw new ValidatorException("Unkonwn category"); } var newProductCategories = new List <ProductCategoryEntity>(); foreach (var category in model.Categories) { var newProductCategory = new ProductCategoryEntity { ProductId = newProduct.ProductId, Product = newProduct, CategoryId = category.CategoryId }; newProductCategories.Add(newProductCategory); } productCategoryRepository.AddCategories(newProductCategories); } productValidator.Validate(newProduct); productRepository.Add(newProduct); return(newProduct.ProductId); }