Esempio n. 1
0
        public ActionResult Create(StaticTextCRUDModel model)
        {
            if (ModelState.IsValid)
            {
                var crudOperations = RepositoryFactory.Action<IStaticTextCrud>();

                if (string.IsNullOrWhiteSpace(model.FriendlyUrl))
                {
                    model.FriendlyUrl = model.Title;
                }

                model.FriendlyUrl = GenerateUniqueFriendlyUrl(model.FriendlyUrl, Guid.Empty);
                model.ChangeDate = DateTime.Now;
                model.Creator = RepositoryFactory.Action<IUserAction>().GetByName(UserInfo.Name);
                model.Description = GenerateDescription(model.Text);

                using (var tran = RepositoryFactory.StartTransaction(IoC.Resolve<SearchUpdateContext>()))
                {
                    RepositoryFactory.Action<IStaticTextCrud>().Create(model);
                    tran.Commit();
                    RepositoryFactory.Action<ISearchAction>().IndexStaticText(tran, model);
                }
                return RedirectToActionWithAjax("Index");

            }
            return ViewWithAjax(model);
        }
Esempio n. 2
0
        public ActionResult Delete(StaticTextCRUDModel model)
        {
            var item = RepositoryFactory.Action<IStaticTextCrud>().FindByPk(model.Id);
            if (item.Creator.Name == UserInfo.Name
                || UserInfo.IsAdmin())
            {
                using (var tran = RepositoryFactory.StartTransaction(IoC.Resolve<SearchUpdateContext>()))
                {
                    RepositoryFactory.Action<IStaticTextCrud>().Delete(model);
                    tran.Commit();
                    RepositoryFactory.Action<ISearchAction>().DeleteFromIndex(tran, model.Id.ToString());
                }
                return RedirectToActionWithAjax("Index");
            }
            ModelState.AddModelError(string.Empty, ModelResources.NotAllowedToDeleteError);

            return ViewWithAjax(model);
        }
Esempio n. 3
0
        public ActionResult Edit(StaticTextCRUDModel model)
        {
            if (ModelState.IsValid)
            {
                var crudOperations = RepositoryFactory.Action<IStaticTextCrud>();

                // get original item to test change permissions
                var originalItem = crudOperations.FindByPk(model.Id);

                if (originalItem != null)
                {
                    if (originalItem.Creator.Name == UserInfo.Name
                        || UserInfo.IsAdmin())
                    {

                        if (string.IsNullOrWhiteSpace(model.FriendlyUrl))
                        {
                            model.FriendlyUrl = model.Title;
                        }

                        model.FriendlyUrl = GenerateUniqueFriendlyUrl(model.FriendlyUrl, model.Id);
                        model.ChangeDate = DateTime.Now;
                        model.Creator = RepositoryFactory.Action<IUserAction>().GetByName(UserInfo.Name);
                        model.Description = GenerateDescription(model.Text);

                        using (var tran = RepositoryFactory.StartTransaction(IoC.Resolve<SearchUpdateContext>()))
                        {
                            RepositoryFactory.Action<IStaticTextCrud>().Update(model);
                            tran.Commit();
                            RepositoryFactory.Action<ISearchAction>().DeleteFromIndex(tran, model.Id.ToString());
                            RepositoryFactory.Action<ISearchAction>().IndexStaticText(tran, model);
                        }
                        return RedirectToActionWithAjax("Index");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, ModelResources.NotAllowedToChangeError);
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, ModelResources.ItemNotExistsError);
                }
            }
            return ViewWithAjax(model);
        }
