diff --git a/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs b/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs index 738bc5b34acb2810373bc8ac6902096ef52bb9bb..f37f044f4c36a08560c5a66bb439feade470bf58 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 de761a5eb908f333c2ceb7c394341f9febb165c8..c4a7e56fa3c89bcfbddfeb21ddf9156e329bcd93 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 0000000000000000000000000000000000000000..549efe2b0f218f6bf7b989d7fc9c7318bf72e619 --- /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() + { + Courses = new List<CoursePreviewDto>(); + } + + public int Id { get; set; } + + public string Name { get; set; } + + public List<CoursePreviewDto> Courses { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantPreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/TenantPreviewDto.cs index 74b8300acafec93c16aac87e34da1af457055766..273d9f5b5deedc458ab30fdb2333a594a889f731 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 f23738a577da9427cd53b0c75d945b93d3dee4b1..f41ff864c4887ea8922e73442f9050233e10713a 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>().ForMember(x => x.Courses, opt => opt.Ignore()); 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 0000000000000000000000000000000000000000..4672c8518bed0f807b1b449625ee79209bc5dddb --- /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 ea42bbcb4fa8850ad5a0e458dec57fd3608704b0..26c6c000930203ca3051ca38b142bde7cd1b4920 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 8f629676d9123978260602c3892aba1ce68e2c6d..71bd3c28acd1093aa04d0a982d31c0768fbc59df 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs @@ -1,7 +1,8 @@ -using AutoMapper; +using AutoMapper; 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(); + + var 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(); + + var 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.Courses.Add(courseDto); + } + + tenantDtos.Add(tenantDto); + } + + result.Data = tenantDtos; + return result; + } } }