public JsonResult Update(int keywordID, string products) { var jsonSuccessResult = new JsonSuccessResult(); try { string[] arrProducts = products.Split(','); var curList = ProductKeywords.GetByKeywordID(keywordID); foreach (var product in arrProducts) { int productID = Int32.Parse(product); if (!curList.Any(item => item.ProductID == productID && item.KeywordID == keywordID)) { var kp = new ProductKeyword { ProductID = productID, KeywordID = keywordID, LastUpdate = DateTime.Now }; ProductKeywords.Insert(kp); } else { curList.Remove(curList.Single(cls => cls.KeywordID == keywordID && cls.ProductID == productID)); } } foreach (var item in curList) { ProductKeywords.Delete(item.ID); } jsonSuccessResult.Success = true; } catch (Exception ex) { jsonSuccessResult.Errors = new string[] { ex.Message }; jsonSuccessResult.Success = false; } return(new JsonResult() { Data = jsonSuccessResult }); }
public async Task <ActionResult> DeleteKeywords([FromQuery] int[] ids) { foreach (int id in ids) { ProductKeyword keyword = await unitOfWork.ProductKeywords.Get(id); unitOfWork.ProductKeywords.Remove(keyword); } await unitOfWork.Save(); return(Ok()); }
public async Task <ActionResult> AddKeyword(ProductItem keyword) { ProductKeyword newKeyword = new ProductKeyword { ProductId = keyword.ProductId, KeywordId = keyword.ItemId }; // Add and save unitOfWork.ProductKeywords.Add(newKeyword); await unitOfWork.Save(); return(Ok(newKeyword.Id)); }
private static void SaveDefaultKeywords(string[] keywords, int productID) { List <ProductKeyword> keys = new List <ProductKeyword>(); foreach (var key in keywords) { if (key != null && key != String.Empty) { var orgKey = Keywords.GetByTitle(key); if (orgKey != null) { if (orgKey.IsActive) { var pk = new ProductKeyword { KeywordID = orgKey.ID, ProductID = productID, LastUpdate = DateTime.Now }; keys.Add(pk); } } else { int keyID = InsertKeyword(key); var pk = new ProductKeyword { KeywordID = keyID, ProductID = productID, LastUpdate = DateTime.Now }; keys.Add(pk); } } } if (keys.Count > 0) { ProductKeywords.Insert(keys); } }