Exemple #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));
        }
        public StudentAssignmentDTO MapStudentAssignment(Assignment assignment, User user)
        {
            var dto = new StudentAssignmentDTO();

            MapAssignmentInternal(dto, assignment);

            var assignedTask = assignment.AssignmentTasks.FirstOrDefault(t => t.StudentsAssigned.Any(sa => sa.StudentId == user.Id));

            if (!(assignedTask is null))
            {
                dto.AssignedTask = taskMapper.MapTask(assignedTask);
            }

            return(dto);
        }