public async Task <ActionResult> Edit(int id, int postId, [Bind(Prefix = "sectionn")] EditSectionInputModel sectionInputModel) { if (!this.ModelState.IsValid) { return(this.RedirectToAction("Edit", "Posts", new { postId })); } try { // TODO: Add update logic here var beforeUpdateSc = await this.sectionService.GetSectionByIdAsync <EditSectionViewModel>(id); if (beforeUpdateSc.Content != sectionInputModel.SectionContent || beforeUpdateSc.Title != sectionInputModel.SectionTitle) { // TODO: Do magic if the boolean is false await this.sectionService.UpdateSectionByIdAsync(id, sectionInputModel); } return(this.RedirectToAction("Edit", "Posts", new { postId })); } catch { return(this.RedirectToAction("Edit", "Posts", new { postId })); } }
public async Task <bool> UpdateSectionByIdAsync(int sectionId, EditSectionInputModel modifiedSection) { var sectionUpdate = await this.sections.All().SingleOrDefaultAsync(s => s.Id == sectionId); sectionUpdate.Content = modifiedSection.SectionContent; sectionUpdate.Title = modifiedSection.SectionTitle; this.sections.Update(sectionUpdate); return(await this.sections.SaveChangesAsync() > 0); }
public async Task UpdateSectionByIdAsyncShouldReturnExpectedSection() { // Arrange var expectedSection = new EditSectionInputModel { Id = 2, SectionContent = "Updated", SectionTitle = "Updated", }; // Act var result = await this.sectionService.UpdateSectionByIdAsync(2, expectedSection); var testedSection = this.sectionRepository.AllWithDeleted().FirstOrDefault(s => s.Id == 2); // Assert Assert.True(expectedSection.SectionContent == testedSection.Content && expectedSection.SectionTitle == expectedSection.SectionTitle && expectedSection.Id == testedSection.Id); }
public async Task EditSection(EditSectionInputModel model) { var section = await this.db.Sections.FirstOrDefaultAsync(x => x.Id == model.Id); var sectionText = await this.db.PartTexts .FirstOrDefaultAsync(x => x.SectionId == model.Id); var sectionImage = await this.db.PartImages .FirstOrDefaultAsync(x => x.SectionId == model.Id); section.Name = model.Name; if (sectionText == null && (model.Heading != null || model.Subheading != null || model.Description != null)) { this.db.PartTexts.Add(new PartText { SectionId = section.Id, Heading = model.Heading, HeadingBg = model.HeadingBg, Subheading = model.Subheading, SubheadingBg = model.SubheadingBg, Description = model.SanitizeDescription, DescriptionBg = model.SanitizeDescriptionBg, }); } if (sectionText != null) { sectionText.Heading = model.Heading; sectionText.HeadingBg = model.HeadingBg; sectionText.Subheading = model.Subheading; sectionText.SubheadingBg = model.SubheadingBg; sectionText.Description = model.SanitizeDescription; sectionText.DescriptionBg = model.DescriptionBg; this.db.PartTexts.Update(sectionText); } if (sectionImage == null && model.Image != null) { var imageUrl = await ApplicationCloudinary.UploadImage( this.cloudinary, model.Image, $"SectionImage-{section.Id}"); this.db.PartImages.Add(new PartImage { SectionId = section.Id, Name = $"SectionImage-{section.Id}", Url = imageUrl, }); } if (sectionImage != null && model.Image != null) { var imageUrl = await ApplicationCloudinary.UploadImage( this.cloudinary, model.Image, sectionImage.Name); sectionImage.Url = imageUrl; this.db.PartImages.Update(sectionImage); } await this.db.SaveChangesAsync(); }