public void Update(Article article, string tags) { using (var db = new KbVaultContext()) { db.Articles.AddOrUpdate(article); db.SaveChanges(); if (!String.IsNullOrEmpty(tags)) { AssignTagsToArticle(article.Id, tags); } db.SaveChanges(); } }
public long Add(Article article, string tags) { using (var db = new KbVaultContext()) { db.Articles.Add(article); db.SaveChanges(); if (!string.IsNullOrEmpty(tags)) { AssignTagsToArticle(article.Id, tags); } db.SaveChanges(); return(article.Id); } }
public ActionResult Detail(string id) { try { using (var db = new KbVaultContext()) { Article article = db.PublishedArticles().FirstOrDefault(a => a.SefName == id); if (article != null) { article.Views++; db.SaveChanges(); ViewBag.SimilarArticles = ArticleRepository.GetVisibleSimilarArticles((int)article.Id, DateTime.Today.Date); return(View(article)); } else { return(View("ArticleNotFound")); } } } catch (Exception ex) { Log.Error(ex); return(RedirectToAction("PublicError", "Error")); } }
public JsonResult Remove(int id = -1) { var result = new JsonOperationResponse { Successful = false }; try { using (var db = new KbVaultContext()) { db.KbUsers.Remove(db.KbUsers.First(u => u.Id == id)); db.SaveChanges(); result.Successful = true; } return(Json(result)); } catch (Exception ex) { Log.Error(ex); result.ErrorMessage = ex.Message; return(Json(result)); } }
public static void RemoveAttachment(string hash, long currentUserId) { try { using (var db = new KbVaultContext()) { var attachment = db.Attachments.First(a => a.Hash == hash); if (attachment == null) { throw new ArgumentNullException(ErrorMessages.AttachmentNotFound); } var localPath = Path.Combine(HttpContext.Current.Server.MapPath(attachment.Path), attachment.FileName); attachment.Author = currentUserId; db.Attachments.Remove(attachment); db.SaveChanges(); File.Delete(localPath); } } catch (Exception ex) { Log.Error(ex); throw; } }
public ActionResult Create([Bind(Exclude = "Id")] KbUserViewModel model) { try { if (ModelState.IsValid) { using (var db = new KbVaultContext()) { KbUser usr = KbVaultAuthHelper.CreateUser(model.UserName, model.OldPassword, model.Email, model.Role, KBVaultHelperFunctions.UserAsKbUser(User).Id); usr = db.KbUsers.FirstOrDefault(u => u.Id == usr.Id); if (usr != null) { usr.LastName = model.LastName; usr.Name = model.Name; db.SaveChanges(); } return(RedirectToAction("Users")); } } return(View(model)); } catch (Exception ex) { Log.Error(ex); AddGlobalException(ex); return(RedirectToAction("Index", "Error")); } }
public JsonResult Like(int articleId) { var result = new JsonOperationResponse(); if (Request.IsAjaxRequest()) { using (var db = new KbVaultContext()) { var article = db.Articles.FirstOrDefault(a => a.Id == articleId); if (article == null) { result.ErrorMessage = ErrorMessages.ArticleNotFound; } else { article.Likes++; db.SaveChanges(); result.Successful = true; result.ErrorMessage = UIResources.ArticleLikeSuccess; } } } return(Json(result)); }
public JsonResult Remove(int id = -1) { var result = new JsonOperationResponse { Successful = false }; try { using (var db = new KbVaultContext()) { // Update all articles owned by deleted user var admin = db.KbUsers.First(a => a.Role == "Admin").Id; KBVaultHelperFunctions.UpdateArticlesOwner(id, admin); //KBVaultHelperFunctions.UpdateActivitiesOwner(id); // Delete user after updating articles db.KbUsers.Remove(db.KbUsers.First(u => u.Id == id)); db.SaveChanges(); result.Successful = true; } return(Json(result)); } catch (Exception ex) { Log.Error(ex); result.ErrorMessage = ex.Message; return(Json(result)); } }
public JsonResult Edit(string pk, string value) { var result = new JsonOperationResponse(); try { using (var db = new KbVaultContext()) { var tagId = Convert.ToInt64(pk); var tag = db.Tags.First(t => t.Id == tagId); if (tag != null) { tag.Author = KBVaultHelperFunctions.UserAsKbUser(User).Id; tag.Name = value; db.SaveChanges(); result.Successful = true; return(Json(result)); } } return(null); } catch (Exception ex) { Log.Error(ex); result.Successful = false; result.ErrorMessage = ex.Message; return(Json(result)); } }
public ActionResult Detail(long id) { try { using (var db = new KbVaultContext()) { var article = db.PublishedArticles().FirstOrDefault(a => a.Id == id); var author = article.KbUser; var lastAuthorEdited = db.KbUsers.FirstOrDefault(a => a.Id == article.LastAuthorEdited); if (article != null) { article.Views++; db.SaveChanges(); ViewBag.SimilarArticles = ArticleRepository.GetVisibleSimilarArticles((int)article.Id, DateTime.Today.Date); ViewBag.Author = author.Name + " " + author.LastName; ViewBag.LastAuthorEdited = lastAuthorEdited.Name + " " + lastAuthorEdited.LastName; return(View(article)); } return(View("ArticleNotFound")); } } catch (Exception ex) { Log.Error(ex); return(RedirectToAction("Index", "Error")); } }
public static void ChangePassword(string username, string oldPassword, string newPassword) { try { if (ValidateUser(username, oldPassword)) { using (var db = new KbVaultContext()) { KbUser usr = db.KbUsers.FirstOrDefault(ku => ku.UserName == username); if (usr != null) { usr.Password = HashPassword(newPassword, Guid.NewGuid().ToString().Replace("-", string.Empty)); db.SaveChanges(); } else { throw new UserNotFoundException(); } } } else { throw new InvalidPasswordException(); } } catch (Exception ex) { Log.Error(ex); throw; } }
public JsonResult Remove(int id = -1) { JsonOperationResponse result = new JsonOperationResponse(); result.Successful = false; try { using (var db = new KbVaultContext()) { Tag tag = db.Tags.First(t => t.Id == id); if (tag != null) { tag.Author = KBVaultHelperFunctions.UserAsKbUser(User).Id; db.Tags.Remove(tag); TagRepository.RemoveTagFromArticles(id); db.SaveChanges(); result.Successful = true; result.ErrorMessage = UIResources.TagListRemoveSuccessful; } else { result.ErrorMessage = ErrorMessages.TagNotFound; } } return(Json(result)); } catch (Exception ex) { Log.Error(ex); result.ErrorMessage = ex.Message; return(Json(result)); } }
public int Add(Category category) { using (var db = new KbVaultContext()) { db.Categories.Add(category); db.SaveChanges(); return(category.Id); } }
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 Index(SettingsViewModel model) { try { if (ModelState.IsValid) { using (var db = new KbVaultContext()) { Settings set = db.Settings.FirstOrDefault(); if (set != null) { db.Settings.Remove(set); } set = new Settings(); set.CompanyName = model.CompanyName; set.ArticleCountPerCategoryOnHomePage = model.ArticleCountPerCategoryOnHomePage; set.DisqusShortName = model.DisqusShortName; set.JumbotronText = model.JumbotronText; set.ShareThisPublicKey = model.ShareThisPublicKey; set.TagLine = model.TagLine; set.IndexFileExtensions = model.IndexFileExtensions; set.ArticlePrefix = model.ArticlePrefix; set.AnalyticsAccount = model.AnalyticsAccount; set.Author = KBVaultHelperFunctions.UserAsKbUser(User).Id; set.BackupPath = model.BackupPath; set.ShowTotalArticleCountOnFrontPage = model.ShowTotalArticleCountOnFrontPage; if (!string.IsNullOrEmpty(set.BackupPath)) { if (!set.BackupPath.EndsWith("\\") && !set.BackupPath.StartsWith("~")) { set.BackupPath += "\\"; } if (!set.BackupPath.EndsWith("/") && set.BackupPath.StartsWith("~")) { set.BackupPath += "/"; } } ConfigurationManager.AppSettings["Theme"] = model.SelectedTheme; db.Settings.Add(set); db.SaveChanges(); ShowOperationMessage(UIResources.SettingsPageSaveSuccessfull); } } model.Themes.AddRange(Directory.EnumerateDirectories(Server.MapPath("~/Views/Themes")).Select(e => Path.GetFileName(e)).ToList()); return(View(model)); } catch (Exception ex) { Log.Error(ex); ShowErrorMessage(ex.Message); return(RedirectToAction("Index", "Error")); } }
public void Save(Settings settings) { using (var db = new KbVaultContext()) { var set = db.Settings.FirstOrDefault(); if (set != null) { db.Settings.Remove(set); } db.Settings.Add(settings); db.SaveChanges(); } }
public static Attachment SaveAttachment(long articleId, HttpPostedFileBase attachedFile, long userId) { try { using (var db = new KbVaultContext()) { db.Configuration.ProxyCreationEnabled = false; db.Configuration.LazyLoadingEnabled = false; var article = db.Articles.First(a => a.Id == articleId); if (article != null) { var attachment = new Attachment(); var localPath = HttpContext.Current.Server.MapPath("~/Uploads"); attachment.Path = "~/Uploads/"; attachment.FileName = Path.GetFileName(attachedFile.FileName); attachment.Extension = Path.GetExtension(attachedFile.FileName); attachment.ArticleId = articleId; attachment.MimeType = attachedFile.ContentType; attachment.Hash = Guid.NewGuid().ToString().Replace("-", string.Empty); attachment.Author = userId; db.Attachments.Add(attachment); article.Attachments.Add(attachment); var path = Path.Combine(localPath, attachment.FileName); while (System.IO.File.Exists(path)) { attachment.FileName = Path.GetFileNameWithoutExtension(attachment.FileName) + Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(1, 5) + Path.GetExtension(attachment.FileName); path = Path.Combine(localPath, attachment.FileName); } attachedFile.SaveAs(path); db.SaveChanges(); return(attachment); } throw new ArgumentNullException(ErrorMessages.FileUploadArticleNotFound); } } catch (Exception ex) { Log.Error(ex); throw; } }
public ActionResult MyProfile(KbUserViewModel model) { try { if (ModelState.IsValid) { using (var db = new KbVaultContext()) { var username = ControllerContext.RequestContext.HttpContext.User.Identity.Name; var usr = db.KbUsers.FirstOrDefault(u => u.UserName == username); if (usr == null) { ModelState.AddModelError("UserNotFound", ErrorMessages.UserNotFound); return(View(model)); } if (KbVaultAuthHelper.ValidateUser(username, model.OldPassword)) { usr.Name = model.Name; usr.LastName = model.LastName; usr.Email = model.Email; if (!string.IsNullOrEmpty(model.NewPassword) && model.NewPassword == model.NewPasswordAgain) { KbVaultAuthHelper.ChangePassword(model.UserName, model.OldPassword, model.NewPassword); } db.SaveChanges(); ShowOperationMessage(UIResources.UserProfileUpdateSuccessful); return(RedirectToAction("Index", "Dashboard")); } else { ShowOperationMessage(ErrorMessages.WrongPassword); } } } return(View(model)); } catch (Exception ex) { Log.Error(ex); ShowOperationMessage(ex.Message); return(RedirectToAction("Index", "Error")); } }
public bool Remove(Category category) { using (var db = new KbVaultContext()) { var cat = db.Categories.FirstOrDefault(c => c.Id == category.Id); if (cat != null) { cat.Author = category.Author; db.Categories.Remove(cat); db.SaveChanges(); return(true); } else { return(false); } } }
public ActionResult UserInfo(KbUserViewModel model) { try { if (ModelState.IsValid) { using (var db = new KbVaultContext()) { var usr = db.KbUsers.FirstOrDefault(u => u.Id == model.Id); if (usr == null) { ModelState.AddModelError("UserNotFound", ErrorMessages.UserNotFound); return(View(model)); } if (KbVaultAuthHelper.ValidateUser(model.UserName, model.OldPassword)) { usr.Name = model.Name; usr.LastName = model.LastName; usr.Role = model.Role; usr.Email = model.Email; if (!string.IsNullOrEmpty(model.NewPassword) && model.NewPassword == model.NewPasswordAgain) { KbVaultAuthHelper.ChangePassword(model.UserName, model.OldPassword, model.NewPassword); } db.SaveChanges(); ShowOperationMessage(UIResources.UserListUserEditSuccessful); return(RedirectToAction("Users")); } } } return(View(model)); } catch (Exception ex) { Log.Error(ex); ShowOperationMessage(ex.Message); return(RedirectToAction("Index", "Error")); } }
public void Update(Category category) { using (var db = new KbVaultContext()) { var cat = db.Categories.FirstOrDefault(c => c.Id == category.Id); if (cat != null) { cat.Name = category.Name; cat.IsHot = category.IsHot; cat.Author = category.Author; cat.Parent = category.Parent; cat.Icon = category.Icon; db.SaveChanges(); } else { throw new ArgumentNullException(); } } }
public static void UpdateArticlesOwner(long oldOwnerId, long newOwnerId) { try { using (var db = new KbVaultContext()) { // Update all articles owned by deleted user var articles = db.Articles.Where(a => a.Author == oldOwnerId); foreach (var article in articles) { article.Author = newOwnerId; } db.SaveChanges(); } } catch (Exception ex) { Log.Error(ex); throw; } }
public static KbUser CreateUser(string username, string password, string email, string role, long author) { try { using (var db = new KbVaultContext()) { KbUser usr = new KbUser(); usr.Password = HashPassword(password, Guid.NewGuid().ToString().Replace("-", string.Empty)); usr.UserName = username; usr.Email = email; usr.Role = role; usr.Author = author; db.KbUsers.Add(usr); db.SaveChanges(); return(usr); } } catch (Exception ex) { Log.Error(ex); throw; } }
public static void AdminChangePassword(long id, string newPassword) { try { using (var db = new KbVaultContext()) { KbUser usr = db.KbUsers.FirstOrDefault(ku => ku.Id == id); if (usr != null) { usr.Password = HashPassword(newPassword, Guid.NewGuid().ToString().Replace("-", string.Empty)); db.SaveChanges(); } else { throw new UserNotFoundException(); } } } catch (Exception ex) { Log.Error(ex); throw; } }