From 042eaf38dc4ca1e03efc8a86af8ff05448e11189 Mon Sep 17 00:00:00 2001
From: Lucca Santangelo <luccasant95@gmail.com>
Date: Sat, 12 Dec 2020 12:56:37 -0300
Subject: [PATCH] commit

---
 Bedelia/Controllers/CoursesController.cs      | 75 ++++++++++++-------
 .../Tsi1.Api/Controllers/CourseController.cs  | 18 ++++-
 .../Controllers/EvaluationController.cs       | 25 ++++++-
 .../Tsi1.Api/Controllers/UserController.cs    | 14 +++-
 .../Dtos/StudentCourseResultPreviewDto.cs     |  2 +
 .../Interfaces/IUserService.cs                |  2 +-
 .../Services/StudentCourseResultService.cs    | 17 ++++-
 .../Services/UserService.cs                   | 21 +-----
 8 files changed, 122 insertions(+), 52 deletions(-)

diff --git a/Bedelia/Controllers/CoursesController.cs b/Bedelia/Controllers/CoursesController.cs
index 56e4e8d..8979b7f 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 06a6fea..2610d44 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 a0b1f65..e136370 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 efdcc33..96cd6b2 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 b1ca70a..3765e1d 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 e3a8920..8fc9a7f 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 6e4b0d1..9eb5e0c 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 d50c978..fac2736 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;
-- 
GitLab