diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index c3597213db864c1467de99fd48ed80b96faac26f..540ab08af66615f623811fa0d894ac7cb00aaea7 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -65,11 +65,38 @@ namespace Tsi1.Api.Controllers var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); var result = await _courseService.Matriculate(userId, courseId); + if (result.HasError) { return BadRequest(result.Message); } + if (result.Data == false) + { + return NotFound(result.Message); + } + + return Ok(); + } + + [Authorize(Roles = UserTypes.Student)] + [HttpPost("DropOutFromCourse/{courseId}")] + public async Task<IActionResult> DropOutFromCourse(int courseId) + { + var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); + + var result = await _courseService.DropOutFromCourse(userId, courseId); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + if (result.Data == false) + { + return NotFound(result.Message); + } + return Ok(); } @@ -78,11 +105,36 @@ namespace Tsi1.Api.Controllers public async Task<IActionResult> AddProfessorToCourse(ProfessorCourseDto professorCourseDto) { var result = await _courseService.AddProfessorToCourse(professorCourseDto); + if (result.HasError) { return BadRequest(result.Message); } + if (result.Data == false) + { + return NotFound(result.Message); + } + + return Ok(); + } + + [Authorize(Roles = UserTypes.FacultyAdmin)] + [HttpPost("RemoveProfessorToCourse")] + public async Task<IActionResult> RemoveProfessorFromCourse(ProfessorCourseDto professorCourseDto) + { + var result = await _courseService.RemoveProfessorToCourse(professorCourseDto); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + if (result.Data == false) + { + return NotFound(result.Message); + } + return Ok(); } diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs index 1fbbe5ca1f7cdb42755c0717dbfc42a9035983aa..1e3f93cc85e8feecb2c7e0bfaee509c2fec8edb3 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs @@ -171,7 +171,7 @@ namespace Tsi1.Api.Controllers } - [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)] + [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor + ", " + UserTypes.FacultyAdmin)] [HttpGet("GetAll")] public async Task<IActionResult> GetAll() { diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs index 99c521a4d670fd66eb3eee6753c7b97c4afc902b..74cab9f3d512fe48200a738d343fef433d46f8ff 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs @@ -10,9 +10,11 @@ namespace Tsi1.BusinessLayer.Helpers public const string IncorrectPassword = "Contraseña incorrecta"; public const string UserTypeDoesNotExist = "El tipo de usuario con id '{0}' no existe"; public const string StudentDoesNotExist = "El estudiante con Id de usuario: '{0}' no existe"; + public const string StudentCourseDoesNotExists = "El estudiante '{0}' no se encuentra matriculado en el curso '{1}'"; public const string StudentCourseAlreadyExists = "El estudiante '{0}' ya se encuentra matriculado en el curso '{1}'"; public const string ProffesorDoesNotExist = "El profesor con Id de usuario: '{0}' no existe"; public const string ProfessorCourseAlreadyExists = "El profesor '{0}' ya es docente del curso '{1}'"; + public const string ProfessorCourseDoesNotExists = "El profesor '{0}' no es docente del curso '{1}'"; public const string InvalidUsername = "El nombre de usuario debe ser de la forma: 'usuario@facultad'"; @@ -35,6 +37,5 @@ namespace Tsi1.BusinessLayer.Helpers public const string DuplicateTenantName = "Ya existe una Facultad con nombre '{0}'"; public const string InvalidUserType = "Tipo de usuario invalido '{0}'"; - } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs index cff802c381fa6603d11639b895a5442b5932ae48..885c3b3729b3a210b70990a747f1e808d6460d30 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs @@ -23,5 +23,9 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<bool>> Modify(int courseId, CourseCreateDto courseDto); Task<ServiceResult<bool>> Delete(int courseId); + + Task<ServiceResult<bool>> DropOutFromCourse(int userId, int courseId); + + Task<ServiceResult<bool>> RemoveProfessorToCourse(ProfessorCourseDto professorCourseDto); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs index 427b686f18fa586e4d5a99008e0b5510b02c30d7..e385562b0bbb4c2986dfe06bd602e40860faa8ce 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs @@ -97,7 +97,6 @@ namespace Tsi1.BusinessLayer.Services if (user == null || user.Student == null) { - result.HasError = true; result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId); return result; } @@ -106,7 +105,6 @@ namespace Tsi1.BusinessLayer.Services if (course == null) { - result.HasError = true; result.Message = string.Format(ErrorMessages.CourseDoesNotExist, courseId); return result; } @@ -130,7 +128,9 @@ namespace Tsi1.BusinessLayer.Services _context.StudentCourses.Add(studentCourse); await _context.SaveChangesAsync(); - + + result.Data = true; + return result; } @@ -144,7 +144,6 @@ namespace Tsi1.BusinessLayer.Services if (user == null || user.Professor == null) { - result.HasError = true; result.Message = string.Format(ErrorMessages.UserDoesNotExist, user.Username); return result; } @@ -154,7 +153,6 @@ namespace Tsi1.BusinessLayer.Services if (course == null) { - result.HasError = true; result.Message = string.Format(ErrorMessages.CourseDoesNotExist, professorCourseDto.CourseId); return result; } @@ -179,6 +177,8 @@ namespace Tsi1.BusinessLayer.Services await _context.SaveChangesAsync(); + result.Data = true; + return result; } @@ -240,5 +240,88 @@ namespace Tsi1.BusinessLayer.Services return result; } + + public async Task<ServiceResult<bool>> DropOutFromCourse(int userId, int courseId) + { + var result = new ServiceResult<bool>(); + + var user = await _context.Users + .Include(x => x.Student) + .FirstOrDefaultAsync(x => x.Id == userId); + + if (user == null || user.Student == null) + { + result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId); + return result; + } + + var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == courseId); + + if (course == null) + { + result.Message = string.Format(ErrorMessages.CourseDoesNotExist, courseId); + return result; + } + + var studentCourse = await _context.StudentCourses + .FirstOrDefaultAsync(x => x.StudentId == user.StudentId && x.CourseId == course.Id); + + if (studentCourse == null) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.StudentCourseDoesNotExists, user.Username, course.Name); + return result; + } + + _context.StudentCourses.Remove(studentCourse); + + await _context.SaveChangesAsync(); + + result.Data = true; + + return result; + } + + public async Task<ServiceResult<bool>> RemoveProfessorToCourse(ProfessorCourseDto professorCourseDto) + { + var result = new ServiceResult<bool>(); + + var user = await _context.Users + .Include(x => x.Professor) + .FirstOrDefaultAsync(x => x.Id == professorCourseDto.UserId); + + if (user == null || user.Professor == null) + { + result.Message = string.Format(ErrorMessages.UserDoesNotExist, user.Username); + return result; + } + + var course = await _context.Courses + .FirstOrDefaultAsync(x => x.Id == professorCourseDto.CourseId); + + if (course == null) + { + result.Message = string.Format(ErrorMessages.CourseDoesNotExist, professorCourseDto.CourseId); + return result; + } + + var professorCourse = await _context.ProfessorCourses + .FirstOrDefaultAsync(x => x.ProfessorId == user.ProfessorId && x.CourseId == course.Id); + + if (professorCourse == null) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.ProfessorCourseDoesNotExists, user.Username, course.Name); + return result; + } + + _context.ProfessorCourses.Remove(professorCourse); + + await _context.SaveChangesAsync(); + + result.Data = true; + + return result; + } } }