public IActionResult Delete(int id) { var Category = _service.GetSingleById(id); if (Category == null) { return(NotFound()); } if (Category.Archive == true) { return(NotFound()); } Category.Archive = true; _service.Update(Category); var list_category = _service_pro_cat.GetMulti(c => c.CategoryID == id); foreach (var item in list_category) { item.Archive = true; item.Is_Active = false; _service_pro_cat.Update(item); } return(Ok(Category)); }
public IActionResult Update(int id, [FromBody] JObject data) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var Product = _service.GetSingleById(id); if (Product == null) { return(NotFound()); } Product model = data["product"].ToObject <Product>(); var _listImg = data["images"].ToList(); var _listCat = data["categories"].ToList(); for (int i = 0; i < _listImg.Count; i++) { Product_Image toCreate = new Product_Image() { ImageID = (int)_listImg[i], ProductID = id, }; _service_img.Add(toCreate); } //Check lai danh muc xem co cai nao bi xoa k - co thi delete no ra. foreach (var item in _service_img.GetMulti(c => c.ProductID == id)) { bool flag = true; for (int i = 0; i < _listImg.Count; i++) { if (item.ImageID == (int)_listImg[i]) { flag = false; break; } } if (flag) { _service_img.Delete(item); } } //Add lai tat ca cac danh muc for (int i = 0; i < _listCat.Count; i++) { Product_Category toCreate = new Product_Category() { CategoryID = (int)_listCat[i], ProductID = id, }; _service_cat.Add(toCreate); Category toUpdate = _catrepository.GetSingleById(toCreate.CategoryID); toUpdate.Quantity++; _catrepository.Update(toUpdate); } //Check lai danh muc xem co cai nao bi xoa k - co thi delete no ra. foreach (var item in _service_cat.GetMulti(c => c.ProductID == id)) { bool flag = true; for (int i = 0; i < _listCat.Count; i++) { if (item.CategoryID == (int)_listCat[i]) { flag = false; break; } } if (flag) { _service_cat.Delete(item); Category toUpdate = _catrepository.GetSingleById(item.CategoryID); toUpdate.Quantity--; _catrepository.Update(toUpdate); } } _service.Update(model); return(Ok(model)); }