From 847173c6714dde421da2081f7e0d70e7a62483dd Mon Sep 17 00:00:00 2001 From: esantangelo <enzo020895@gmail.com> Date: Sat, 24 Oct 2020 17:39:25 -0300 Subject: [PATCH] add endpoint AddProfessorToCourse --- .../Tsi1.Api/Controllers/CourseController.cs | 13 ++++ .../Dtos/ProfessorCourseDto.cs | 13 ++++ .../Helpers/ErrorMessages.cs | 4 +- .../Interfaces/ICourseService.cs | 2 + .../Services/CourseService.cs | 62 ++++++++++++++++++- 5 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/ProfessorCourseDto.cs diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index 85233b6..c94df79 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 0000000..d88467e --- /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 05dcfdb..880795c 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 bf60de0..2d89f2d 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 19bb7a7..8ca7b40 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; } -- GitLab