public async Task <Result <Unit> > Handle(CreateDiscussionCommand request, CancellationToken cancellationToken)
        {
            // Get the currently authorized user.
            var user = await _context.Users.SingleOrDefaultAsync(x =>
                                                                 x.UserName == _userAccessor.GetCurrentUsername());

            // Get the classroom that this discussion belongs to.
            var classroom = await _context.Classrooms.SingleOrDefaultAsync(x =>
                                                                           x.Id == request.ClassroomId);

            // Create a new entry in the Discussions table.
            var discussion = new Discussion {
                Id        = request.Id,
                Name      = request.Name,
                Classroom = classroom,
                CreatedBy = user
            };

            _context.Discussions.Add(discussion);

            // Save changes to the database.
            var success = await _context.SaveChangesAsync() > 0;

            // Return the final response.
            if (success)
            {
                return(Result <Unit> .Success(Unit.Value));
            }

            return(Result <Unit> .Failure("There was a problem saving changes."));
        }
Exemplo n.º 2
0
        public void ShouldNotCallHandleIfNotSavedChanges()
        {
            context.Setup(x => x.Discussions).Returns(dbSetDiscussion.Object);
            context.Setup(x => x.Posts).Returns(dbSetPost.Object);
            context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(0));

            CreateDiscussionCommandHandler createDiscussionCommandHandler = new CreateDiscussionCommandHandler(context.Object, stringLocalizer.Object, mapper.Object);
            CreateDiscussionCommand        createDiscussionCommand        = new CreateDiscussionCommand(discussionDto);

            mapper.Setup(x => x.Map <Discussion>(createDiscussionCommand)).Returns(discussion);

            Func <Task> act = async() => await createDiscussionCommandHandler.Handle(createDiscussionCommand, new CancellationToken());

            act.Should().Throw <RestException>();
        }
Exemplo n.º 3
0
        public async Task ShouldCallHandle()
        {
            context.Setup(x => x.Discussions).Returns(dbSetDiscussion.Object);
            context.Setup(x => x.Posts).Returns(dbSetPost.Object);
            context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(1));

            CreateDiscussionCommandHandler createDiscussionCommandHandler = new CreateDiscussionCommandHandler(context.Object, stringLocalizer.Object, mapper.Object);
            CreateDiscussionCommand        createDiscussionCommand        = new CreateDiscussionCommand(discussionDto);

            mapper.Setup(x => x.Map <Discussion>(createDiscussionCommand)).Returns(discussion);

            var result = await createDiscussionCommandHandler.Handle(createDiscussionCommand, new CancellationToken());

            result.Should().Be(id);
        }
        public void ShouldThrowValidationExceptionDuringDiscussionCreation(string testTitle, string testDescription, string testPostBody)
        {
            //arrange
            var command = new CreateDiscussionCommand()
            {
                Id          = Guid.NewGuid(),
                Title       = testTitle,
                Description = testDescription,
                PostBody    = testPostBody
            };

            //act

            //assert
            FluentActions.Invoking(() =>
                                   SendAsync(command)).Should().Throw <Common.Exceptions.ValidationException>();
        }
Exemplo n.º 5
0
        public async Task ShouldThrowValidationExceptionDuringDiscussionCreation(string testTitle, string testDescription)
        {
            //arrange
            var loggedUser = await RunAsUserAsync("scott101@localhost", "Pa$$w0rd!");

            var createdDiscussionId = await SendAsync(new Faker <CreateDiscussionCommand>("en")
                                                      .RuleFor(d => d.Id, f => f.Random.Guid())
                                                      .RuleFor(d => d.Title, f => f.Lorem.Sentence(wordCount: 3))
                                                      .RuleFor(d => d.Description, f => f.Lorem.Sentences(sentenceCount: 2))
                                                      .RuleFor(d => d.PostBody, f => f.Lorem.Paragraph()).Generate());

            var editCommand = new CreateDiscussionCommand()
            {
                Id          = createdDiscussionId,
                Title       = testTitle,
                Description = testDescription
            };

            //act

            //assert
            FluentActions.Invoking(() =>
                                   SendAsync(editCommand)).Should().ThrowAsync <Common.Exceptions.ValidationException>();
        }
Exemplo n.º 6
0
 public async Task <IActionResult> Create([FromBody] CreateDiscussionCommand command)
 {
     return(HandleResult(await Mediator.Send(command)));
 }