Skip to content
Snippets Groups Projects
Commit 67f8566c authored by esantangelo's avatar esantangelo
Browse files

fix GetMyCommunications and add 2 endpoints

parent f76622a0
No related branches found
No related tags found
2 merge requests!31Develop,!27fix GetMyCommunications and add 2 endpoints
...@@ -71,8 +71,45 @@ namespace Tsi1.Api.Controllers ...@@ -71,8 +71,45 @@ namespace Tsi1.Api.Controllers
public async Task<IActionResult> GetMyCommunications() public async Task<IActionResult> GetMyCommunications()
{ {
var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); 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) if (result.HasError)
{ {
......
...@@ -13,7 +13,11 @@ namespace Tsi1.BusinessLayer.Interfaces ...@@ -13,7 +13,11 @@ namespace Tsi1.BusinessLayer.Interfaces
Task<ServiceResult<bool>> Delete(int communicationId); 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); Task<ServiceResult<bool>> TenantValidation(int tenantId, int courseId);
} }
......
...@@ -62,11 +62,19 @@ namespace Tsi1.BusinessLayer.Services ...@@ -62,11 +62,19 @@ namespace Tsi1.BusinessLayer.Services
return result; 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 result = new ServiceResult<List<CommunicationPreviewDto>>();
var user = await _context.Users.AsNoTracking().FirstOrDefaultAsync(x => x.Id == userId); 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 var courseIds = await _context.StudentCourses
.AsNoTracking() .AsNoTracking()
.Where(x => x.StudentId == user.StudentId) .Where(x => x.StudentId == user.StudentId)
...@@ -77,7 +85,7 @@ namespace Tsi1.BusinessLayer.Services ...@@ -77,7 +85,7 @@ namespace Tsi1.BusinessLayer.Services
.AsNoTracking() .AsNoTracking()
.Include(x => x.Tenant) .Include(x => x.Tenant)
.Include(x => x.Course) .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) && x.ValidUntil >= DateTime.Now)
.ToListAsync(); .ToListAsync();
...@@ -86,6 +94,53 @@ namespace Tsi1.BusinessLayer.Services ...@@ -86,6 +94,53 @@ namespace Tsi1.BusinessLayer.Services
return result; 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) public async Task<ServiceResult<bool>> TenantValidation(int tenantId, int courseId)
{ {
var result = new ServiceResult<bool>(); var result = new ServiceResult<bool>();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment