public async Task <ApiResponse <string> > Update(int trademarkId, TrademarkRequest request) { var trademarkFromDb = await _db.Trademarks.FindAsync(trademarkId); if (trademarkFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(trademarkId))); } if (string.IsNullOrEmpty(request.Name)) { return(new ApiErrorResponse <string>(ConstantStrings.emptyNameFieldError)); } trademarkFromDb.Name = request.Name; trademarkFromDb.Status = request.Status; trademarkFromDb.Description = request.Description; if (request.Image != null) { var result = await DeleteImage(trademarkFromDb.PublicId); if (result.Error != null) { return(new ApiErrorResponse <string>(ConstantStrings.cloudDeleteFailed)); } var uploadResult = await UploadImage(request.Image); trademarkFromDb.Image = uploadResult.SecureUrl.ToString(); trademarkFromDb.PublicId = uploadResult.PublicId; } await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.editSuccessfully)); }
public async Task <ApiResponse <Trademark> > GetById(int trademarkId) { var trademarkFromDb = await _db.Trademarks.FindAsync(trademarkId); if (trademarkFromDb == null) { return(new ApiErrorResponse <Trademark>(ConstantStrings.FindByIdError(trademarkId))); } return(new ApiSuccessResponse <Trademark>(trademarkFromDb)); }
public async Task <ApiResponse <Demand> > GetById(int demandId) { var demandFromDb = await _db.Demands.FindAsync(demandId); if (demandFromDb == null) { return(new ApiErrorResponse <Demand>(ConstantStrings.FindByIdError(demandId))); } return(new ApiSuccessResponse <Demand>(demandFromDb)); }
public async Task <ApiResponse <Vendor> > GetById(int vendorId) { var vendorFromDb = await _db.Vendors.FindAsync(vendorId); if (vendorFromDb == null) { return(new ApiErrorResponse <Vendor>(ConstantStrings.FindByIdError(vendorId))); } return(new ApiSuccessResponse <Vendor>(vendorFromDb)); }
public async Task <ApiResponse <Category> > GetById(int categoryId) { var categoryFromDb = await _db.Categories.FindAsync(categoryId); if (categoryFromDb == null) { return(new ApiErrorResponse <Category>(ConstantStrings.FindByIdError(categoryId))); } return(new ApiSuccessResponse <Category>(categoryFromDb)); }
public async Task <ApiResponse <Condition> > GetById(int conditionId) { var conditionFromDb = await _db.Conditions.FindAsync(conditionId); if (conditionFromDb == null) { return(new ApiErrorResponse <Condition>(ConstantStrings.FindByIdError(conditionId))); } return(new ApiSuccessResponse <Condition>(conditionFromDb)); }
public async Task <ApiResponse <string> > Delete(int demandId) { var demandFromDb = await _db.Demands.FindAsync(demandId); if (demandFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(demandId))); } _db.Demands.Remove(demandFromDb); await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.deleteSuccessfully)); }
public async Task <ApiResponse <string> > Delete(int productId) { var product = await _db.Products.FindAsync(productId); if (product == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(productId))); } _db.Products.Remove(product); await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.deleteSuccessfully)); }
public async Task <ApiResponse <string> > Delete(int conditionId) { var conditionFromDb = await _db.Conditions.FindAsync(conditionId); if (conditionFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(conditionId))); } _db.Conditions.Remove(conditionFromDb); await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.deleteSuccessfully)); }
public async Task <ApiResponse <string> > Delete(int vendorId) { var vendorFromDb = await _db.Vendors.FindAsync(vendorId); if (vendorFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(vendorId))); } _db.Vendors.Remove(vendorFromDb); await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.deleteSuccessfully)); }
public async Task <ApiResponse <Product> > GetById(int productId) { var product = await _db.Products.AsNoTracking().Include(x => x.Category).Include(x => x.Vendor) .Include(x => x.Trademark).Include(x => x.Demand) .Include(x => x.Condition).Include(x => x.ProductImages) .FirstOrDefaultAsync(x => x.Id == productId); if (product == null) { return(new ApiErrorResponse <Product>(ConstantStrings.FindByIdError(productId))); } return(new ApiSuccessResponse <Product>(product)); }
public async Task <ApiResponse <string> > Update(int productId, ProductImageRequest request) { var productFromDb = await _db.Products.FindAsync(productId); if (productFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(productId))); } var productImageFromDb = await _db.ProductImages.Where(x => x.ProductId == productId).ToListAsync(); if (productImageFromDb.Count == 0) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(productId))); } if (request.ProductImages == null) { return(new ApiErrorResponse <string>(ConstantStrings.imgIsEmptyOrNull)); } var images = request.ProductImages.ToList(); for (int i = 0; i < images.Count; i++) { foreach (var item in productImageFromDb) { if (images[i].Name == item.Caption) { var deleteResult = await DeleteImage(item.PublicId); if (deleteResult.Error != null) { return(new ApiErrorResponse <string>(deleteResult.Error.ToString())); } var uploadResult = await UploadImage(images[i]); if (uploadResult.Error != null) { return(new ApiErrorResponse <string>(uploadResult.Error.ToString())); } item.ImageUrl = uploadResult.SecureUrl.ToString(); item.PublicId = uploadResult.PublicId; break; } } } await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.editSuccessfully)); }
public async Task <ApiResponse <string> > Update(int vendorId, VendorRequest request) { var vendorFromDb = await _db.Vendors.FindAsync(vendorId); if (vendorFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(vendorId))); } if (string.IsNullOrEmpty(request.Name)) { return(new ApiErrorResponse <string>(ConstantStrings.emptyNameFieldError)); } _mapper.Map <VendorRequest, Vendor>(request, vendorFromDb); await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.editSuccessfully)); }
public async Task <ApiResponse <string> > Update(int productId, ProductRequest request) { var productFromDb = await _db.Products.FindAsync(productId); if (productFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(productId))); } if (string.IsNullOrEmpty(request.Name)) { return(new ApiErrorResponse <string>(ConstantStrings.emptyNameFieldError)); } _mapper.Map <ProductRequest, Product>(request, productFromDb); await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.editSuccessfully)); }
public async Task <ApiResponse <string> > Update(int demandId, DemandRequest request) { var demandFromDb = await _db.Demands.FindAsync(demandId); if (demandFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(demandId))); } if (string.IsNullOrEmpty(request.Name)) { return(new ApiErrorResponse <string>(ConstantStrings.emptyNameFieldError)); } demandFromDb.Name = request.Name; _db.Demands.Update(demandFromDb); await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.editSuccessfully)); }
public async Task <ApiResponse <string> > Create(int productId, ProductImageRequest request) { var productFromDb = await _db.Products.FindAsync(productId); if (productFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(productId))); } var productImageFromDb = await _db.ProductImages.Where(x => x.ProductId == productFromDb.Id) .OrderByDescending(x => x.SortOrder).ToListAsync(); if (productFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(productId))); } if (request.ProductImages == null) { return(new ApiErrorResponse <string>(ConstantStrings.imgIsEmptyOrNull)); } var images = request.ProductImages.ToList(); for (int i = 0; i < images.Count; i++) { var uploadResult = await UploadImage(images[i]); if (uploadResult.Error != null) { return(new ApiErrorResponse <string>(uploadResult.Error.ToString())); } var productImage = new ProductImage() { ImageUrl = uploadResult.SecureUrl.ToString(), PublicId = uploadResult.PublicId, Caption = SystemFunctions.ProductImageCaption(productFromDb.Name, productImageFromDb.FirstOrDefault().SortOrder + i + 1), ProductId = productFromDb.Id, SortOrder = productImageFromDb.FirstOrDefault().SortOrder + i + 1 }; _db.ProductImages.Add(productImage); } _db.ProductImages.Remove(productImageFromDb.Where(x => x.PublicId == ConstantStrings.blankProductImagePublicId).FirstOrDefault()); await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.addSuccessfully)); }
public async Task <ApiResponse <string> > Update(int categoryId, CategoryRequest request) { if (string.IsNullOrEmpty(request.Name)) { return(new ApiErrorResponse <string>(ConstantStrings.emptyNameFieldError)); } var categoryFromDb = await _db.Categories.FindAsync(categoryId); if (categoryFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(categoryId))); } categoryFromDb.Name = request.Name; categoryFromDb.Description = request.Description; _db.Categories.Update(categoryFromDb); await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.editSuccessfully)); }
public async Task <ApiResponse <string> > Delete(int trademarkId) { var trademarkFromDb = await _db.Trademarks.FindAsync(trademarkId); if (trademarkFromDb == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(trademarkId))); } var result = await DeleteImage(trademarkFromDb.PublicId); if (result.Error != null) { return(new ApiErrorResponse <string>(ConstantStrings.cloudDeleteFailed)); } _db.Trademarks.Remove(trademarkFromDb); await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.deleteSuccessfully)); }
public async Task <ApiResponse <string> > Delete(int productImageId) { var productImage = await _db.ProductImages.FindAsync(productImageId); if (productImage == null) { return(new ApiErrorResponse <string>(ConstantStrings.FindByIdError(productImageId))); } var deleteResult = await DeleteImage(productImage.PublicId); if (deleteResult.Error != null) { return(new ApiErrorResponse <string>(deleteResult.Error.ToString())); } _db.ProductImages.Remove(productImage); await _db.SaveChangesAsync(); return(new ApiSuccessResponse <string>(ConstantStrings.deleteSuccessfully)); }