diff --git a/Bedelia/Controllers/CoursesController.cs b/Bedelia/Controllers/CoursesController.cs index 56e4e8da60d9f5a601fd17532cc672a8a2fc0ee7..8979b7f77ef814f38ab4b9cc030419e256fa460a 100644 --- a/Bedelia/Controllers/CoursesController.cs +++ b/Bedelia/Controllers/CoursesController.cs @@ -110,40 +110,61 @@ namespace Bedelia.Controllers [HttpPost("closeRecord")] public async Task<IActionResult> CloseRecord(CloseRecordDto closeRecord) { - var now = DateTime.Now; - var course = await _context.Courses - .FirstOrDefaultAsync(x => x.Name == closeRecord.CourseName); - - if (course == null) + try { - return BadRequest(string.Format(@"No existe el curso con nombre {0} en bedelia", closeRecord.CourseName)); - } + var now = DateTime.Now; + var course = await _context.Courses + .FirstOrDefaultAsync(x => x.Name == closeRecord.CourseName); - var userIdentityCards = closeRecord.UserGrades.Select(x => x.IdentityCard); + if (course == null) + { + return BadRequest(string.Format("No existe el curso con nombre {0} en bedelia", closeRecord.CourseName)); + } - var users = await _context.Users - .Where(x => userIdentityCards.Contains(x.IdentityCard)) - .ToListAsync(); + var userIdentityCards = closeRecord.UserGrades.Select(x => x.IdentityCard); - var userCourses = new List<UserCourse>(); - foreach (var user in users) - { - var grade = closeRecord.UserGrades - .Where(x => x.IdentityCard == user.IdentityCard) - .Select(x => x.Grade) - .FirstOrDefault(); + var users = await _context.Users + .Where(x => userIdentityCards.Contains(x.IdentityCard)) + .ToListAsync(); + + var dbUserIdentityCards = users.Select(x => x.IdentityCard); + var identityCardNotFounds = userIdentityCards.Except(dbUserIdentityCards); - userCourses.Add(new UserCourse() + var errors = string.Empty; + foreach (var identityCard in identityCardNotFounds) { - CourseId = course.Id, - UserId = user.Id, - Grade = grade, - GradeDate = now - }); - } + errors += string.Format("No existe el usuario con cedula {0} en bedelia", identityCard) + "\n"; + } - _context.UserCourses.AddRange(userCourses); - await _context.SaveChangesAsync(); + if (!string.IsNullOrEmpty(errors)) + { + return BadRequest(errors); + } + + var userCourses = new List<UserCourse>(); + foreach (var user in users) + { + var grade = closeRecord.UserGrades + .Where(x => x.IdentityCard == user.IdentityCard) + .Select(x => x.Grade) + .FirstOrDefault(); + + userCourses.Add(new UserCourse() + { + CourseId = course.Id, + UserId = user.Id, + Grade = grade, + GradeDate = now + }); + } + + _context.UserCourses.AddRange(userCourses); + await _context.SaveChangesAsync(); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } return Ok(); } diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index 06a6fea9f496414bc9a627c14da589806565eb37..2610d44b06250e559f0b6710fb0d93f6bf0b23d6 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -19,12 +19,18 @@ namespace Tsi1.Api.Controllers private readonly ICourseService _courseService; private readonly IUserService _userService; private readonly IBedeliaService _bedeliaService; + private readonly IStudentCourseResultService _studentCourseResultService; - public CourseController(ICourseService courseService, IUserService userService, IBedeliaService bedeliaService) + public CourseController( + ICourseService courseService, + IUserService userService, + IBedeliaService bedeliaService, + IStudentCourseResultService studentCourseResultService) { _courseService = courseService; _userService = userService; _bedeliaService = bedeliaService; + _studentCourseResultService = studentCourseResultService; } [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)] @@ -282,13 +288,21 @@ namespace Tsi1.Api.Controllers [HttpPost("CloseRecord/{courseId}")] public async Task<IActionResult> CloseRecord(int courseId) { + var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); + var courseResult = await _courseService.GetById(courseId); if (courseResult.HasError) { return BadRequest(courseResult.Message); } - var userResult = await _userService.GetUserGrades(courseId); + var studentCourseResult = await _studentCourseResultService.GetLastStudentCourseResults(courseId, userId); + if (studentCourseResult.HasError) + { + return BadRequest(studentCourseResult.Message); + } + + var userResult = _userService.GetUserGrades(studentCourseResult.Data); if (userResult.HasError) { return BadRequest(userResult.Message); diff --git a/Tsi1.Api/Tsi1.Api/Controllers/EvaluationController.cs b/Tsi1.Api/Tsi1.Api/Controllers/EvaluationController.cs index a0b1f65f705a09c9ad7115bce5624c5b5b1e5d77..e13637016b443b0613a8fbf864faa3943fac4741 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/EvaluationController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/EvaluationController.cs @@ -17,22 +17,41 @@ namespace Tsi1.Api.Controllers { private readonly IEvaluationService _evaluationService; private readonly IEvaluationTypeService _evaluationTypeService; + private readonly IUserService _userService; + private readonly IBedeliaService _bedeliaService; - public EvaluationController(IEvaluationService evaluationService, IEvaluationTypeService evaluationTypeService) + public EvaluationController( + IEvaluationService evaluationService, + IEvaluationTypeService evaluationTypeService, + IUserService userService, + IBedeliaService bedeliaService) { _evaluationService = evaluationService; _evaluationTypeService = evaluationTypeService; + _userService = userService; + _bedeliaService = bedeliaService; } [Authorize(Roles = UserTypes.Student)] [HttpPost("Registration/{evaluationId}")] - public async Task<IActionResult> GetAll(int evaluationId) + public async Task<IActionResult> Registration(int evaluationId) { var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); - var result = await _evaluationService.Registration(evaluationId, userId); + var userResult = await _userService.GetById(userId); + if (userResult.HasError) + { + return BadRequest(userResult.Message); + } + var bedeliaResult = await _bedeliaService.IsValidUser(userResult.Data.Student.IdentityCard); + if (bedeliaResult.HasError) + { + return BadRequest(bedeliaResult.Message); + } + + var result = await _evaluationService.Registration(evaluationId, userId); if (result.HasError) { return BadRequest(result.Message); diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs index efdcc334c20069c783cde602141edc73bcbc608f..96cd6b2b4f9aecc9d1f2e7a97cbb5d6b00e1423d 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs @@ -24,19 +24,22 @@ namespace Tsi1.Api.Controllers private readonly IUserTypeService _userTypeService; private readonly ITenantService _tenantService; private readonly IEmailService _emailService; + private readonly IBedeliaService _bedeliaService; public UserController( IJwtAuthManager jwtAuthManager, IUserService userService, IUserTypeService userTypeService, ITenantService tenantService, - IEmailService emailService) + IEmailService emailService, + IBedeliaService bedeliaService) { _jwtAuthManager = jwtAuthManager; _userService = userService; _userTypeService = userTypeService; _tenantService = tenantService; _emailService = emailService; + _bedeliaService = bedeliaService; } [AllowAnonymous] @@ -153,6 +156,15 @@ namespace Tsi1.Api.Controllers return BadRequest(string.Format(ErrorMessages.InvalidUserType, userType.Name)); } + if (userType.Name == UserTypes.Student) + { + var bedeliaResult = await _bedeliaService.IsValidUser(dto.IdentityCard); + if (bedeliaResult.HasError) + { + return BadRequest(bedeliaResult.Message); + } + } + var userServiceResult = await _userService.Create(dto, userType.Name, (int) tenantId); if (userServiceResult.HasError) diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentCourseResultPreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentCourseResultPreviewDto.cs index b1ca70a88385bfce974b5b4d9873940eda483caf..3765e1dc21d4ab4ca6b850b62907030b71b19975 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentCourseResultPreviewDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/StudentCourseResultPreviewDto.cs @@ -13,6 +13,8 @@ namespace Tsi1.BusinessLayer.Dtos public DateTime Date { get; set; } public int StudentId { get; set; } + public string StudenIdentityCard { get; set; } public int CourseId { get; set; } + public string CourseName { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs index e3a89207701c5fb213da6b5365f9555bf446afca..8fc9a7fe13c5399ba565a560d5e3dc96f7df47d7 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs @@ -35,6 +35,6 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<List<UserPreviewDto>>> GetProfessors(int tenantId); Task<ServiceResult<List<UserPreviewDto>>> GetAdmins(int tenantId, string userType); - Task<ServiceResult<List<UserGradeDto>>> GetUserGrades(int courseId); + ServiceResult<List<UserGradeDto>> GetUserGrades(List<StudentCourseResultPreviewDto> studentCourseResults); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/StudentCourseResultService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/StudentCourseResultService.cs index 6e4b0d1e69f7fa12058438ed710f4e41fbd55944..9eb5e0cb3a2157efefb6769396daf7b9b102a0cd 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/StudentCourseResultService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/StudentCourseResultService.cs @@ -119,6 +119,8 @@ namespace Tsi1.BusinessLayer.Services .ToListAsync(); var auxStudentCourseResults = await _context.StudentCourseResults + .Include(x => x.Course) + .Include(x => x.Student) .AsNoTracking() .Where(x => x.CourseId == courseId && studentIds.Contains(x.StudentId)) .ToListAsync(); @@ -127,7 +129,20 @@ namespace Tsi1.BusinessLayer.Services .Select(x => x.OrderByDescending(g => g.Date).First()) .ToList(); - result.Data = _mapper.Map<List<StudentCourseResultPreviewDto>>(studentCourseResults); + result.Data = new List<StudentCourseResultPreviewDto>(); + foreach (var item in studentCourseResults) + { + result.Data.Add(new StudentCourseResultPreviewDto() + { + Id = item.Id, + CourseId = item.CourseId, + CourseName = item.Course.Name, + Date = item.Date, + StudentId = item.StudentId, + StudenIdentityCard = item.Student.IdentityCard, + Result = item.Result + }); + } return result; } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs index d50c97812da77f2eeaf700601085255012d5f42b..fac2736dd41cb410e01b5ceb5550a2b4ae4d882e 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs @@ -357,27 +357,14 @@ namespace Tsi1.BusinessLayer.Services return result; } - public async Task<ServiceResult<List<UserGradeDto>>> GetUserGrades(int courseId) + public ServiceResult<List<UserGradeDto>> GetUserGrades(List<StudentCourseResultPreviewDto> studentCourseResults) { var result = new ServiceResult<List<UserGradeDto>>(); - var course = await _context.Courses - .Include(x => x.StudentCourses) - .ThenInclude(x => x.Student) - .FirstOrDefaultAsync(x => x.Id == courseId); - - if (!course.StudentCourses.Any()) - { - result.HasError = true; - result.AddMessage(string.Format(ErrorMessages.CourseHasNoStudents, courseId)); - return result; - } - - // TODO: obtain the grade from StudentCourses - result.Data = course.StudentCourses.Select(x => new UserGradeDto() + result.Data = studentCourseResults.Select(x => new UserGradeDto() { - Grade = 10, - IdentityCard = x.Student.IdentityCard + Grade = x.Result, + IdentityCard = x.StudenIdentityCard }).ToList(); return result;