Skip to content
Snippets Groups Projects
Commit efb337d0 authored by Enzo Santangelo Dodera's avatar Enzo Santangelo Dodera
Browse files

Merge branch 'feature/fix-communications' into 'develop'

fix GetMyCommunications and add 2 endpoints

See merge request !27
parents 0e764a59 67f8566c
No related branches found
No related tags found
2 merge requests!31Develop,!27fix GetMyCommunications and add 2 endpoints
Pipeline #10395 passed
......@@ -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)
{
......
......@@ -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);
}
......
......@@ -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>();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment