public void Test_update() { using (RepositoryFactory.StartUnitOfWork()) { var item = new CommentCRUDModel() { Name = "test_update", ParentId = null, ChangeUser = _user, AnonymousName = null, ClientIp = "127.0.0.1", ContentId = _testText.Id, Text = "test_update", ChangeDate = new DateTime(2002, 1, 1), UserAgent = "ie6" }; var crudActions = RepositoryFactory.GetRepository <Comment>().GetAction <ICommentCrud>(); using (var tran = RepositoryFactory.StartTransaction()) { item = crudActions.Create(item); tran.Commit(); } var storedItem = crudActions.FindByPk(item.Id); Assert.AreEqual(item.Id, storedItem.Id); Assert.AreEqual(((User)item.ChangeUser).Id, ((User)storedItem.ChangeUser).Id); Assert.AreEqual(item.ChangeDate, storedItem.ChangeDate); Assert.AreEqual(item.AnonymousName, storedItem.AnonymousName); Assert.AreEqual(item.UserAgent, storedItem.UserAgent); Assert.AreEqual(item.ClientIp, storedItem.ClientIp); Assert.AreEqual(item.Name, storedItem.Name); Assert.AreEqual(item.Text, storedItem.Text); Assert.AreEqual(item.ContentId, storedItem.ContentId); item.Text = "test_update_changed"; item.ChangeDate = new DateTime(2002, 1, 1); item.ChangeUser = null; using (var tran = RepositoryFactory.StartTransaction()) { item = crudActions.Create(item); tran.Commit(); } storedItem = crudActions.FindByPk(item.Id); Assert.AreEqual(item.Id, storedItem.Id); Assert.AreEqual(null, storedItem.ChangeUser); Assert.AreEqual(item.ChangeDate, storedItem.ChangeDate); Assert.AreEqual(item.AnonymousName, storedItem.AnonymousName); Assert.AreEqual(item.UserAgent, storedItem.UserAgent); Assert.AreEqual(item.ClientIp, storedItem.ClientIp); Assert.AreEqual(item.Name, storedItem.Name); Assert.AreEqual(item.Text, storedItem.Text); Assert.AreEqual(item.ContentId, storedItem.ContentId); } }
public ActionResult Edit(CommentCRUDModel model) { if (ModelState.IsValid) { var crudOperations = RepositoryFactory.Action <ICommentCrud>(); // get original item to test change permissions var originalItem = crudOperations.FindByPk(model.Id); if (originalItem != null) { originalItem.Name = model.Name; originalItem.Text = model.Text; using (var tran = RepositoryFactory.StartTransaction(IoC.Resolve <SearchUpdateContext>())) { crudOperations.Update(originalItem); tran.Commit(); RepositoryFactory.Action <ISearchAction>().DeleteFromIndex(tran, originalItem.Id.ToString()); RepositoryFactory.Action <ISearchAction>().IndexComment(tran, originalItem); } return(RedirectToActionWithAjax("Index")); } else { ModelState.AddModelError(string.Empty, ModelResources.ItemNotExistsError); } } return(ViewWithAjax(model)); }
public void Test_delete() { using (RepositoryFactory.StartUnitOfWork()) { var item = new CommentCRUDModel() { Name = "test_delete", ParentId = null, ChangeUser = _user, AnonymousName = null, ClientIp = "127.0.0.1", ContentId = _testText.Id, Text = "test_delete", ChangeDate = new DateTime(2002, 1, 1), UserAgent = "ie6" }; var crudActions = RepositoryFactory.GetRepository <Comment>().GetAction <ICommentCrud>(); var dataActions = RepositoryFactory.GetRepository <Comment>().GetAction <ICommentData>(); var initialCommentCount = dataActions.GetAllForAdmin().Count(); using (var tran = RepositoryFactory.StartTransaction()) { item = crudActions.Create(item); tran.Commit(); } WaitForIndexing(); // check created item var storedItem = crudActions.FindByPk(item.Id); Assert.AreEqual(item.Id, storedItem.Id); Assert.AreEqual(((User)item.ChangeUser).Id, ((User)storedItem.ChangeUser).Id); Assert.AreEqual(item.ChangeDate, storedItem.ChangeDate); Assert.AreEqual(item.AnonymousName, storedItem.AnonymousName); Assert.AreEqual(item.UserAgent, storedItem.UserAgent); Assert.AreEqual(item.ClientIp, storedItem.ClientIp); Assert.AreEqual(item.Name, storedItem.Name); Assert.AreEqual(item.Text, storedItem.Text); Assert.AreEqual(item.ContentId, storedItem.ContentId); Assert.AreNotEqual(initialCommentCount, dataActions.GetAllForAdmin().Count()); using (var tran = RepositoryFactory.StartTransaction()) { crudActions.Delete(item); tran.Commit(); } WaitForIndexing(); Assert.AreEqual(initialCommentCount, dataActions.GetAllForAdmin().Count()); } }
public ActionResult Delete(CommentCRUDModel model) { var item = RepositoryFactory.Action <ICommentCrud>().FindByPk(model.Id); using (var tran = RepositoryFactory.StartTransaction(IoC.Resolve <SearchUpdateContext>())) { RepositoryFactory.Action <ICommentCrud>().Delete(item); tran.Commit(); RepositoryFactory.Action <ISearchAction>().DeleteFromIndex(tran, item.Id.ToString()); } return(RedirectToActionWithAjax("Index")); }
public ActionResult NewComment(PagedModel <CommentViewModel> commentsModel, CommentCRUDModel model, string sort) { var staticText = RepositoryFactory.Action <IStaticTextData>().Get(model.ContentId, DateTime.Now); model.ChangeUser = UserInfo.User; if (model.ChangeUser == null && model.RoboCheck != model.ExpressionCorrectValue) { ModelState.AddModelError <CommentCRUDModel>(item => item.RoboCheck, ModelResources.RoboCheckError); } // check for anonymous name required if not logged user if (model.ChangeUser == null && string.IsNullOrEmpty(model.AnonymousName)) { ModelState.AddModelError <CommentCRUDModel>(item => item.AnonymousName, ModelResources.AnonymousRequireError); } else if (ModelState.IsValid) { model.ChangeDate = DateTime.Now; model.ClientIp = Request.UserHostAddress; model.UserAgent = Request.UserAgent; // for anonymous user encode text if (model.ChangeUser == null) { model.Text = HtmlManipulation.RemoveTags(model.Text); } using (var tran = RepositoryFactory.StartTransaction(IoC.Resolve <SearchUpdateContext>())) { RepositoryFactory.Action <ICommentCrud>().Create(model); tran.Commit(); RepositoryFactory.Action <ISearchAction>().IndexComment(tran, model); } return(RedirectToActionWithAjax(staticText.FriendlyUrl, routeValues: new { sort = sort })); } CommentViewTypeEnum sortType = ParseCommentViewType(sort); IEnumerable <CommentTreeViewModel> comments; LoadComments(sortType, out comments, commentsModel, staticText.Id); return(ViewWithAjax("ViewPage", new PageViewModel { StaticText = staticText, CommentViewType = sortType, FlatComments = commentsModel, TreeComments = comments, NewComment = model })); }
/// <summary> /// Creates the specified item. /// </summary> /// <param name="item">The item.</param> /// <returns>Created item.</returns> public CommentCRUDModel Create(CommentCRUDModel item) { var content = SessionFactory <Content> .Load(item.ContentId); var comment = new Comment() { Id = Guid.NewGuid(), Name = item.Name, Content = content, Owner = (User)item.ChangeUser, AnonymousName = item.AnonymousName, CreatedDate = item.ChangeDate, ActualVersion = 0, CommentVersions = new List <CommentVersion>() { new CommentVersion { CreatedDate = item.ChangeDate, CreatedBy = (User)item.ChangeUser, ClientIp = item.ClientIp, UserAgent = item.UserAgent, Text = item.Text, Version = 0 } } }; if (item.ParentId == null) { comment.Level = 0; comment.TopComment = comment; comment.ParentVersion = comment.ActualVersion; } else { var parentComment = SessionFactory <Comment> .Load(item.ParentId.Value); comment.Level = parentComment.Level + 1; comment.ParentComment = parentComment; comment.TopComment = parentComment.TopComment; comment.ParentVersion = parentComment.ActualVersion; } SessionFactory <Comment> .Store(comment); // assign id item.Id = comment.Id; return(item); }
/// <summary> /// Updates the specified item. /// </summary> /// <param name="item">The item.</param> /// <returns>Updted item.</returns> public CommentCRUDModel Update(CommentCRUDModel item) { var comment = SessionFactory <Comment> .Load(item.Id); comment.Name = item.Name; comment.ActualVersion = comment.CommentVersions.Count; comment.CommentVersions.Add( new CommentVersion() { CreatedDate = item.ChangeDate, CreatedBy = (User)item.ChangeUser, ClientIp = item.ClientIp, UserAgent = item.UserAgent, Text = item.Text, Version = comment.ActualVersion } ); SessionFactory <Comment> .Store(comment); return(item); }
/// <summary> /// Indexes the comment. /// </summary> /// <param name="transaction">The transaction.</param> /// <param name="comment">The comment.</param> public void IndexComment(ITransaction transaction, CommentCRUDModel comment) { SearchUpdateContext tranContext = transaction.TransactionContext as SearchUpdateContext; if (tranContext == null) { throw new Exception("SearchUpdateContext not part of ITransaction!"); } Document doc = new Document(); doc.Add(new Field(SearchResult.IdField, comment.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field(SearchResult.TypeField, SearchResult.CommentType, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("Title", comment.Name, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("Text", RemoveTags(comment.Text), Field.Store.NO, Field.Index.ANALYZED)); doc.Add(new Field("Published", DateTools.DateToString(comment.ChangeDate, DateTools.Resolution.SECOND), Field.Store.NO, Field.Index.NOT_ANALYZED)); doc.Add(new Field("Date", DateTools.DateToString(comment.ChangeDate, DateTools.Resolution.SECOND), Field.Store.NO, Field.Index.NOT_ANALYZED)); var user = comment.ChangeUser != null ? comment.ChangeUser.Name : comment.AnonymousName; doc.Add(new Field("User", user, Field.Store.NO, Field.Index.ANALYZED)); tranContext.IndexWriter.AddDocument(doc); }
/// <summary> /// Deletes the specified item. /// </summary> /// <param name="item">The item.</param> public void Delete(CommentCRUDModel item) { var comment = SessionFactory <Comment> .Load(item.Id); SessionFactory <Comment> .Delete(comment); }