public async Task <int> Handle(CreateIssueCommand request, CancellationToken cancellationToken)
        {
            var project = await _context.Projects.Include(x => x.Issues)
                          .SingleOrDefaultAsync(x => x.Id == request.ProjectId, cancellationToken);

            if (project == null)
            {
                throw new NotFoundException($"{nameof(Project)} {request.ProjectId} not found.");
            }

            var participantsListQuery = new GetProjectParticipantsListQuery {
                ProjectId = request.ProjectId
            };
            var projectParticipantsListVm = await _mediator.Send(participantsListQuery, cancellationToken);

            var reporter = await _context.Participants.SingleOrDefaultAsync(x => x.UserId == _currentUserService.UserId, cancellationToken);

            var projectParticipantDto = _mapper.Map <ProjectParticipantDto>(reporter);

            if (projectParticipantsListVm.Participants.All(x => x.Id != projectParticipantDto.Id))
            {
                throw new DomainException($"Participants can create an issue of any type.");
            }

            var issue = new Issue(project, reporter, request.IssueType, request.Title, request.Status);

            if (request.AssigneeId.HasValue)
            {
                var assignee = await _context.Participants.SingleOrDefaultAsync(x => x.Id == request.AssigneeId, cancellationToken);

                issue.SetAssignee(assignee);
            }

            issue.SetDescription(request.Description);

            issue.AddDomainEvent(new List <DomainEvent> {
                new IssueCreatedEvent(issue)
            });

            project.AddIssue(issue);

            await _context.Issues.AddAsync(issue, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(issue.Id);
        }
示例#2
0
        public async Task ShouldReturnProjectParticipants()
        {
            // Arange
            var userId = await RunAsDefaultUserAsync();

            var owner = new Participant(userId, "firstName", "lastName");

            var project = new Project(owner, "Project Name", "Key");

            await AddAsync(project);

            var query = new GetProjectParticipantsListQuery {
                ProjectId = project.Id
            };
            // Act
            var result = await SendAsync(query);

            // Assert
            result.Participants.Should().NotBeEmpty();
            result.Participants.Should().HaveCount(project.Participants.Count);
            result.Participants.First().Name.Should().Be($"{owner.FirstName} {owner.LastName}");
            result.Participants.First().Id.Should().Be(owner.Id);
            result.Participants.First().Title.Should().Be(owner.Title);
        }
示例#3
0
        public async Task <Unit> Handle(UpdateIssueCommand request, CancellationToken cancellationToken)
        {
            var issue = await _context.Issues.FindAsync(request.Id);

            if (issue == null)
            {
                throw new NotFoundException($"{nameof(Issue)} {request.Id} not found.");
            }

            var participantsListQuery = new GetProjectParticipantsListQuery {
                ProjectId = issue.ProjectId
            };

            var projectParticipantsListVm = await _mediator.Send(participantsListQuery, cancellationToken);

            var participant = await _context.Participants.SingleOrDefaultAsync(x => x.UserId == _currentUserService.UserId, cancellationToken);

            var projectParticipantDto = _mapper.Map <ProjectParticipantDto>(participant);

            if (projectParticipantsListVm.Participants.All(x => x.Id != projectParticipantDto.Id))
            {
                throw new DomainException($"Participants can update an issue");
            }

            if (request.ProjectId != null)
            {
                var project = await _context.Projects.SingleOrDefaultAsync(x => x.Id == request.ProjectId, cancellationToken);

                if (project == null)
                {
                    throw new NotFoundException($"{nameof(Project)} {request.AssigneeId} not found.");
                }

                issue.SetProject(project);
            }

            if (request.AssigneeId != null)
            {
                var assignee = await _context.Participants.SingleOrDefaultAsync(x => x.Id == request.AssigneeId, cancellationToken);

                if (assignee == null)
                {
                    throw new NotFoundException($"{nameof(Participant)} {request.AssigneeId} not found.");
                }

                issue.SetAssignee(assignee);
            }

            if (!string.IsNullOrEmpty(request.IssueId))
            {
                issue.SetIssueId(request.IssueId);
            }

            issue.SetTitle(request.Title)
            .SetDescription(request.Description)
            .SetStatus(request.Status)
            ;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }