From cf2778788ac5b646a4822043e1988831aa7b1058 Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Sat, 12 Dec 2020 10:56:17 -0300
Subject: [PATCH] commit

---
 .../StudentCourseResultController.cs          | 16 ++++++
 .../Dtos/StudentCourseResultQuantityDto.cs    | 19 +++++++
 .../Interfaces/IStudentCourseResultService.cs |  1 +
 .../Services/StudentCourseResultService.cs    | 50 +++++++++++++++++++
 4 files changed, 86 insertions(+)
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentCourseResultQuantityDto.cs

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/StudentCourseResultController.cs b/Tsi1.Api/Tsi1.Api/Controllers/StudentCourseResultController.cs
index 7767d5b..0ad2976 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 0000000..c3b8e57
--- /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 5fdec18..c590fff 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 1f2a16d..6e4b0d1 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;
+        }
     }
 }
-- 
GitLab