Example #1
0
        public IssueViewModel Add(string authorId, IssueBindingModel model)
        {
            var issue = mapper.Map <IssueBindingModel, Issue>(model);

            var issueProject = this.data.ProjectRepository.GetById(issue.ProjectId)
                               .Include(p => p.Issues)
                               .Include(i => i.ProjectLabels)
                               .Include(p => p.ProjectPriorities.Select(pp => pp.Priority))
                               .FirstOrDefault();

            if (issueProject == null)
            {
                throw new ArgumentException(Constants.UnexistingProjectErrorMessage);
            }

            var user = this.data.UserRepository.GetById(authorId);

            if (issueProject.LeadId != user.Id && !user.isAdmin)
            {
                throw new InvalidOperationException(Constants.NotProjectLeaderOrAdmin);
            }

            var issueAssignee = this.data.UserRepository.GetById(model.AssigneeId);

            if (issueAssignee == null)
            {
                throw new ArgumentException(Constants.UnexistingUserErrorMessage);
            }

            var issuePriority =
                this.data.ProjectPrioritiesRepository.Get(pp => pp.ProjectId == issue.ProjectId &&
                                                          pp.PriorityId == issue.PriorityId).FirstOrDefault();

            if (issuePriority == null)
            {
                throw new ArgumentException(Constants.UnexistingPriorityForProjectErrorMessage);
            }

            var projectIssuesCount = issueProject.Issues.Count;

            issue.IssueKey = issueProject.ProjectKey + "-" + ++projectIssuesCount;

            issue.AuthorId = authorId;
            SelectParentStatusInTransitionScheme(issueProject.TransitionSchemeId, issue);

            if (model.Labels != null)
            {
                AddLabels(model.Labels, issue);
            }

            this.data.IssueRepository.Insert(issue);
            this.data.Save();

            return(GetMappedIssueWithAvailableStatuses(issue));
        }
        public IHttpActionResult Add(IssueBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(ModelState));
            }

            var userId = RequestContext.Principal.Identity.GetUserId();
            var issue  = this.issuesService.Add(userId, model);

            return(CreatedAtRoute("GetIssueById", new { id = issue.Id }, issue));
        }