diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CommunicationController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CommunicationController.cs index 7cdda5f21891cb2f1727dc66ec91cfe8e1b486a3..49152b69e8858e801ddd1517b51f30100fa0bce5 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CommunicationController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CommunicationController.cs @@ -71,8 +71,45 @@ namespace Tsi1.Api.Controllers public async Task<IActionResult> GetMyCommunications() { var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); + var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); + + var result = await _communicationService.GetMyCommunications(userId, tenantId); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(result.Data); + } + + [Authorize(Roles = UserTypes.Student + ", " + UserTypes.FacultyAdmin + ", " + UserTypes.Professor)] + [HttpGet("GetCourseCommunications/{courseId}")] + public async Task<IActionResult> GetCourseCommunications(int courseId) + { + var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); + + var result = await _communicationService.GetCourseCommunications(courseId, tenantId); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(result.Data); + } + + [Authorize(Roles = UserTypes.Student + ", " + UserTypes.FacultyAdmin + ", " + UserTypes.Professor + ", " + UserTypes.UdelarAdmin)] + [HttpGet("GetGlobalCommunications")] + public async Task<IActionResult> GetGlobalCommunications(int tenantId) + { + var userType = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value; + if (userType != UserTypes.UdelarAdmin) + { + tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); + } - var result = await _communicationService.GetMyCommunications(userId); + var result = await _communicationService.GetGlobalCommunications(tenantId); if (result.HasError) { diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICommunicationService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICommunicationService.cs index ce39e10fe3c8219d4b17d1316779a3b066027cdc..4fa78a04a6ae0604d6baf2db30755575f6e9a33a 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICommunicationService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICommunicationService.cs @@ -13,7 +13,11 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<bool>> Delete(int communicationId); - Task<ServiceResult<List<CommunicationPreviewDto>>> GetMyCommunications(int userId); + Task<ServiceResult<List<CommunicationPreviewDto>>> GetMyCommunications(int userId, int tenantId); + + Task<ServiceResult<List<CommunicationPreviewDto>>> GetCourseCommunications(int courseId, int tenantId); + + Task<ServiceResult<List<CommunicationPreviewDto>>> GetGlobalCommunications(int tenantId); Task<ServiceResult<bool>> TenantValidation(int tenantId, int courseId); } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CommunicationService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CommunicationService.cs index 9f7e31be67ce597fbf52dd4a9af7ff9dfcb616a1..3acdbae79a68447975539a4e428f712f4ee8b60e 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CommunicationService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CommunicationService.cs @@ -62,11 +62,19 @@ namespace Tsi1.BusinessLayer.Services return result; } - public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetMyCommunications(int userId) + public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetMyCommunications(int userId, int tenantId) { var result = new ServiceResult<List<CommunicationPreviewDto>>(); var user = await _context.Users.AsNoTracking().FirstOrDefaultAsync(x => x.Id == userId); + var tenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Id == tenantId); + + if (tenant == null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, tenantId)); + } + var courseIds = await _context.StudentCourses .AsNoTracking() .Where(x => x.StudentId == user.StudentId) @@ -77,7 +85,7 @@ namespace Tsi1.BusinessLayer.Services .AsNoTracking() .Include(x => x.Tenant) .Include(x => x.Course) - .Where(x => (x.Tenant.Id == userId || courseIds.Contains(x.Course.Id)) + .Where(x => (x.Tenant.Id == tenantId || courseIds.Contains(x.Course.Id)) && x.ValidUntil >= DateTime.Now) .ToListAsync(); @@ -86,6 +94,53 @@ namespace Tsi1.BusinessLayer.Services return result; } + public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetCourseCommunications(int courseId, int tenantId) + { + var result = new ServiceResult<List<CommunicationPreviewDto>>(); + + var tenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Id == tenantId); + + if (tenant == null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, tenantId)); + } + + var validation = await this.TenantValidation(tenantId, courseId); + + var communications = await _context.Communications + .AsNoTracking() + .Include(x => x.Course) + .Where(x => (x.Course.Id == courseId)) + .ToListAsync(); + + result.Data = _mapper.Map<List<CommunicationPreviewDto>>(communications); + + return result; + } + + public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetGlobalCommunications(int tenantId) + { + var result = new ServiceResult<List<CommunicationPreviewDto>>(); + var tenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Id == tenantId); + + if (tenant == null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, tenantId)); + } + + var communications = await _context.Communications + .AsNoTracking() + .Include(x => x.Tenant) + .Where(x => (x.Tenant.Id == tenantId)) + .ToListAsync(); + + result.Data = _mapper.Map<List<CommunicationPreviewDto>>(communications); + + return result; + } + public async Task<ServiceResult<bool>> TenantValidation(int tenantId, int courseId) { var result = new ServiceResult<bool>();