public async Task <ActionResult> ProductEdit(ProductManageModel model) { if (!ModelState.IsValid) { return(View(model)); } if (model.Id == 0) { return(View(model)); } var product = ProductManager.Get(model.Id); if (product == null) { return(View(model)); } product.Title = model.Title; product.Description = model.Description; product.Cost = model.Cost; product.Size = model.Size; ProductManager.Save(product); if (!string.IsNullOrEmpty(model.File.FileName)) { var imageName = Guid.NewGuid().ToString("N") + "." + model.File.FileName.Split('.').Last(); ProductFileSave(imageName, model.File); } return(RedirectToAction("ProductList", "Employee")); }
public async Task <ResponseModel> CreateProductAsync(ProductManageModel productManageModel) { var product = await _productRepository.FetchFirstAsync(x => x.Name == productManageModel.Name && x.CategoryId == productManageModel.CategoryId); if (product != null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.BadRequest, Message = "This product has already existed. You can try again with the update!", }); } else { product = AutoMapper.Mapper.Map <Product>(productManageModel); await _productRepository.InsertAsync(product); return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.OK, Data = new ProductViewModel(product) }); } }
public async Task <ResponseModel> UpdateProductAsync(Guid id, ProductManageModel productManageModel) { var product = await GetAll().FirstOrDefaultAsync(x => x.Id == id); if (product == null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.NotFound, Message = MessageConstants.NOT_FOUND }); } else { var existedProduct = await _productResponsitory.FetchFirstAsync(x => x.Name == productManageModel.Name && x.Id != id); if (existedProduct != null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.BadRequest, Message = MessageConstants.EXISTED_CREATED }); } else { //update Product productManageModel.SetDataToModel(product); //update productInCategory await _productInCategoryReponsitory.DeleteAsync(product.ProductInCategories); var productInCategories = new List <ProductInCategory>(); foreach (var categoryId in productManageModel.CategoryIds) { var productInCategory = new ProductInCategory() { ProductId = product.Id, CategoryId = categoryId }; productInCategories.Add(productInCategory); } await _productInCategoryReponsitory.InsertAsync(productInCategories); await _productResponsitory.UpdateAsync(product); return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.OK, Data = new ProductViewModel(product), Message = MessageConstants.UPDATED_SUCCESSFULLY }); } } }
public async Task <IActionResult> Update(Guid id, [FromBody] ProductManageModel productManageModel) { var responseModel = await _productService.UpdateProductAsync(id, productManageModel); if (responseModel.StatusCode == System.Net.HttpStatusCode.OK) { return(Ok(responseModel.Data)); } return(BadRequest(responseModel.Message)); }
public async Task <IActionResult> Post([FromBody] ProductManageModel productManageModel) { var responseModel = await _productService.CreateProductAsync(productManageModel); if (responseModel.StatusCode == System.Net.HttpStatusCode.OK) { var product = (ProductViewModel)responseModel.Data; return(Ok(product)); } return(BadRequest(responseModel.Message)); }
public async Task <IActionResult> Post([FromBody] ProductManageModel productManagerModel) { var responseModel = await _productService.CreateProductAsync(productManagerModel); if (responseModel.StatusCode == System.Net.HttpStatusCode.OK) { return(Ok(responseModel)); } else { return(BadRequest(new { Message = responseModel.Message })); } }
public async Task <ResponseModel> CreateProductAsync(ProductManageModel productManageModel) { var product = await _productResponsitory.FetchFirstAsync(x => x.Name == productManageModel.Name); if (product != null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.BadRequest, Message = MessageConstants.EXISTED_CREATED }); } else { //create product product = AutoMapper.Mapper.Map <Product>(productManageModel); await _productResponsitory.InsertAsync(product); product = await GetAll().FirstOrDefaultAsync(x => x.Id == product.Id); //create productInCategory var productInCategories = new List <ProductInCategory>(); if (productManageModel.CategoryIds != null) { foreach (var categoryId in productManageModel.CategoryIds) { var productInCategory = new ProductInCategory(); productInCategory.ProductId = product.Id; productInCategory.CategoryId = categoryId; productInCategories.Add(productInCategory); } } await _productInCategoryReponsitory.InsertAsync(productInCategories); return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.OK, Data = new ProductViewModel(product), Message = MessageConstants.CREATED_SUCCESSFULLY }); } }
/// <summary> /// GET: Редактирует товар /// </summary> /// <param name="id">Идентификатор</param> /// <returns>Товар</returns> public async Task <ActionResult> ProductEdit(int id) { var product = ProductManager.Get(id); if (product == null) { return(RedirectToAction("ProductList", "Employee")); } var model = new ProductManageModel { Id = product.Id, Title = product.Title, Description = product.Description, Cost = product.Cost, Size = product.Size, }; return(View(model)); }
public async Task <ActionResult> ProductAdd(ProductManageModel model) { if (!ModelState.IsValid || string.IsNullOrEmpty(model.File.FileName)) { return(View(model)); } var imageName = Guid.NewGuid().ToString("N") + "." + model.File.FileName.Split('.').Last(); var product = new Product { Id = model.Id, Title = model.Title, Description = model.Description, Cost = model.Cost, Size = model.Size, ImageName = imageName, }; ProductManager.Save(product); ProductFileSave(imageName, model.File); return(RedirectToAction("ProductList", "Employee")); }
public async Task <Result <ProductManageModel> > UpdateProduct([FromBody] ProductManageModel model) { if (!Validate(model)) { return(null); } var product = await _mapper.FromModelAsync(model); var sUserId = _userManager.GetUserId(User); int userId; if (Int32.TryParse(sUserId, out userId)) { product.IdEditedBy = userId; } if (model.SourceId.HasValue) { if (product.Skus?.Count > 0) { foreach (var productSku in product.Skus) { productSku.Id = 0; } } } ProductContentTransferEntity transferEntity = new ProductContentTransferEntity(); ProductContent content = new ProductContent(); content.Id = model.Id; content.Url = model.Url; content.ContentItem = new ContentItem(); content.ContentItem.Template = model.Template ?? string.Empty; content.ContentItem.Description = model.Description ?? string.Empty; content.ContentItem.Title = model.MetaTitle; content.ContentItem.MetaDescription = model.MetaDescription; content.MasterContentItemId = model.MasterContentItemId; transferEntity.ProductContent = content; transferEntity.ProductDynamic = product; List <int> categoryIdsForResave = new List <int>(); if (model.Id > 0) { var dbItem = await productService.SelectAsync(model.Id); product = await productService.UpdateAsync(transferEntity); foreach (var productDynamicCategoryId in product.CategoryIds) { var add = true; foreach (var dbItemCategoryId in dbItem.CategoryIds) { if (productDynamicCategoryId == dbItemCategoryId) { add = false; break; } } if (add) { categoryIdsForResave.Add(productDynamicCategoryId); } } } else { transferEntity.ProductDynamic.PublicId = Guid.NewGuid(); product = await productService.InsertAsync(transferEntity); categoryIdsForResave = product.CategoryIds.ToList(); } ProductManageModel toReturn = await _mapper.ToModelAsync <ProductManageModel>(product); toReturn.MasterContentItemId = transferEntity.ProductContent.MasterContentItemId; //Set products orderring in categories where they were added if (categoryIdsForResave.Count > 0) { await productService.UpdateProductsOnCategoriesOrderAsync(categoryIdsForResave); } return(toReturn); }
public async Task <Result <ProductManageModel> > GetProduct(string id) { int idProduct = 0; if (id != null && !Int32.TryParse(id, out idProduct)) { throw new NotFoundException(); } if (idProduct == 0) { return(new ProductManageModel() { StatusCode = RecordStatusCode.Active, IdVisibility = CustomerTypeCode.All, CategoryIds = new List <int>(), SKUs = new List <SKUManageModel>(), CrossSellProducts = new List <CrossSellProductModel>() { new CrossSellProductModel() { IsDefault = true }, new CrossSellProductModel() { IsDefault = true }, new CrossSellProductModel() { IsDefault = true }, new CrossSellProductModel() { IsDefault = true }, }, Videos = new List <VideoModel>() { new VideoModel() { IsDefault = true }, new VideoModel() { IsDefault = true }, new VideoModel() { IsDefault = true }, new VideoModel() { IsDefault = true } }, }); } var item = await productService.SelectTransferAsync(idProduct); if (item == null || item.ProductDynamic == null) { throw new NotFoundException(); } ProductManageModel toReturn = await _mapper.ToModelAsync <ProductManageModel>(item?.ProductDynamic); if (item.ProductContent != null) { toReturn.Url = item.ProductContent.Url; toReturn.MasterContentItemId = item.ProductContent.MasterContentItemId; toReturn.Template = item.ProductContent.ContentItem.Template; toReturn.MetaTitle = item.ProductContent.ContentItem.Title; toReturn.MetaDescription = item.ProductContent.ContentItem.MetaDescription; } if (toReturn.CrossSellProducts != null) { foreach (var product in toReturn.CrossSellProducts) { product.IsDefault = String.IsNullOrEmpty(product.Image) && String.IsNullOrEmpty(product.Url); } } if (toReturn.Videos != null) { foreach (var video in toReturn.Videos) { video.IsDefault = String.IsNullOrEmpty(video.Image) && String.IsNullOrEmpty(video.Video) && String.IsNullOrEmpty(video.Text); } } return(toReturn); }
public async Task <ResponseModel> UpdateProductAsync(Guid id, ProductManageModel productManageModel) { var product = await GetAll().FirstOrDefaultAsync(x => x.Id == id); if (product != null) { var existedProductName = await _productRepository.FetchFirstAsync(x => x.Name == productManageModel.Name && x.Id != id); if (existedProductName != null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.BadRequest, Message = "Product " + productManageModel.Name + " has already existed. Please try another name!" }); } else { product.CategoryId = productManageModel.CategoryId; productManageModel.SetProductModel(product); if (product.ProductSizes != null && product.ProductSizes.Any()) { foreach (var productSizeManageModel in productManageModel.ProductSizes) { var productSize = await _productSizeRepository.GetAll().FirstOrDefaultAsync(x => (x.ProductId == id && x.SizeId == productSizeManageModel.SizeId)); if (productSize != null) { productSize.Price = productSizeManageModel.Price; await _productSizeRepository.UpdateAsync(productSize); } else { productSize = new ProductSize() { ProductId = id, SizeId = productSizeManageModel.SizeId, Price = productSizeManageModel.Price }; await _productSizeRepository.InsertAsync(productSize); } } } else { product.ProductSizes = new List <ProductSize>(); foreach (var productSize in productManageModel.ProductSizes) { product.ProductSizes.Add(new ProductSize() { ProductId = product.Id, SizeId = productSize.SizeId, Price = productSize.Price }); } } await _productRepository.UpdateAsync(product); return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.OK, Data = new ProductViewModel(product) }); } } else { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.BadRequest, Message = "This product has not existed. Please try again!", }); } }