diff --git a/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs b/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs index 9ffbc2b3037b88c37929ffa00ef8036999aafa3a..b66e243b45edcf4a85d8e1ea42e14e6f2c768501 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/TenantController.cs @@ -161,5 +161,21 @@ namespace Tsi1.Api.Controllers return Ok(result.Data); } + + [Authorize(Roles = UserTypes.FacultyAdmin + ", " + UserTypes.Professor)] + [HttpGet("CourseAttendance/{courseId}")] + public async Task<IActionResult> CourseAttendance(int courseId) + { + var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); + + var result = await _tenantService.CourseAttendance(tenantId, courseId); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(result.Data); + } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseAttendanceDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseAttendanceDto.cs index 608b1cdc3c112e139f2789ee499bfd3ba54f7ae0..65325062748b67a48cb62b5e5172705e7b1e5e94 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseAttendanceDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseAttendanceDto.cs @@ -10,6 +10,8 @@ namespace Tsi1.BusinessLayer.Dtos public string Name { get; set; } + public int ActivityQuantity { get; set; } + public List<UserAttendanceDto> Users { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs index 4390132671345531ee26f7a02b93ccda724d30f4..8c6ae9d609e8284e1abd55d82c74db9f76db5983 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ITenantService.cs @@ -29,5 +29,6 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<List<RegistrationDto>>> CourseStudentQuantity(int tenantId); Task<ServiceResult<List<CourseAttendanceDto>>> CourseAttendance(int tenantId); + Task<ServiceResult<CourseAttendanceDto>> CourseAttendance(int tenantId, int courseId); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs index 2a41d0d352248bc47e19f1a1430a644396a11f83..38fbb0025a20753fb91c308bf7b615a1143b0918 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/TenantService.cs @@ -261,7 +261,7 @@ namespace Tsi1.BusinessLayer.Services var attendances = await _context.Attendances .AsNoTracking() .Include(x => x.Activity) - .Where(x => x.User.TenantId == tenantId) + .Where(x => x.User.TenantId == tenantId && x.Activity.IsVideoConference) .ToListAsync(); var courseAttendanceDtos = new List<CourseAttendanceDto>(); @@ -271,6 +271,11 @@ namespace Tsi1.BusinessLayer.Services { Id = course.Id, Name = course.Name, + ActivityQuantity = attendances + .Where(x => x.Activity.CourseId == course.Id) + .Select(x => x.ActivityId) + .Distinct() + .Count(), Users = new List<UserAttendanceDto>(), }; @@ -302,5 +307,63 @@ namespace Tsi1.BusinessLayer.Services result.Data = courseAttendanceDtos; return result; } + + public async Task<ServiceResult<CourseAttendanceDto>> CourseAttendance(int tenantId, int courseId) + { + var result = new ServiceResult<CourseAttendanceDto>(); + + var tenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Id == tenantId && x.Name != TenantAdmin.Name); + + if (tenant == null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, tenantId)); + return result; + } + + var course = await _context.Courses + .AsNoTracking() + .Include(x => x.StudentCourses) + .FirstOrDefaultAsync(x => x.Id == courseId); + + var studentIds = course.StudentCourses.Select(x => x.StudentId).ToList(); + + var users = await _context.Users + .AsNoTracking() + .Where(x => x.TenantId == tenantId && studentIds.Contains((int)x.StudentId)) + .ToListAsync(); + + var attendances = await _context.Attendances + .AsNoTracking() + .Include(x => x.Activity) + .Where(x => x.Activity.CourseId == courseId && x.Activity.IsVideoConference) + .ToListAsync(); + + var courseAttendanceDto = new CourseAttendanceDto + { + Id = course.Id, + Name = course.Name, + ActivityQuantity = attendances.Select(x => x.ActivityId).Distinct().Count(), + Users = new List<UserAttendanceDto>(), + }; + + foreach (var user in users) + { + var attendanceQuantity = attendances.Where(x => x.UserId == user.Id).Count(); + + var userAttendanceDto = new UserAttendanceDto + { + Email = user.Email, + FirstName = user.FirstName, + LastName = user.LastName, + Quantity = attendanceQuantity + }; + + courseAttendanceDto.Users.Add(userAttendanceDto); + } + + result.Data = courseAttendanceDto; + return result; + } } }