public async Task <IActionResult> Post([FromBody] SectionInputModel sectionInputModel) { var section = Section.Create(sectionInputModel.Name, sectionInputModel.Description, sectionInputModel.FormId); await _dbContext.Sections.AddAsync(section); await _dbContext.SaveChangesAsync(); return(NoContent()); }
public async Task <bool> AddSectionToPostAsync(int postId, SectionInputModel sectionInputModel) { var currentPost = await this.posts.All() .SingleOrDefaultAsync(p => p.Id == postId); var sectionForAdding = await this.sectionService.CreateSectionServiceOnlyAsync(sectionInputModel); currentPost.PostSections.Add(new SectionPost { Section = sectionForAdding }); int savedChanges = await this.posts.SaveChangesAsync(); return(savedChanges > 0); }
public async Task <Section> CreateSectionServiceOnlyAsync(SectionInputModel sectionInputModel) { var sectionForDb = new Section { CreatedOn = DateTime.UtcNow, Content = sectionInputModel.SectionContent, Title = sectionInputModel.SectionTitle, }; await this.sections.AddAsync(sectionForDb); await this.sections.SaveChangesAsync(); return(sectionForDb); }
public ActionResult Create(SectionInputModel input) { if (input != null && this.ModelState.IsValid) { var section = new Section { Title = input.Title }; this.Data.Sections.Add(section); this.Data.SaveChanges(); return(this.RedirectToAction("All")); } return(this.View(input)); }
public async Task CreateSectionAsyncShouldReturnExpectedId() { // Arrange var expectedId = 11; SectionInputModel expectedSection = new SectionInputModel() { SectionContent = "dfd", SectionTitle = "dfdf", }; // Act await this.sectionService.CreateSectionAsync(expectedSection, 1); // Assert var actualSection = this.sectionRepository.AllWithDeleted().Last(); Assert.True(expectedSection.SectionContent == actualSection.Content && expectedSection.SectionTitle == actualSection.Title && expectedId == actualSection.Id); }
public async Task <int> CreateSectionAsync(SectionInputModel sectionInputModel, int postId) { var sectionForDb = new Section { CreatedOn = DateTime.UtcNow, Content = sectionInputModel.SectionContent, Title = sectionInputModel.SectionTitle, }; await this.sections.AddAsync(sectionForDb); sectionForDb.SectionPosts.Add(new SectionPost { PostId = postId }); await this.sections.SaveChangesAsync(); return(sectionForDb.Id); }
public async Task AddSectionToPostAsyncShouldAddTheSectionToThePost() { // Arrange var sectionTitle = "dsfdfsdfs"; var sectionContent = "fdssdf"; var sectionInputModel = new SectionInputModel { SectionContent = sectionContent, SectionTitle = sectionTitle, }; // Act await this.postService.AddSectionToPostAsync(1, sectionInputModel); var currentPost = this.postsRepository.AllWithDeleted().FirstOrDefault(p => p.Id == 1); // Assert var addedSection = currentPost.PostSections.Last()?.Section; Assert.True(addedSection?.Title == sectionTitle && addedSection?.Content == sectionContent); }
public async Task <IActionResult> Add(SectionInputModel sectionInputModel) { var id = int.Parse(this.TempData["ProductId"].ToString()); if (!this.ModelState.IsValid) { this.TempData["alert"] = SectionErrorMsg; return(this.RedirectToAction("Details", "Posts", new { id = id })); } var sectionId = await this.sectionService.CreateSectionAsync(sectionInputModel, id); if (sectionId != 0) { return(this.RedirectToAction("Details", "Posts", new { id = id })); } this.TempData["alert"] = SectionAddErrorMsg; this.logger.LogError(message: $"The section cannot be saved in the database <-----> postId -> {id}."); return(this.RedirectToAction("Details", "Posts", new { id = id })); }