diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index 85233b6259047a0e6311e32adb7732f0ac0f10c5..c94df7923448ce7fc4e1af8b3d24d8d8adfe01af 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -73,5 +73,18 @@ namespace Tsi1.Api.Controllers return Ok(); } + + [Authorize(Roles = UserTypes.FacultyAdmin)] + [HttpPost("AddProfessorToCourse")] + public async Task<IActionResult> AddProfessorToCourse(ProfessorCourseDto professorCourseDto) + { + var result = await _courseService.AddProfessorToCourse(professorCourseDto); + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(); + } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ProfessorCourseDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ProfessorCourseDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..d88467ecb8daea0661fc3555806d77e2c5b7c667 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ProfessorCourseDto.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class ProfessorCourseDto + { + public int UserId { get; set; } + + public int CourseId { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs index 05dcfdbbcd5e6013d14e474cc5c9c735780e4f4d..880795c242b472b8ec7df424f0bc42ac33b789f7 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs @@ -10,8 +10,10 @@ 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 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 ForumDoesNotExist = "El foro con id '{0}' no existe"; public const string DuplicateForumName = "Ya existe un foro con nombre '{0}'"; diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs index bf60de00b7af55ba51b2d760bddcad9091e123bd..2d89f2d51a75d6fe72be9e61e968c0d63796e5db 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs @@ -15,5 +15,7 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<Course>> Create(CourseCreateDto newCourse); Task<ServiceResult<bool>> Matriculate(int userId, int courseId); + + Task<ServiceResult<bool>> AddProfessorToCourse(ProfessorCourseDto professorCourseDto); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs index 19bb7a7811ce7a18b75fd647038e69928a148506..8ca7b4017631c59d7b2ce394d666fd94257a782f 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs @@ -102,13 +102,69 @@ namespace Tsi1.BusinessLayer.Services var studentCourse = new StudentCourse { - CourseId = courseId, - StudentId = user.Student.Id + Course = course, + Student = user.Student }; _context.StudentCourses.Add(studentCourse); - await _context.SaveChangesAsync(); + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateException) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.StudentCourseAlreadyExists, user.Username, course.Name); + return result; + } + + return result; + } + + public async Task<ServiceResult<bool>> AddProfessorToCourse(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.HasError = true; + 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.HasError = true; + result.Message = string.Format(ErrorMessages.CourseDoesNotExist, professorCourseDto.CourseId); + return result; + } + + var professorCourse = new ProfessorCourse + { + Course = course, + Professor = user.Professor + }; + + _context.ProfessorCourses.Add(professorCourse); + + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateException) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.ProfessorCourseAlreadyExists, user.Username, course.Name); + return result; + } return result; }