public async Task <PriceDto> Create(Guid userId, PriceForCreationDto priceForCreationDto) { var product = await _productRepository.GetById(priceForCreationDto.ProductId); if (product == null) { throw new KeyNotFoundException($"Product with id: {priceForCreationDto.ProductId} not found."); } var price = new Price() { Value = priceForCreationDto.Value, DateTime = priceForCreationDto.DateTime, ProductId = priceForCreationDto.ProductId, Product = product, CreatedBy = userId }; price = await _priceRepository.Add(price); await _priceRepository.CommitAsync(); return(_mapper.Map <Price, PriceDto>(price)); }
public async Task <ProductDto> Create(Guid userId, ProductForCreationDto productForCreationDto) { var vendor = await _vendorRepository.GetById(productForCreationDto.VendorId); if (vendor == null) { throw new KeyNotFoundException($"Vendor with id: {productForCreationDto.VendorId} not found."); } var category = await _categoryRepository.GetById(productForCreationDto.CategoryId); if (category == null) { throw new KeyNotFoundException($"Category with id: {productForCreationDto.CategoryId} not found."); } var product = new Product() { Name = productForCreationDto.Name, Description = productForCreationDto.Description, Code = productForCreationDto.Code, CategoryId = productForCreationDto.CategoryId, VendorId = productForCreationDto.VendorId, Brand = productForCreationDto.Brand, MinimumStock = productForCreationDto.MinimumStock, Stock = 0, UnitOfMeasurement = productForCreationDto.UnitOfMeasurement, CreatedBy = userId }; product = await _productRepository.Add(product); //PurchasePrice var purchasePrice = new Price() { Value = productForCreationDto.PurchasePrice.Value, DateTime = DateTime.Now, ProductId = product.Id, PriceType = ePriceTypes.PurchasePrice }; await _priceRepository.Add(purchasePrice); //SalePrice var salePrice = new Price() { Value = productForCreationDto.SalePrice.Value, DateTime = DateTime.Now, ProductId = product.Id, PriceType = ePriceTypes.SalePrice }; await _priceRepository.Add(salePrice); await _productRepository.CommitAsync(); await _priceRepository.CommitAsync(); var productDto = _mapper.Map <Product, ProductDto>(product); productDto.PurchasePrice = _mapper.Map <Price, PriceDto>(purchasePrice); productDto.SalePrice = _mapper.Map <Price, PriceDto>(salePrice); return(productDto); }