From 294c6a7656e61a021968781e294d47be2946ae0e Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Sat, 31 Oct 2020 23:10:45 -0300
Subject: [PATCH] add 2 endpoints for report

---
 .../Tsi1.Api/Controllers/TenantController.cs  | 28 ++++++++++
 .../Dtos/CoursePreviewDto.cs                  |  1 +
 .../Dtos/TenantCourseDto.cs                   | 20 +++++++
 .../Dtos/TenantPreviewDto.cs                  |  2 +
 .../Helpers/MappingProfile.cs                 |  2 +
 .../Tsi1.BusinessLayer/Helpers/TenantAdmin.cs | 11 ++++
 .../Interfaces/ITenantService.cs              |  4 ++
 .../Services/TenantService.cs                 | 53 +++++++++++++++++++
 8 files changed, 121 insertions(+)
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantCourseDto.cs
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Helpers/TenantAdmin.cs

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs b/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs
index 738bc5b..f37f044 100644
--- a/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs
+++ b/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs
@@ -87,5 +87,33 @@ namespace Tsi1.Api.Controllers
 
             return Ok();
         }
+
+        [Authorize(Roles = UserTypes.UdelarAdmin)]
+        [HttpGet("FacultyList")]
+        public async Task<IActionResult> FacultyList()
+        {
+            var result = await _tenantService.FacultyList();
+
+            if (result.HasError)
+            {
+                return BadRequest(result.Message);
+            }
+
+            return Ok(result.Data);
+        }
+
+        [Authorize(Roles = UserTypes.UdelarAdmin)]
+        [HttpGet("CourseList")]
+        public async Task<IActionResult> CourseList()
+        {
+            var result = await _tenantService.CourseList();
+
+            if (result.HasError)
+            {
+                return BadRequest(result.Message);
+            }
+
+            return Ok(result.Data);
+        }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CoursePreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CoursePreviewDto.cs
index de761a5..c4a7e56 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CoursePreviewDto.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CoursePreviewDto.cs
@@ -8,5 +8,6 @@ namespace Tsi1.BusinessLayer.Dtos
     {
         public int Id { get; set; }
         public string Name { get; set; }
+        public int StudentQuantity { get; set; }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantCourseDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantCourseDto.cs
new file mode 100644
index 0000000..aba2faa
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantCourseDto.cs
@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Tsi1.BusinessLayer.Dtos
+{
+    public class TenantCourseDto
+    {
+        public TenantCourseDto()
+        {
+            CourseDtos = new List<CoursePreviewDto>();
+        }
+
+        public int Id { get; set; }
+
+        public string Name { get; set; }
+
+        public List<CoursePreviewDto> CourseDtos { get; set; }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantPreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantPreviewDto.cs
index 74b8300..273d9f5 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantPreviewDto.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantPreviewDto.cs
@@ -9,5 +9,7 @@ namespace Tsi1.BusinessLayer.Dtos
         public int Id { get; set; }
 
         public string Name { get; set; }
+
+        public int StudentQuantity { get; set; }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
index f23738a..6ff26fc 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
@@ -29,6 +29,7 @@ namespace Tsi1.BusinessLayer.Helpers
             CreateMap<Course, CoursePreviewDto>();
             CreateMap<Tenant, TenantPreviewDto>();
             CreateMap<Tenant, TenantCreateDto>();
+            CreateMap<Tenant, TenantCourseDto>();
             CreateMap<UserType, UserTypeDto>();
 
             CreateMap<ForumCreateDto, Forum>();
@@ -48,6 +49,7 @@ namespace Tsi1.BusinessLayer.Helpers
             CreateMap<CoursePreviewDto, Course>();
             CreateMap<TenantPreviewDto, Tenant>();
             CreateMap<TenantCreateDto, Tenant>();
+            CreateMap<TenantCourseDto, Tenant>();
             CreateMap<UserTypeDto, UserType>();
         }
     }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/TenantAdmin.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/TenantAdmin.cs
new file mode 100644
index 0000000..4672c85
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/TenantAdmin.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Tsi1.BusinessLayer.Helpers
+{
+    public static class TenantAdmin
+    {
+        public const string Name = "admin";
+    }
+}
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs
index ea42bbc..26c6c00 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs
@@ -18,5 +18,9 @@ namespace Tsi1.BusinessLayer.Interfaces
         Task<ServiceResult<bool>> Modify(int tenantId, TenantCreateDto tenantDto);
 
         Task<ServiceResult<bool>> Delete(int tenantId);
+
+        Task<ServiceResult<List<TenantPreviewDto>>> FacultyList();
+
+        Task<ServiceResult<List<TenantCourseDto>>> CourseList();
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs
index 8f62967..c109241 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs
@@ -2,6 +2,7 @@
 using Microsoft.EntityFrameworkCore;
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using Tsi1.BusinessLayer.Dtos;
@@ -122,5 +123,57 @@ namespace Tsi1.BusinessLayer.Services
 
             return result;
         }
+
+        public async Task<ServiceResult<List<TenantPreviewDto>>> FacultyList()
+        {
+            var result = new ServiceResult<List<TenantPreviewDto>>();
+
+            var tenants = await _context.Tenants
+                .Include(x => x.Students)
+                .Where(x => x.Name != TenantAdmin.Name)
+                .ToListAsync();
+
+            List<TenantPreviewDto> tenantDtos = new List<TenantPreviewDto>();
+            foreach (var tenant in tenants)
+            {
+                var tenantDto = _mapper.Map<TenantPreviewDto>(tenant);
+                tenantDto.StudentQuantity = tenant.Students.Count();
+
+                tenantDtos.Add(tenantDto);
+            }
+
+            result.Data = tenantDtos;
+            return result;
+        }
+
+        public async Task<ServiceResult<List<TenantCourseDto>>> CourseList()
+        {
+            var result = new ServiceResult<List<TenantCourseDto>>();
+
+            var tenants = await _context.Tenants
+                .Include(x => x.Courses)
+                    .ThenInclude(x => x.StudentCourses)
+                .Where(x => x.Name != TenantAdmin.Name)
+                .ToListAsync();
+
+            List<TenantCourseDto> tenantDtos = new List<TenantCourseDto>();
+            foreach (var tenant in tenants)
+            {
+                var tenantDto = _mapper.Map<TenantCourseDto>(tenant);
+
+                foreach (var course in tenant.Courses)
+                {
+                    var courseDto = _mapper.Map<CoursePreviewDto>(course);
+                    courseDto.StudentQuantity = course.StudentCourses.Count();
+
+                    tenantDto.CourseDtos.Add(courseDto);
+                }
+
+                tenantDtos.Add(tenantDto);
+            }
+
+            result.Data = tenantDtos;
+            return result;
+        }
     }
 }
-- 
GitLab