public JsonResult Remove(int id) { var result = new JsonOperationResponse(); try { using (var db = new KbVaultContext()) { var currentUserId = KBVaultHelperFunctions.UserAsKbUser(User).Id; var queryParams = new SqlParameter[] { new SqlParameter("ArticleId", id) }; db.Database.ExecuteSqlCommand("Delete from ArticleTag Where ArticleId = @ArticleId", queryParams); var article = db.Articles.Single(a => a.Id == id); if (article == null) { throw new Exception(ErrorMessages.ArticleNotFound); } while (article.Attachments.Count > 0) { var a = article.Attachments.First(); KbVaultAttachmentHelper.RemoveLocalAttachmentFile(a); KbVaultLuceneHelper.RemoveAttachmentFromIndex(a); article.Attachments.Remove(a); /* * Also remove the attachment from db.attachments collection * * http://stackoverflow.com/questions/17723626/entity-framework-remove-vs-deleteobject * * If the relationship is required (the FK doesn't allow NULL values) and the relationship is not * identifying (which means that the foreign key is not part of the child's (composite) primary key) * you have to either add the child to another parent or you have to explicitly delete the child * (with DeleteObject then). If you don't do any of these a referential constraint is * violated and EF will throw an exception when you call SaveChanges - * the infamous "The relationship could not be changed because one or more of the foreign-key properties * is non-nullable" exception or similar. */ db.Attachments.Remove(a); } article.Author = currentUserId; KbVaultLuceneHelper.RemoveArticleFromIndex(article); db.Articles.Remove(article); db.SaveChanges(); result.Data = id; result.Successful = true; return(Json(result)); } } catch (Exception ex) { Log.Error(ex); result.Successful = false; result.ErrorMessage = ex.Message; return(Json(result)); } }
public ActionResult Edit([Bind(Exclude = "Category.Name,Category.SefName")] ArticleViewModel model) { try { ModelState.Remove("Category.Name"); ModelState.Remove("Category.SefName"); if (ModelState.IsValid) { if (model.PublishEndDate < model.PublishStartDate) { ModelState.AddModelError("PublishDate", ErrorMessages.PublishEndDateMustBeGreater); } else { var article = ArticleRepository.Get(model.Id); article.CategoryId = model.Category.Id; article.IsDraft = model.IsDraft ? 1 : 0; article.PublishEndDate = model.PublishEndDate; article.PublishStartDate = model.PublishStartDate; article.Edited = DateTime.Now; article.Title = model.Title; article.Content = model.Content; article.Author = KBVaultHelperFunctions.UserAsKbUser(User).Id; article.SefName = model.SefName; ArticleRepository.Update(article, model.Tags); if (article.IsDraft == 0) { KbVaultLuceneHelper.AddArticleToIndex(article); } else { KbVaultLuceneHelper.RemoveArticleFromIndex(article); } ShowOperationMessage(UIResources.ArticleCreatePageEditSuccessMessage); } } } catch (Exception ex) { Log.Error(ex); ModelState.AddModelError("Exception", ex.Message); } return(View("Create", model)); }
public async Task <IActionResult> Edit(long id, [Bind("Id,Title,Content,IsDraft,PublishStartDate,PublishEndDate,SefName,Category,Tags")] CreateArticleViewModel model) { if (id != model.Id) { return(NotFound()); } try { // ModelState.Remove("Category.Name"); // ModelState.Remove("Category.SefName"); if (ModelState.IsValid) { string currentUser = _httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); if (model.PublishEndDate < model.PublishStartDate) { //vrati se ModelState.AddModelError("PublishDate", ErrorMessages.PublishEndDateMustBeGreater); } else { var article = _articleRepository.Get(model.Id); article.Category = _context.Categories.Find(model.Category); article.IsDraft = model.IsDraft ? 1 : 0; article.PublishEndDate = model.PublishEndDate; article.PublishStartDate = model.PublishStartDate; article.Edited = DateTime.Now; article.Title = model.Title; article.Content = model.Content; article.Author = await _userRepository.Get(currentUser); article.SefName = model.SefName; //_articleRepository.Update(article, model.Tags); _articleRepository.Update(article, model.Tags, currentUser); if (article.IsDraft == 0) { _lucene.AddArticleToIndex(article); } else { _lucene.RemoveArticleFromIndex(article); } //vrati se //ShowOperationMessage(UIResources.ArticleCreatePageEditSuccessMessage); var articleTags = _articleRepository.GetArticleTagsForArticleId((long)id); var tags = articleTags.Select(at => at.Tag.Name).ToList(); ViewData["Tags"] = new SelectList(tags, "Id"); return(View("Details", article)); } } } catch (Exception ex) { log.Error(ex); ModelState.AddModelError("Exception", ex.Message); } return(RedirectToAction(nameof(Index))); }