diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index d65d1f94dc4d1e266f65dfceffa7f050518356ca..cb90fae29fb465e9298dddbe750edfbf5e5677c3 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -179,6 +179,9 @@ namespace Tsi1.Api.Controllers [HttpPut("Modify/{courseId}")] public async Task<IActionResult> Modify(int courseId, CourseCreateDto courseDto) { + var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); + courseDto.TenantId = tenantId; + var result = await _courseService.Modify(courseId, courseDto); if (result.HasError) diff --git a/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs b/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs index 8b4373aca7795d7fb930d12ff41d1a2caf36babe..92a0665ffad2a0892a1365f1b16fbbf9730a92e6 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs @@ -94,5 +94,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 9e1822f19be6d557dff520dc4536b3c11df1e53f..00a67f4141fbdb357d1dc3552ed02951b200b20c 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<File, FileDto>(); @@ -49,6 +50,7 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<CoursePreviewDto, Course>(); CreateMap<TenantPreviewDto, Tenant>(); CreateMap<TenantCreateDto, Tenant>(); + CreateMap<TenantCourseDto, Tenant>(); CreateMap<UserTypeDto, UserType>(); CreateMap<FileDto, File>(); } 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 a54ceb12faeba753c7ca67f8bbda3bb27c0ded54..a1b88ed057e0435a0c6ff68901bdccd86d235ca4 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs @@ -20,5 +20,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 d749ea887b6b46b53f3759e5d1c0d1fa9b9441f7..17d6668ac6a2234260d9a64fb1007dcdeefa7437 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; @@ -123,6 +124,58 @@ 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; + } + public async Task<ServiceResult<int>> GetById(int tenantId) { var result = new ServiceResult<int>();