public async Task <ActionResult> CreateSpecificProduct(int id, [FromForm] CreateSpecificProductDTO createSpecificProductDTO) { if (createSpecificProductDTO.sizeIds == null) { return(BadRequest(new { SizeIds = "Invalid sizeIds" })); } for (var i = 0; i < createSpecificProductDTO.sizeIds.Count(); i += 1) { var sizeId = createSpecificProductDTO.sizeIds[i]; string specificProductId = $"P{id}C{createSpecificProductDTO.ColorId}S{sizeId}".ToString(); // Check exist product var existingProduct = await _productService.FindByIdAsync(id); if (existingProduct == null) { return(BadRequest(new { ProductId = $"Product {id} not found".ToString() })); } // Check exist specific product var existingSpecificProduct = await _productService.FindSpecificProductByIdAsync(specificProductId); if (existingSpecificProduct != null) { return(BadRequest(new { ColorId = "Specific product with this color and this size allready exist(choose another color or size)", SizeId = "Specific product with this color and this size allready exist(choose another color or size)" })); } // Check is valid color var validColor = await _colorService.FindByIdAsync(createSpecificProductDTO.ColorId); if (validColor == null) { return(BadRequest(new { ColorId = "Color not found" })); } // Check is valid size var validSize = await _sizeService.FindByIdAsync(sizeId); if (validSize == null) { return(BadRequest(new { SizeId = $"Size ID {sizeId} not found".ToString() })); } } await _productService.CreateSpecificProduct(id, createSpecificProductDTO); return(NoContent()); }
public async Task CreateSpecificProduct(int productId, CreateSpecificProductDTO createSpecificProductDTO) { List <IFormFile> images = createSpecificProductDTO.Images; List <Image> imagesToAdd = new List <Image>(); List <SpecificProduct> specificProductsToAdd = new List <SpecificProduct>(); var product = await FindByIdAsync(productId); // Create directory if not Exists if (!Directory.Exists(Folder.RootImagePath)) { Directory.CreateDirectory(Folder.RootImagePath); } for (var i = 0; i < createSpecificProductDTO.sizeIds.Count(); i += 1) { var sizeId = createSpecificProductDTO.sizeIds[i]; string specificProductId = $"P{productId}C{createSpecificProductDTO.ColorId}S{sizeId}".ToString(); var specificProduct = new SpecificProduct { Id = specificProductId, ProductId = productId, ColorId = createSpecificProductDTO.ColorId, SizeId = sizeId }; await _bus.Publish(new CreateSpecificProductMessage { Id = specificProductId, Price = product.Price }); specificProductsToAdd.Add(specificProduct); } // Save specific product await _context.SpecificProducts.AddRangeAsync(specificProductsToAdd); await _context.SaveChangesAsync(); // Ko lưu image vô server nếu tạo specific product xảy ra lỗi. // Save db thành công thì lưu image vô server. for (int i = 0; i < images.Count(); i++) { var image = images[i]; string fileName = Path.GetRandomFileName() + image.FileName; string publicPath = Path.Combine("/images", fileName); string filePath = Path.Combine(Folder.RootImagePath, fileName); using (var stream = System.IO.File.Create(filePath)) { await image.CopyToAsync(stream); } await _context.Images.AddAsync( new Image { IsDefault = i == 0, ProductId = productId, Url = publicPath, ColorId = createSpecificProductDTO.ColorId } ); } // Save images // await _context.Images.AddRangeAsync(imagesToAdd); await _context.SaveChangesAsync(); }