Esempio n. 1
0
        public async Task <ActionResult <AssignmentTaskDTO> > PostTask(AssignmentTaskCreateRequest taskRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var assignmentTask = taskMapper.MapTaskCreateRequest(taskRequest);

            var assginment = await _context.Assignments.FindAsync(assignmentTask.AssignmentId);

            if (ShouldCustomAssignStudent(taskRequest, assginment))
            {
                var studentUser = await _context.Students.FirstOrDefaultAsync(s => s.UserId == taskRequest.AssignedTo);

                if (studentUser is null)
                {
                    ModelState.AddModelError("assignedTo", "The selected student to be assigned could not be found.");
                    return(BadRequest(ModelState));
                }

                assignmentTask.StudentsAssigned.Add(new StudentAssignedTask {
                    State     = StudentAssignedTaskState.Assigned,
                    Task      = assignmentTask,
                    StudentId = taskRequest.AssignedTo
                });
            }

            _context.AssignmentTasks.Add(assignmentTask);
            await _context.SaveChangesAsync();

            return(taskMapper.MapTask(assignmentTask));
        }
Esempio n. 2
0
 public AssignmentTask MapTaskCreateRequest(AssignmentTaskCreateRequest task)
 {
     return(new AssignmentTask {
         Id = Guid.Empty,
         AssignmentId = task.AssignmentId,
         Name = task.Name,
         Description = task.Description
     });
 }
Esempio n. 3
0
 private bool ShouldCustomAssignStudent(AssignmentTaskCreateRequest taskRequest, Assignment assginment)
 {
     return(assginment.Type == AssignmentType.CustomAssignedTasks && !string.IsNullOrWhiteSpace(taskRequest.AssignedTo));
 }