public async Task <ApiResult <bool> > Create(CategoryCreateRequest request) { var category = new Category() { IsShowOnHome = request.IsShowOnHome, Created_At = DateTime.Now, CategoryTranslations = new List <CategoryTranslation>() { new CategoryTranslation() { Name = request.Name, CategoryUrl = GetUrlByName.Converts(request.Name), LanguageId = request.LanguageId } }, }; //Save Image if (request.ThumbnailImage != null) { category.ImagePath = await this.SaveFile(request.ThumbnailImage); } _context.Categories.Add(category); return(await SaveChangeService.SaveChangeAsyncNotImage(_context)); }
public async Task <ApiResult <bool> > Update(CategoryUpdateRequest request, int categoryId) { var category = await _context.Categories.FindAsync(categoryId); var categoryTranslation = await _context.CategoryTranslations.FirstOrDefaultAsync(x => x.CategoryId == categoryId && x.LanguageId == request.LanguageId); if (category == null || categoryTranslation == null) { return(new ApiResultErrors <bool>($"Cannot find a category with id: {categoryId}")); } categoryTranslation.Name = request.Name; categoryTranslation.CategoryUrl = GetUrlByName.Converts(request.Name); category.IsShowOnHome = request.IsShowOnHome; //Save Image if (request.ThumbnailImage != null) { var OldImagePath = category.ImagePath; category.ImagePath = await this.SaveFile(request.ThumbnailImage); if (OldImagePath != null) { await _storageService.DeleteFileAsync(OldImagePath); } } return(await SaveChangeService.SaveChangeAsyncNotImage(_context)); }
public async Task <ApiResult <bool> > Update(ProductUpdateRequest request) { var product = await _context.Products.FindAsync(request.Id); var productTranslation = await _context.ProductTranslations.FirstOrDefaultAsync(x => x.ProductId == request.Id && x.LanguageId == request.LanguageId); if (product == null || productTranslation == null) { return(new ApiResultErrors <bool>($"Cannot find a product with id: {request.Id}")); } productTranslation.Name = request.Name; productTranslation.ProductUrl = GetUrlByName.Converts(request.Name); productTranslation.Description = request.Description; //Save Image if (request.ThumbnailImage != null) { var thumbnailImage = await _context.ProductImages.FirstOrDefaultAsync(i => i.IsDefault == true && i.ProductId == request.Id); if (thumbnailImage != null) { var OldImagePath = thumbnailImage.ImagePath; thumbnailImage.FileSize = request.ThumbnailImage.Length; thumbnailImage.ImagePath = await this.SaveFile(request.ThumbnailImage); if (OldImagePath != null) { await _storageService.DeleteFileAsync(OldImagePath); } _context.ProductImages.Update(thumbnailImage); } else { var image = new ProductImage() { ProductId = request.Id, IsDefault = false, Caption = "ThumbanailImage", }; image.FileSize = request.ThumbnailImage.Length; image.ImagePath = await this.SaveFile(request.ThumbnailImage); _context.ProductImages.Add(image); } } return(await SaveChangeService.SaveChangeAsyncNotImage(_context)); }
public async Task <ApiResult <bool> > Create(ProductCreateRequest request) { var product = new Product() { Price = request.Price, OriginalPrice = request.OriginalPrice, CategoryId = request.CategoryId, Stock = request.Stock, Created_At = DateTime.Now, ProductTranslations = new List <ProductTranslation>() { new ProductTranslation() { Name = request.Name, Description = request.Description, ProductUrl = GetUrlByName.Converts(request.Name), LanguageId = request.LanguageId, } }, }; //Save Image if (request.ThumbnailImage != null) { product.ProductImages = new List <ProductImage>() { new ProductImage() { Caption = "Thumbnail image", FileSize = request.ThumbnailImage.Length, ImagePath = await this.SaveFile(request.ThumbnailImage), IsDefault = true, } }; } _context.Products.Add(product); return(await SaveChangeService.SaveChangeAsyncNotImage(_context)); }