public async Task <IActionResult> CreateAssignments([FromBody] CreateIssueAssignmentsViewModel model, int issueId)
        {
            foreach (int id in model.UserIdList)
            {
                var assignment = new IssueAssignment
                {
                    AssigneeId = id,
                    IssueId    = issueId,
                    RoleId     = model.RoleId
                };

                Database.IssueAssignment.Add(assignment);
            }

            int userId = int.Parse(User.Claims.First(c => c.Type == "UserId").Value);

            var user = Database.User
                       .Where(u => u.Id == userId)
                       .FirstOrDefault();

            var issue = Database.Issue
                        .Include(i => i.ProjectVersion)
                        .Where(i => i.Id == issueId)
                        .FirstOrDefault();

            string roleName = Database.IssueAssignmentRole
                              .Where(r => r.Id == model.RoleId)
                              .Select(r => r.Name)
                              .FirstOrDefault();

            await Database.SaveChangesAsync();

            ProjectActivity activity = new ProjectActivity
            {
                ProjectId = issue.ProjectVersion.ProjectId,
                AuthorId  = userId,
                Content   = $"{user.FirstName} {user.LastName} was assigned as {roleName} on issue '#{issue.Id}: {issue.Subject}'"
            };

            Database.ProjectActivity.Add(activity);

            await Database.SaveChangesAsync();

            return(Json(new
            {
                status = true,
                url = Url.Action("Assignments")
            }));
        }
        public ActionResult Create([Bind(Exclude = "IssueAssignmentID")] IssueAssignment newIssueAssignment)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            try
            {
                _dataModel.AddToIssueAssignments(newIssueAssignment);
                _dataModel.SaveChanges();

                return(RedirectToAction("Index", "Issue"));
            }
            catch
            {
                return(RedirectToAction("Index", "Issue"));
            }
        }
        //
        // GET: /IssueAssignment/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                IssueAssignment issueAsgnmt = GetIssueAssignmentByID(id);

                if (issueAsgnmt == null)
                {
                    return(RedirectToAction("Index", "Issue"));
                }
                else
                {
                    return(View(issueAsgnmt));
                }
            }
            catch
            {
                return(RedirectToAction("Index", "Issue"));
            }
        }
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                IssueAssignment issueAsgnmt = GetIssueAssignmentByID(id);

                if (issueAsgnmt == null)
                {
                    return(RedirectToAction("Index", "Issue"));
                }

                _dataModel.DeleteObject(issueAsgnmt);
                _dataModel.SaveChanges();

                return(RedirectToAction("Index", "Issue"));
            }
            catch
            {
                return(RedirectToAction("Index", "Issue"));
            }
        }
        public async Task <IActionResult> Create(CreateIssueViewModel model, string projectUrl, string versionUrl)
        {
            if (ModelState.IsValid)
            {
                int userId = int.Parse(User.Claims.First(c => c.Type == "UserId").Value);

                if (model.StartDate > model.DueDate)
                {
                    ModelState.AddModelError("DueDate", "Due date cannot be before start date.");
                    return(View(model));
                }

                var version = Database.ProjectVersion
                              .Include(v => v.Project)
                              .Where(v => v.Url == versionUrl && v.Project.Url == projectUrl)
                              .FirstOrDefault();

                if (model.StartDate < version.StartDate)
                {
                    ModelState.AddModelError("StartDate", $"Start date must be after or equal {version.StartDate:dd.MM.yy}");
                    return(View(model));
                }
                if (model.DueDate > version.DueDate)
                {
                    ModelState.AddModelError("DueDate", $"Due date must be before or equal {version.DueDate:dd.MM.yy}");
                    return(View(model));
                }

                Issue issue = new Issue
                {
                    Description      = model.Description,
                    Done             = model.Done,
                    DueDate          = model.DueDate,
                    EstimatedTime    = model.EstimatedTime,
                    ParentIssueId    = model.ParentIssueId == 0 ? (int?)null : model.ParentIssueId,
                    PriorityId       = model.PriorityId,
                    StartDate        = model.StartDate,
                    StatusId         = model.StatusId,
                    Subject          = model.Subject,
                    ProjectVersionId = version.Id,
                    TypeId           = model.TypeId
                };
                Database.Issue.Add(issue);

                int roleId = Database.IssueAssignmentRole
                             .Where(r => r.Name == "Creator")
                             .Select(r => r.Id)
                             .FirstOrDefault();

                IssueAssignment assignment = new IssueAssignment
                {
                    AssigneeId = userId,
                    IssueId    = issue.Id,
                    RoleId     = roleId
                };
                Database.IssueAssignment.Add(assignment);

                var user = Database.User
                           .Where(u => u.Id == userId)
                           .FirstOrDefault();

                await Database.SaveChangesAsync();

                ProjectActivity activity = new ProjectActivity
                {
                    ProjectId = version.ProjectId,
                    AuthorId  = userId,
                    Content   = $"Created issue '#{issue.Id}: {issue.Subject}'"
                };
                Database.ProjectActivity.Add(activity);
                await Database.SaveChangesAsync();

                return(RedirectToAction("Index", "Issue", new { projectUrl = projectUrl, versionUrl = versionUrl, issueId = issue.Id }));
            }

            return(View(model));
        }