Esempio n. 4
0
        public void Test_update()
        {
            using (RepositoryFactory.StartUnitOfWork())
            {
                var item = new StaticTextCRUDModel()
                {
                    Creator = _user,
                    ChangeDate = new DateTime(2002, 1, 1),
                    PublishDate = new DateTime(2002, 2, 1),
                    AllowComments = true,
                    Title = "text",
                    FriendlyUrl = "text_update",
                    Text = "updateable new content",
                    Description = "description update"
                };

                var crudActions = RepositoryFactory.GetRepository<StaticText>().GetAction<IStaticTextCrud>();

                using (var tran = RepositoryFactory.StartTransaction())
                {
                    item = crudActions.Create(item);
                    tran.Commit();
                }

                // check created item
                var storedItem = crudActions.FindByPk(item.Id);

                Assert.AreEqual(item.Id, storedItem.Id);
                Assert.AreEqual(item.FriendlyUrl, storedItem.FriendlyUrl);
                Assert.AreEqual(((User)item.Creator).Id, ((User)storedItem.Creator).Id);
                Assert.AreEqual(item.ChangeDate, storedItem.ChangeDate);
                Assert.AreEqual(item.PublishDate, storedItem.PublishDate);
                Assert.AreEqual(item.AllowComments, storedItem.AllowComments);
                Assert.AreEqual(item.Title, storedItem.Title);
                Assert.AreEqual(item.Text, storedItem.Text);
                Assert.AreEqual(item.Description, storedItem.Description);

                // change item
                item.Text = "updateable new content - change 1";
                item.Description = "updated description update";
                item.ChangeDate = new DateTime(2002, 2, 1);

                using (var tran = RepositoryFactory.StartTransaction())
                {
                    item = crudActions.Update(item);
                    tran.Commit();
                }

                // check changed item
                storedItem = crudActions.FindByPk(item.Id);

                Assert.AreEqual(item.Id, storedItem.Id);
                Assert.AreEqual(item.FriendlyUrl, storedItem.FriendlyUrl);
                Assert.AreEqual(((User)item.Creator).Id, ((User)storedItem.Creator).Id);
                Assert.AreEqual(item.ChangeDate, storedItem.ChangeDate);
                Assert.AreEqual(item.PublishDate, storedItem.PublishDate);
                Assert.AreEqual(item.AllowComments, storedItem.AllowComments);
                Assert.AreEqual(item.Title, storedItem.Title);
                Assert.AreEqual(item.Text, storedItem.Text);
                Assert.AreEqual(item.Description, storedItem.Description);

                // change item
                item.Text = "updateable new content - change 2";
                item.ChangeDate = new DateTime(2002, 3, 1);

                using (var tran = RepositoryFactory.StartTransaction())
                {
                    item = crudActions.Update(item);
                    tran.Commit();
                }

                // check changed item
                storedItem = crudActions.FindByPk(item.Id);

                Assert.AreEqual(item.Id, storedItem.Id);
                Assert.AreEqual(((User)item.Creator).Id, ((User)storedItem.Creator).Id);
                Assert.AreEqual(item.ChangeDate, storedItem.ChangeDate);
                Assert.AreEqual(item.PublishDate, storedItem.PublishDate);
                Assert.AreEqual(item.AllowComments, storedItem.AllowComments);
                Assert.AreEqual(item.Title, storedItem.Title);
                Assert.AreEqual(item.Text, storedItem.Text);
                Assert.AreEqual(item.Description, storedItem.Description);

                // check if there are 3 items in history
                var staticText = SessionFactory<StaticText>.Load(item.Id);
                Assert.AreEqual(3, staticText.StaticTextVersions.Count);
                Assert.AreEqual(new DateTime(2002, 1, 1), staticText.StaticTextVersions[0].CreatedDate);
                Assert.AreEqual("updateable new content", staticText.StaticTextVersions[0].Text);
                Assert.AreEqual(new DateTime(2002, 2, 1), staticText.StaticTextVersions[1].CreatedDate);
                Assert.AreEqual("updateable new content - change 1", staticText.StaticTextVersions[1].Text);
                Assert.AreEqual(new DateTime(2002, 3, 1), staticText.StaticTextVersions[2].CreatedDate);
                Assert.AreEqual("updateable new content - change 2", staticText.StaticTextVersions[2].Text);
            }
        }
Esempio n. 5
0
        public void Test_delete()
        {
            using (RepositoryFactory.StartUnitOfWork())
            {

                var item = new StaticTextCRUDModel()
                               {
                                   Creator = _user,
                                   ChangeDate = new DateTime(2002, 1, 1),
                                   PublishDate = new DateTime(2002, 2, 1),
                                   AllowComments = true,
                                   Title = "updateable new",
                                   Text = "updateable new content"
                               };

                var crudActions = RepositoryFactory.GetRepository<StaticText>().GetAction<IStaticTextCrud>();
                var dataActions = RepositoryFactory.GetRepository<StaticText>().GetAction<IStaticTextData>();
                var initialStaticTextCount = dataActions.GetAll().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.Creator).Id, ((User)storedItem.Creator).Id);
                Assert.AreEqual(item.ChangeDate, storedItem.ChangeDate);
                Assert.AreEqual(item.PublishDate, storedItem.PublishDate);
                Assert.AreEqual(item.AllowComments, storedItem.AllowComments);
                Assert.AreEqual(item.Title, storedItem.Title);
                Assert.AreEqual(item.Text, storedItem.Text);

                Assert.AreEqual(initialStaticTextCount + 1, dataActions.GetAll().Count());

                using (var tran = RepositoryFactory.StartTransaction())
                {
                    crudActions.Delete(item);
                    tran.Commit();
                }

                WaitForIndexing();

                Assert.AreEqual(initialStaticTextCount, dataActions.GetAll().Count());
            }
        }
Esempio n. 6
0
        public void Test_create()
        {
            using (RepositoryFactory.StartUnitOfWork())
            {
                var item = new StaticTextCRUDModel()
                               {
                                   Creator = _user,
                                   ChangeDate = new DateTime(2002, 1, 1),
                                   PublishDate = new DateTime(2002, 2, 1),
                                   AllowComments = true,
                                   Title = "čreate new",
                                   Description = "description new",
                                   FriendlyUrl = "create-new",
                                   Text = "create new content"
                               };

                var crudActions = RepositoryFactory.GetRepository<StaticText>().GetAction<IStaticTextCrud>();

                using (var tran = RepositoryFactory.StartTransaction())
                {
                    item = crudActions.Create(item);
                    tran.Commit();
                }

                Assert.AreEqual("create-new", item.FriendlyUrl);

                var storedItem = crudActions.FindByPk(item.Id);

                Assert.AreEqual(item.Id, storedItem.Id);
                Assert.AreEqual(item.FriendlyUrl, storedItem.FriendlyUrl);
                Assert.AreEqual(((User)item.Creator).Id, ((User)storedItem.Creator).Id);
                Assert.AreEqual(item.ChangeDate, storedItem.ChangeDate);
                Assert.AreEqual(item.PublishDate, storedItem.PublishDate);
                Assert.AreEqual(item.AllowComments, storedItem.AllowComments);
                Assert.AreEqual(item.Title, storedItem.Title);
                Assert.AreEqual(item.Text, storedItem.Text);
                Assert.AreEqual(item.Description, storedItem.Description);
            }
        }