public async Task <IActionResult> Edit(int id, [Bind("Id,CategoryId,ProductId")] ProductInCategory productInCategory) { if (id != productInCategory.Id) { return(NotFound()); } if (ModelState.IsValid) { _uow.ProductsInCategories.Update(productInCategory); await _uow.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } var viewModel = new ProductInCategoryViewModel { ProductInCategory = productInCategory, Categories = new SelectList(await _uow.Categories.AllAsync(), nameof(Category.Id), nameof(Category.CategoryAndOwnerName)), Products = new SelectList(await _uow.Products.AllAsync(), nameof(Product.Id), nameof(Product.ProductAndOwnerName)) }; return(View(viewModel)); }
public async Task <ApiResult <bool> > CategoryAssisgn(int productId, CategoryAssignRequest request) { var product = await _context.Products.FindAsync(productId); if (product == null) { throw new eShopException($"Cannot find a product: {productId}"); } foreach (var category in request.Categories) { var productInCategory = await _context.ProductInCategories.FirstOrDefaultAsync(x => x.ProductId == productId && x.CategoryId == int.Parse(category.Id)); if (productInCategory != null && category.Selected == false) //Kiem tra nếu product đang được gán category, category ko duoc chon { _context.ProductInCategories.Remove(productInCategory); // thì bỏ gán } else if (productInCategory == null && category.Selected == true) //Kiem tra nếu product đang chưa được gán category, { productInCategory = new ProductInCategory() { ProductId = productId, CategoryId = int.Parse(category.Id) }; _context.ProductInCategories.Add(productInCategory); //thì gán vào } } await _context.SaveChangesAsync(); return(new ApiResultSuccess <bool>()); }
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 <int> AddProductInCategory(int productId, int categoryId) { var productInCategory = new ProductInCategory { CategoryId = categoryId, ProductId = productId }; await _context.ProductInCategories.AddAsync(productInCategory); return(await _context.SaveChangesAsync()); }
//Tạo danh mục chưa sản phẩm public async Task <int> CreateInCategory(int productId, int categoriesId) { var categoriesInproduct = new ProductInCategory { ProductId = productId, CategoryId = categoriesId }; _context.ProductInCategories.Add(categoriesInproduct); return(await _context.SaveChangesAsync()); }
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 }); } }