public async Task <int> SaveDataAsync(ProductAdderVM viewModel) { var adder = _mapper.Map <ProductAdder>(viewModel); adder.PurchaseDate = DateTime.Now; var product = await _productRepository.GetProductByAdderAsync(adder, EntityExtensions.ConvertToUnSign(adder.Name)); if (product != null) { product.AvailableQuantity += adder.AvailableQuantity; await _productAdderRepository.AddAsync(adder); await _productRepository.UpdateAsync(product); return(await _unitOfWork.SaveAsync()); } return(0); }
public async Task <ActionResult> Detail(DiscountVM viewModel, HttpPostedFileBase file) { if (ModelState.IsValid) { viewModel.DiscountNameAlias = EntityExtensions.ConvertToUnSign(viewModel.DiscountName); if (file != null && file.ContentLength > 0) { string pic = "discount_" + (int)viewModel.DiscountType + "_" + viewModel.DiscountNameAlias + Path.GetExtension(file.FileName); string path = Path.Combine(Server.MapPath("/img/"), pic); viewModel.DiscountCover = EntityExtensions.SaveImage(file, pic, path); } if (await _adminDiscountService.SaveDataAsync(viewModel) > 0) { return(RedirectToAction("Index", new { status = StatusMessageId.UpdateSuccess })); } } ModelState.AddModelError("Error", "Cannot save your product"); return(View()); }
public async Task <ActionResult> Detail(ProductVM product, HttpPostedFileBase file) { if (ModelState.IsValid) { product.NameAlias = EntityExtensions.ConvertToUnSign(product.Name); if (file != null && file.ContentLength > 0) { string pic = "book_" + product.CategoryId + "_" + product.NameAlias + Path.GetExtension(file.FileName); string path = Path.Combine(Server.MapPath("/img/"), pic); product.Image = EntityExtensions.SaveImage(file, pic, path); } if (await _productService.SaveDataAsync(product) > 0) { if (product.ProductId == 0) { return(RedirectToAction("Index", new { status = StatusMessageId.AddSuccess })); } return(RedirectToAction("Index", new { status = StatusMessageId.UpdateSuccess })); } } ModelState.AddModelError("Error", "Cannot save your product"); return(View()); }
public async Task <int> SaveDataAsync(CategoryVM viewModel) { var entity = _mapper.Map <CategoryVM, Category>(viewModel); entity.NameAlias = EntityExtensions.ConvertToUnSign(viewModel.CategoryName); if (viewModel.CategoryId == 0) { entity.CreateBy = "admin"; entity.UpdateBy = "admin"; entity.CreateDate = DateTime.Now; entity.UpdatedDate = DateTime.Now; entity.UniqueStringKey = Guid.NewGuid(); await _categoryRepository.AddAsync(entity); } else { entity.UpdatedDate = DateTime.Now; await _categoryRepository.UpdateAsync(entity); } return(await _unitOfWork.SaveAsync()); }
public async Task <IndexViewModel <ProductVM> > GetAllProducts(string categoryId = null, string search = null, int pageSize = 16, int page = 1) { var data = _mapper.Map <IEnumerable <Product>, IEnumerable <ProductVM> >(await _productRepository.GetPagingAsync(page, pageSize, new string[2] { "Category", "Discount" })); var pager = new Pager(_productRepository.GetTotalRow(), page, pageSize); if (!string.IsNullOrEmpty(search)) { categoryId = string.Empty; data = _mapper.Map <IEnumerable <Product>, IEnumerable <ProductVM> >(await _productRepository.GetBySearchPagingAsync(EntityExtensions.ConvertToUnSign(search), page, pageSize)); pager = new Pager(data.Count(), page); } if (!string.IsNullOrEmpty(categoryId)) { search = string.Empty; data = _mapper.Map <IEnumerable <Product>, IEnumerable <ProductVM> >(await _productRepository.GetByCategoryPagingAsync(categoryId, page, pageSize)); pager = new Pager(data.Count(), page); } return(new IndexViewModel <ProductVM>() { Items = data.Where(m => m.Status == true), Pager = pager }); }
public async Task <IndexViewModel <ProductAdderVM> > GetDataAsync(int page, int pageSize, int?categoryId, string search = null) { var data = _mapper.Map <IEnumerable <ProductAdderVM> >(await _productAdderRepository.GetAllAsync()); var pager = new Pager(_productAdderRepository.GetTotalRow(), page); if (!string.IsNullOrEmpty(search)) { data = _mapper.Map <IEnumerable <ProductAdderVM> >(await _productAdderRepository.GetBySearchAsync(EntityExtensions.ConvertToUnSign(search))); pager = new Pager(data.Count(), page); } if (categoryId.HasValue && categoryId > 0) { data = _mapper.Map <IEnumerable <ProductAdderVM> >(await _productAdderRepository.GetByCategoryAsync(categoryId.Value)); pager = new Pager(data.Count(), page); } return(new IndexViewModel <ProductAdderVM>() { Items = data.Skip((pager.CurrentPage - 1) * pager.PageSize).Take(pager.PageSize), Pager = pager }); }