Пример #1
0
        public async Task UpdateAsyncDoesntUpdateWithNonExistendId()
        {
            await this.SeedData();

            var input = new EditPageInputModel()
            {
                Id   = 2,
                Name = "test2",
            };

            await this.pagesService.UpdateAsync(input, "test");

            Assert.Equal("test", (await this.pagesRepository.All().FirstOrDefaultAsync()).Name);
        }
Пример #2
0
        public async Task UpdateAsync(EditPageInputModel input, string path)
        {
            var page = this.pagesRepository.All().Where(x => x.Id == input.Id).FirstOrDefault();

            if (page != null)
            {
                page.Name        = input.Name;
                page.Description = input.Description;
                page.Email       = input.Email;
                page.Phone       = input.Phone;

                var tagIds = new List <int>();
                if (input.Tags?.Count() > 0)
                {
                    foreach (var tag in input.Tags.Distinct())
                    {
                        var tagId = await this.tagsService.GetIdAsync(tag);

                        tagIds.Add(tagId);
                        if (!this.pageTagsRepository.AllAsNoTracking().Any(x => x.TagId == tagId && x.PageId == page.Id))
                        {
                            await this.pageTagsRepository.AddAsync(new PageTag()
                            {
                                TagId  = tagId,
                                PageId = page.Id,
                            });
                        }
                    }
                }

                var tagsToRemove = this.pageTagsRepository.All().Where(x => x.PageId == page.Id && !tagIds.Contains(x.TagId)).ToList();
                foreach (var tag in tagsToRemove)
                {
                    this.pageTagsRepository.Delete(tag);
                }

                await this.pageTagsRepository.SaveChangesAsync();

                if (input.Image != null && input.Image.Length > 0)
                {
                    page.ImageId = await this.imagesService.CreateAsync(input.Image, path);
                }

                this.pagesRepository.Update(page);
                await this.pagesRepository.SaveChangesAsync();
            }
        }
Пример #3
0
        public async Task UpdateAsyncWorksCorrectly()
        {
            await this.SeedData();

            var input = new EditPageInputModel()
            {
                Id   = 1,
                Name = "test2",
                Tags = new List <string> {
                    "tag1"
                },
            };

            await this.pagesService.UpdateAsync(input, "test");

            Assert.Equal("test2", (await this.pagesRepository.All().FirstOrDefaultAsync()).Name);
        }
Пример #4
0
        public async Task <IActionResult> Edit(EditPageInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var userId  = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
            var isOwner = this.pagesService.IsOwner(userId, input.Id);

            if (isOwner)
            {
                await this.pagesService.UpdateAsync(input, $"{this.webHost.WebRootPath}/img/pages");

                return(this.RedirectToAction(nameof(this.ById), new { input.Id }));
            }

            return(this.BadRequest());
        }
Пример #5
0
        public async Task UpdateAsyncWorksCorrectlyWithImageAndNoTags()
        {
            await this.SeedData();

            var imageMock = new Mock <IFormFile>();

            imageMock.Setup(x => x.Length).Returns(1);

            var input = new EditPageInputModel()
            {
                Id    = 1,
                Name  = "test2",
                Tags  = null,
                Image = imageMock.Object,
            };

            await this.pagesService.UpdateAsync(input, "test");

            Assert.Equal("test2", (await this.pagesRepository.All().FirstOrDefaultAsync()).Name);
        }
Пример #6
0
        public IActionResult EditStaticPages(EditPageInputModel model)
        {
            if (ModelState.IsValid)
            {
                pageService.EditStaticPage(model.OldName, model.Name, model.Content);

                string content = string.Format("{0} modified contents of pagename: {1} / {2}.", GetCurrentUserName(User.GetUserId()), model.OldName, model.Name);
                logsService.AddNewLog("Modify", content);

                return(RedirectToAction("EditStaticPages", new { pageName = model.Name }));
            }
            else
            {
                var pageData = PopulateAdminPageViewModel(model.Name);

                pageData.Content = "";

                ViewData["message"] = "Content cannot be empty!";

                return(View(pageData));
            }
        }
Пример #7
0
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var page = this.Data.Pages.Find(id);

            if (page == null)
            {
                return this.HttpNotFound();
            }

            var model = new EditPageInputModel
            {
                Id = page.Id,
                Title = page.Title,
                Content = page.Content,
                CreatedOn = page.CreatedOn,
                AuthorId = page.AuthorId
            };

            return this.View(model);
        }
Пример #8
0
        public ActionResult Edit(EditPageInputModel pageInputModel)
        {
            if (this.ModelState.IsValid)
            {
                var page = this.Data.Pages.Find(pageInputModel.Id);

                page.Id = pageInputModel.Id;
                page.Title = pageInputModel.Title;
                page.Content = pageInputModel.Content;
                page.AuthorId = pageInputModel.AuthorId;
                page.CreatedOn = pageInputModel.CreatedOn;

                this.Data.Pages.Update(page);
                this.Data.SaveChanges();

                return this.RedirectToAction("Index");
               }

            return this.View(pageInputModel);
        }
Пример #9
0
 public EditPageModel EditPage(EditPageInputModel editPageInputModel)
 {
     return(Post <EditPageModel, EditPageInputModel>("mod_wiki_edit_page", editPageInputModel));
 }