Exemple #1
0
        public async Task <IActionResult> Create(MergeModel model)
        {
            var assignedBy = UserMgr.GetUserId(HttpContext.User);

            Bug bug = model.Bugs;

            bug.BugCreationTimeStamp = DateTime.Now;
            bug.AssignedById         = Convert.ToInt32(assignedBy);

            _context.Add(bug);
            await _context.SaveChangesAsync();

            var user           = UserMgr.Users.FirstOrDefault(m => m.Id == bug.AssignedToId);
            var userAssignedBy = UserMgr.Users.FirstOrDefault(m => m.Id == bug.AssignedById);
            var role           = await UserMgr.GetRolesAsync(user);

            var roleAssignedBy = await UserMgr.GetRolesAsync(userAssignedBy);

            //check if the user assigned to the bug is already in the bug history model
            var userAssigned = _context.BugAssignmentHistories.FirstOrDefault(m =>
                                                                              m.AssignedToId == bug.AssignedToId && m.BugId == bug.BugId);

            var notification = new Notification
            {
                NotificationType    = NotificationTypeEnum.BugAssigned,
                NotificationMessage = $"Hey {user.FirstName}, you've" +
                                      " been assigned a new Bug by " +
                                      $"({roleAssignedBy[0]}) {userAssignedBy.UserName}!",
                ReceipientId = bug.AssignedToId,
                SenderId     = bug.AssignedById,
                BugId        = bug.BugId
            };

            _context.Add(notification);

            var bugHistory = new BugAssignmentHistory
            {
                BugId        = bug.BugId,
                BugStatus    = bug.BugStatus,
                AssignedToId = bug.AssignedToId,
                RoleName     = role[0]
            };

            if (userAssigned == null)
            {
                _context.Add(bugHistory);
            }
            else
            {
                _context.Update(bugHistory);
            }


            await _context.SaveChangesAsync();

            return(RedirectToAction("Index", "Bug"));
        }
Exemple #2
0
        /// <summary>
        /// This action method takes in the MergeModel object and assigns a new comment that
        /// will be tied to the bug it was added on.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <IActionResult> AddComment(MergeModel model)
        {
            Bug bug = model.Bugs;

            //access logged in user's User record and Role
            var userLoggedInId = Convert.ToInt32(UserMgr.GetUserId(HttpContext.User));
            var currentUser    = await UserMgr.FindByIdAsync(userLoggedInId.ToString());

            var currentUserRole = await UserMgr.GetRolesAsync(currentUser);

            var userAssignedTo = await UserMgr.FindByIdAsync(bug.AssignedToId.ToString());


            /*If the current user is adding a comment to their bug then no need
             * send out a notification.
             */
            if (userLoggedInId != bug.AssignedToId)
            {
                var notification = new Notification
                {
                    NotificationType    = NotificationTypeEnum.NewBugComment,
                    NotificationMessage = $"Hey {userAssignedTo.FirstName}, " +
                                          $"({currentUserRole[0]}) {currentUser.UserName}" +
                                          $" just added a new comment to Bug #{bug.BugId}",
                    ReceipientId = bug.AssignedToId,
                    SenderId     = bug.AssignedById,
                    BugId        = bug.BugId
                };

                _context.Add(notification);
            }


            var comments = new Comment
            {
                CommentText = model.Comment.CommentText,
                BugId       = bug.BugId,
                UserId      = userLoggedInId
            };

            _context.Add(comments);


            await _context.SaveChangesAsync();

            return(RedirectToAction("Details", "Bug", new { id = bug.BugId }));
        }