diff --git a/Tsi1.Api/Tsi1.Api/Controllers/StudentCourseResultController.cs b/Tsi1.Api/Tsi1.Api/Controllers/StudentCourseResultController.cs index 7767d5b87727735ca8caa605ab26fd7bcb55fb6b..0ad29768ae9e96116dc2e3a52bd2314a70119bcd 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/StudentCourseResultController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/StudentCourseResultController.cs @@ -69,6 +69,22 @@ namespace Tsi1.Api.Controllers return Ok(result.Data); } + + [Authorize(Roles = UserTypes.FacultyAdmin)] + [HttpGet("GetAllStudentCourseResults")] + public async Task<IActionResult> GetAllStudentCourseResults() + { + var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); + + var result = await _studentCourseResultService.GetAllLastStudentCourseResults(tenantId); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(result.Data); + } [Authorize(Roles = UserTypes.FacultyAdmin + ", " + UserTypes.Professor)] [HttpPost("Create")] diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentCourseResultQuantityDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentCourseResultQuantityDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..c3b8e5770fb25dfcd932edb491edb58386545857 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentCourseResultQuantityDto.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class StudentCourseResultQuantityDto + { + public int CourseId { get; set; } + + public string CourseName { get; set; } + + public int Total{ get; set; } + + public int Approved { get; set; } + + public int Disapproved { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IStudentCourseResultService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IStudentCourseResultService.cs index 5fdec183e893622ae5a4ca376d12e8ef2bb828ba..c590fff1f8b74ed4571a015ce26bb2ce3587389d 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IStudentCourseResultService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IStudentCourseResultService.cs @@ -18,5 +18,6 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<StudentCourseResultPreviewDto>> GetMyLastResult(int courseId, int userId); Task<ServiceResult<List<StudentCourseResultPreviewDto>>> GetLastStudentCourseResults(int courseId, int userId); + Task<ServiceResult<List<StudentCourseResultQuantityDto>>> GetAllLastStudentCourseResults(int tenantId); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/StudentCourseResultService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/StudentCourseResultService.cs index 1f2a16dca15d27e8ec3a6761c5896f3f3e7a2937..6e4b0d1e69f7fa12058438ed710f4e41fbd55944 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/StudentCourseResultService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/StudentCourseResultService.cs @@ -303,5 +303,55 @@ namespace Tsi1.BusinessLayer.Services result.Data = (int)user.StudentId; return result; } + + public async Task<ServiceResult<List<StudentCourseResultQuantityDto>>> GetAllLastStudentCourseResults(int tenantId) + { + var result = new ServiceResult<List<StudentCourseResultQuantityDto>>(); + + var courses = await _context.Courses.Where(x => x.TenantId == tenantId).ToListAsync(); + + if (courses == null) + { + return result; + } + + var studentCourseResultDtos = new List<StudentCourseResultQuantityDto>(); + + foreach (var course in courses) + { + var studentIds = await _context.StudentCourses + .AsNoTracking() + .Where(x => x.CourseId == course.Id) + .Select(x => x.StudentId) + .ToListAsync(); + + var auxStudentCourseResults = await _context.StudentCourseResults + .AsNoTracking() + .Where(x => x.CourseId == course.Id && studentIds.Contains(x.StudentId)) + .ToListAsync(); + + var studentCourseResults = auxStudentCourseResults.GroupBy(x => new { x.StudentId, x.CourseId }) + .Select(x => x.OrderByDescending(g => g.Date).First()) + .ToList(); + + var total = studentCourseResults.Count(); + var approved = studentCourseResults.Where(x => x.Result >= CourseApproval.MinimumApproval).Count(); + var disapproved = total - approved; + + var studentCourseResultDto = new StudentCourseResultQuantityDto + { + CourseId = course.Id, + CourseName = course.Name, + Total = total, + Approved = approved, + Disapproved = disapproved, + }; + + studentCourseResultDtos.Add(studentCourseResultDto); + } + + result.Data = studentCourseResultDtos; + return result; + } } }