public async Task <long> AddAsync(long productId, List <string> imageList) { using (MyDbContext dbc = new MyDbContext()) { await dbc.GetAll <ProductImageEntity>().Where(a => a.ProductId == productId).ForEachAsync(a => dbc.ProductImages.Remove(a)); await dbc.SaveChangesAsync(); foreach (var item in imageList) { ProductImageEntity entity = new ProductImageEntity(); entity.ProductId = productId; entity.ImgSrc = item; dbc.ProductImages.Add(entity); if (item == imageList.First()) { ProductEntity product = await dbc.GetAll <ProductEntity>().SingleOrDefaultAsync(p => p.Id == productId); if (product == null) { return(-1); } product.FirstImage = item; } } await dbc.SaveChangesAsync(); return(1); } }
public async Task <ProductModel> AddImage(int id, IFormFile image) { var product = await _db.Products .Include(v => v.Images) .FirstOrDefaultAsync(v => v.Id == id); if (product == null) { throw new Exception("404"); } var entity = new ProductImageEntity() { ProductId = product.Id }; var img = await _fileService.UploadFile(image); entity.Link = img.Link; entity.Name = img.Name; product.Images.Add(entity); _db.Products.Update(product); await _db.SaveChangesAsync(); return(new ProductModel(product)); }
public ProductImageDTO ToDTO(ProductImageEntity entity) { ProductImageDTO dto = new ProductImageDTO(); dto.Id = entity.Id; dto.ImgSrc = entity.ImgSrc; dto.ProductId = entity.ProductId; return(dto); }