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

add endpoint GetAllCourseCommunications

parent d5dc0082
No related branches found
No related tags found
1 merge request!31Develop
......@@ -99,6 +99,22 @@ namespace Tsi1.Api.Controllers
return Ok(result.Data);
}
[Authorize(Roles = UserTypes.FacultyAdmin)]
[HttpGet("GetAllCourseCommunications")]
public async Task<IActionResult> GetAllCourseCommunications()
{
var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
var result = await _communicationService.GetAllCourseCommunications(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)
......
......@@ -21,5 +21,6 @@ namespace Tsi1.BusinessLayer.Interfaces
Task<ServiceResult<bool>> TenantValidation(int tenantId, int courseId);
Task<ServiceResult<List<CommunicationPreviewDto>>> GetAllGlobalCommunications();
Task<ServiceResult<List<CommunicationPreviewDto>>> GetAllCourseCommunications(int tenantId);
}
}
......@@ -102,6 +102,13 @@ namespace Tsi1.BusinessLayer.Services
var validation = await this.TenantValidation(tenantId, courseId);
if (validation.HasError)
{
result.HasError = true;
result.AddMessage(validation.Message);
return result;
}
var communications = await _context.Communications
.AsNoTracking()
.Include(x => x.Course)
......@@ -142,6 +149,13 @@ namespace Tsi1.BusinessLayer.Services
var tenantAdmin = await _context.Tenants.AsNoTracking().FirstOrDefaultAsync(x => x.Name == TenantAdmin.Name);
var course = await _context.Courses.AsNoTracking().FirstOrDefaultAsync(x => x.Id == courseId);
if (course == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.CourseDoesNotExist, courseId));
return result;
}
if (tenantAdmin.Id != tenantId)
{
result.HasError = course.TenantId != tenantId;
......@@ -151,6 +165,47 @@ namespace Tsi1.BusinessLayer.Services
return result;
}
public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetAllGlobalCommunications()
{
var result = new ServiceResult<List<CommunicationPreviewDto>>();
var communications = await _context.Communications
.AsNoTracking()
.Include(x => x.Tenant)
.Where(x => x.IsGlobal)
.ToListAsync();
result.Data = _mapper.Map<List<CommunicationPreviewDto>>(communications);
return result;
}
public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetAllCourseCommunications(int tenantId)
{
var result = new ServiceResult<List<CommunicationPreviewDto>>();
var tenant = await _context.Tenants.Include(x => x.Courses).FirstOrDefaultAsync(x => x.Id == tenantId);
if (tenant == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, tenantId));
}
var courseIds = tenant.Courses.Select(x => x.Id);
var communications = await _context.Communications
.AsNoTracking()
.Include(x => x.Course)
.Where(x => courseIds.Contains(x.Course.Id))
.ToListAsync();
result.Data = _mapper.Map<List<CommunicationPreviewDto>>(communications);
return result;
}
private async Task<ServiceResult<bool>> CreateValidation(int id, bool isGlobal, Communication commmunication)
{
var result = new ServiceResult<bool>();
......@@ -185,19 +240,5 @@ namespace Tsi1.BusinessLayer.Services
return result;
}
public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetAllGlobalCommunications()
{
var result = new ServiceResult<List<CommunicationPreviewDto>>();
var communications = await _context.Communications
.AsNoTracking()
.Include(x => x.Tenant)
.Where(x => x.IsGlobal)
.ToListAsync();
result.Data = _mapper.Map<List<CommunicationPreviewDto>>(communications);
return result;
}
}
}
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