コード例 #1
0
        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());
        }
コード例 #2
0
        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);
        }
コード例 #3
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);
        }
コード例 #4
0
        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));
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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 }));
        }