diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index 824870e326c4b18ce0ca7c2c00bb33b7a040d586..47e734450fdf6b3b2b3b42136635a90a461a2335 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -231,7 +231,7 @@ namespace Tsi1.Api.Controllers return Ok(); } - + [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor + ", " + UserTypes.FacultyAdmin + ", " + UserTypes.UdelarAdmin)] [HttpGet("GetProfessors/{courseId}")] public async Task<IActionResult> GetProfessors(int courseId) { @@ -244,6 +244,19 @@ namespace Tsi1.Api.Controllers return Ok(result.Data); } + [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor + ", " + UserTypes.FacultyAdmin + ", " + UserTypes.UdelarAdmin)] + [HttpGet("GetStudents/{courseId}")] + public async Task<IActionResult> GetStudents(int courseId) + { + var result = await _courseService.GetStudents(courseId); + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(result.Data); + } + [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor + ", " + UserTypes.FacultyAdmin)] [HttpGet("GetById/{courseId}")] public async Task<IActionResult> GetById(int courseId) diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs index 846f11c2a36b56d22cb06a342fc16dcb69a00295..7aa31a8b8058df12c9c5b7b03e05753cd6c6ced2 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs @@ -27,5 +27,6 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<bool>> RemoveProfessorToCourse(ProfessorCourseDto professorCourseDto); Task<ServiceResult<List<UserPreviewDto>>> GetProfessors(int courseId); Task<ServiceResult<CourseDetailDto>> GetById(int courseId); + Task<ServiceResult<List<UserPreviewDto>>> GetStudents(int courseId); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs index 92d6ffc019139a29b94edb392771106a75bb2d26..93cfa7828df1f343bc648bfa2660df590460b5ac 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs @@ -427,5 +427,22 @@ namespace Tsi1.BusinessLayer.Services return result; } + + public async Task<ServiceResult<List<UserPreviewDto>>> GetStudents(int courseId) + { + var result = new ServiceResult<List<UserPreviewDto>>(); + + var users = await _context.StudentCourses + .Include(x => x.Student) + .ThenInclude(x => x.User) + .Where(x => x.CourseId == courseId) + .Select(x => x.Student.User) + .ToListAsync(); + + var userDtos = _mapper.Map<List<UserPreviewDto>>(users); + + result.Data = userDtos; + return result; + } } }