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..b9d543f7f1c43cff454925076b42bf62cf869ae4 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)] @@ -50,6 +56,12 @@ namespace Tsi1.Api.Controllers var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); newCourse.TenantId = tenantId; + var bedeliaResult = await _bedeliaService.IsValidCourse(newCourse.Name); + if (bedeliaResult.HasError) + { + return BadRequest(bedeliaResult.Message); + } + var result = await _courseService.Create(newCourse); if (result.HasError) { @@ -282,13 +294,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/DataLoadController.cs b/Tsi1.Api/Tsi1.Api/Controllers/DataLoadController.cs index d97bd21d6deb6b862fb45fc87fb2e235c8e8ca9c..23c30fa56cbd345847d9cbacf0cc962ba5f5cbcc 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/DataLoadController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/DataLoadController.cs @@ -28,5 +28,46 @@ namespace Tsi1.Api.Controllers return Ok(); } + + [AllowAnonymous] + [HttpPost("LoadStudentsInBedelia")] + public async Task<IActionResult> LoadStudentsInBedelia() + { + try + { + var result = await _dataLoad.LoadStudentsInBedelia(); + if (result.HasError) + { + return BadRequest(result.Message); + } + + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + + return Ok(); + } + + [AllowAnonymous] + [HttpPost("LoadCoursesInBedelia")] + public async Task<IActionResult> LoadCoursesInBedelia() + { + try + { + var result = await _dataLoad.LoadCoursesInBedelia(); + if (result.HasError) + { + return BadRequest(result.Message); + } + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + + return Ok(); + } } } 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 ae078b3207911f7ce8ccb57be25d44a1dbb46fe9..9bc016b50a9ca667e9016e93502424d841d1a0cc 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs @@ -174,6 +174,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/DataLoad/DataLoad.cs b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs index 4562c75fc7f0f6e09222d506d31b41ab1cdcb43b..ff63b39f7f0e0c2aca51014442d273565911a38e 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs @@ -1,5 +1,7 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading.Tasks; using Tsi1.BusinessLayer.Dtos; @@ -18,13 +20,15 @@ namespace Tsi1.BusinessLayer.DataLoad private readonly ICourseService _courseService; private readonly ISectionService _sectionService; private readonly ISectionItemService _sectionItemService; + private readonly IBedeliaService _bedeliaService; public DataLoad(Tsi1Context context, ITenantService tenantService, IUserService userService, ICourseService courseService, ISectionService sectionService, - ISectionItemService sectionItemService) + ISectionItemService sectionItemService, + IBedeliaService bedeliaService) { _context = context; _tenantService = tenantService; @@ -32,6 +36,34 @@ namespace Tsi1.BusinessLayer.DataLoad _courseService = courseService; _sectionService = sectionService; _sectionItemService = sectionItemService; + _bedeliaService = bedeliaService; + } + + public async Task<ServiceResult<bool>> LoadStudentsInBedelia() + { + var users = await (from u in _context.Users + join s in _context.Students + on u.StudentId equals s.Id + select new UserCreateBedeliaDto + { + IdentityCard = s.IdentityCard, + Name = string.Join(" ", u.FirstName, u.LastName), + Password = u.Password + }).ToListAsync(); + + return await _bedeliaService.CreateUsers(users); + } + + public async Task<ServiceResult<bool>> LoadCoursesInBedelia() + { + var courses = await _context.Courses.Select(x => + new CourseCreateBedeliaDto() + { + Name = x.Name + }) + .ToListAsync(); + + return await _bedeliaService.CreateCourses(courses); } public async Task LoadDataAsync() diff --git a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs index 613fbe6364554626bc8db4b44ce90f4a1e86725c..b2a80aa5aa70de415a8502aabe6cf504fef967d1 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs @@ -2,11 +2,14 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; +using Tsi1.BusinessLayer.Helpers; namespace Tsi1.BusinessLayer.DataLoad { public interface IDataLoad { Task LoadDataAsync(); + Task<ServiceResult<bool>> LoadStudentsInBedelia(); + Task<ServiceResult<bool>> LoadCoursesInBedelia(); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateBedeliaDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateBedeliaDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..5897c34e4c61e4c53166832477411d21fe1adc14 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateBedeliaDto.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class CourseCreateBedeliaDto + { + public string Name { get; set; } + } +} 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/Dtos/UserCreateBedeliaDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserCreateBedeliaDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..207f76b1854967fc95d812bcc1aa0090ef944a00 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserCreateBedeliaDto.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class UserCreateBedeliaDto + { + public string IdentityCard { get; set; } + public string Name { get; set; } + public string Password { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs index 0c58b2d07df47de41b99c5203e9a84f442be4641..1a159dbe04dba842753442fcd68b6303da49f555 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs @@ -10,9 +10,11 @@ namespace Tsi1.BusinessLayer.Interfaces public interface IBedeliaService { Task<ServiceResult<bool>> IsValidUser(string identityCard); - Task<ServiceResult<bool>> CloseRecord(string courseName, List<UserGradeDto> userGrades); Task<ServiceResult<bool>> Login(string identityCard, string password); + Task<ServiceResult<bool>> IsValidCourse(string courseName); + Task<ServiceResult<bool>> CreateUsers(List<UserCreateBedeliaDto> users); + Task<ServiceResult<bool>> CreateCourses(List<CourseCreateBedeliaDto> users); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs index 5b6c77f1acc56364b40aa8fc503a13f5a05bfd10..457339e77a102439c3f6a28c3257e0af64035804 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs @@ -37,6 +37,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/BedeliaService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/BedeliaService.cs index 8ea29f9b0a43eded823973d5f136fa76d757affd..94531055c440ed6be620a16087b8538013b920fe 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/BedeliaService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/BedeliaService.cs @@ -81,5 +81,56 @@ namespace Tsi1.BusinessLayer.Services return result; } + + public async Task<ServiceResult<bool>> IsValidCourse(string courseName) + { + var result = new ServiceResult<bool>(); + + var response = await _httpClient.GetAsync($"api/Courses/{courseName}"); + if (response.StatusCode != HttpStatusCode.OK) + { + result.HasError = true; + var errorMessage = await response.Content.ReadAsStringAsync(); + result.AddMessage(errorMessage); + } + + return result; + } + + public async Task<ServiceResult<bool>> CreateUsers(List<UserCreateBedeliaDto> users) + { + var result = new ServiceResult<bool>(); + + var jsonInString = JsonConvert.SerializeObject(users); + var model = new StringContent(jsonInString, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync("api/Users", model); + if (response.StatusCode != HttpStatusCode.OK) + { + result.HasError = true; + var errorMessage = await response.Content.ReadAsStringAsync(); + result.AddMessage(errorMessage); + } + + return result; + } + + public async Task<ServiceResult<bool>> CreateCourses(List<CourseCreateBedeliaDto> courses) + { + var result = new ServiceResult<bool>(); + + var jsonInString = JsonConvert.SerializeObject(courses); + var model = new StringContent(jsonInString, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync("api/Courses", model); + if (response.StatusCode != HttpStatusCode.OK) + { + result.HasError = true; + var errorMessage = await response.Content.ReadAsStringAsync(); + result.AddMessage(errorMessage); + } + + return result; + } } } 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 9ebcbdacd1529ae26b77db5027027a2e7b78d42e..5d3ac88e847a9dae291820244e09a4db52ebaced 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;