Exemplo n.º 1
0
        public TaskAssignment PostTaskAssignment(int id, TaskAssignment taskAssignment)
        {
            var user = _context.Users.SingleOrDefault(x => x.Id == id);

            if (user == null)
            {
                HttpContext.Response.StatusCode = 404;
                return(null);
            }

            if (!taskAssignment.IsValid())
            {
                HttpContext.Response.StatusCode = 400;
                return(null);
            }

            taskAssignment.CreatedBy = id;
            taskAssignment.setStatus(0);
            taskAssignment.CreatedAt = DateTime.UtcNow;

            _context.TaskAssignments.Add(taskAssignment);
            _context.SaveChanges();

            _context.TaskLogs.Add(new TaskLog()
            {
                TaskAssignmentId = taskAssignment.Id,
                UserId           = id,
                TaskLogType      = Enums.TaskLogType.ADDED,
                CreatedAt        = DateTime.UtcNow,
                Title            = "Task '" + taskAssignment.Title + "' created from '" + user.NickName + "'",
                Description      = ""
            });
            _context.SaveChanges();

            return(taskAssignment);
        }
Exemplo n.º 2
0
        public TaskAssignment PutTaskAssignment(int id, int taskAssignmentId, TaskAssignment taskAssignment)
        {
            var user = _context.Users.SingleOrDefault(x => x.Id == id);

            if (user == null)
            {
                HttpContext.Response.StatusCode = 404;
                return(null);
            }

            var editTask = _context.TaskAssignments.Include(x => x.TaskAssignmentGroup).Include(x => x.TaskAssignmentUsers).SingleOrDefault(x => x.Id == taskAssignmentId);

            if (editTask == null)
            {
                HttpContext.Response.StatusCode = 404;
                return(null);
            }

            if (!taskAssignment.IsValid())
            {
                HttpContext.Response.StatusCode = 400;
                return(null);
            }


            var logType          = Enums.TaskLogType.EDIT;
            var notificationType = Enums.NotificationType.EDIT_TASK;
            var logDescr         = "";

            if (editTask.Priority != taskAssignment.Priority)
            {
                logType          = Enums.TaskLogType.CHANGED_PRIORITY;
                notificationType = Enums.NotificationType.CHANGED_TASK_PRIORITY;
                logDescr         = "Priority changed from " + Enum.GetName(typeof(Enums.TaskAssignmentPriority), editTask.Priority) + " to " + Enum.GetName(typeof(Enums.TaskAssignmentPriority), taskAssignment.Priority);
            }
            else if (editTask.Status != taskAssignment.Status)
            {
                logType          = Enums.TaskLogType.CHANGED_STATUS;
                notificationType = Enums.NotificationType.CHANGED_TASK_STATUS;
                logDescr         = "Status changed from " + Enum.GetName(typeof(Enums.TaskAssignmentStatus), editTask.Status) + " to " + Enum.GetName(typeof(Enums.TaskAssignmentStatus), taskAssignment.Status);
            }
            else if (editTask.Deadline != taskAssignment.Deadline)
            {
                logType          = Enums.TaskLogType.CHANGED_DEADLINE;
                notificationType = Enums.NotificationType.CHANGED_TASK_DEADLINE;
                logDescr         = "Deadline changed from " + editTask.Deadline == null ? "" : editTask.Deadline.ToString() + " to " + taskAssignment.Deadline == null ? "" : taskAssignment.Deadline.ToString();
            }
            else if (editTask.TaskAssignmentGroupId != taskAssignment.TaskAssignmentGroupId)
            {
                logType          = Enums.TaskLogType.CHANGED_TASK_GROUP;
                notificationType = Enums.NotificationType.CHANGED_TASK_GROUP;
                logDescr         = "Task moved from group '" + editTask.TaskAssignmentGroup.Name + "' to '" + taskAssignment.TaskAssignmentGroup.Name + "'";
            }

            editTask.Title       = taskAssignment.Title;
            editTask.Description = taskAssignment.Description;
            editTask.Priority    = taskAssignment.Priority;
            editTask.Status      = taskAssignment.Status;
            editTask.Deadline    = taskAssignment.Deadline;

            editTask.TaskAssignmentGroupId = taskAssignment.TaskAssignmentGroupId;


            foreach (var taskAssignmentUser in editTask.TaskAssignmentUsers)
            {
                _context.UserNotifications.Add(new UserNotification()
                {
                    UserId           = taskAssignmentUser.UserId,
                    NotificationType = notificationType,
                    CreatedAt        = DateTime.UtcNow,
                    Title            = "Edit Task Assignment",
                    Description      = "The task with name " + editTask.Title + " has been edit from user '" + user.NickName + "'."
                });
            }

            _context.TaskLogs.Add(new TaskLog()
            {
                TaskAssignmentId = editTask.Id,
                UserId           = id,
                TaskLogType      = logType,
                CreatedAt        = DateTime.UtcNow,
                Title            = "Task '" + editTask.Title + "' edit from '" + user.NickName + "'",
                Description      = logDescr
            });
            _context.SaveChanges();

            return(editTask);
        }