Skip to content
Snippets Groups Projects
Commit dcb40117 authored by esantangelo's avatar esantangelo
Browse files

add endpoints DropOutFromCourse and RemoveProfessorFromCourse

parent 3a050fac
No related branches found
No related tags found
3 merge requests!20merge from develop,!16Feature/chat signalr,!11Feature/courses
......@@ -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();
}
......
......@@ -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}'";
}
}
......@@ -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);
}
}
......@@ -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;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment