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

add endpoint AddProfessorToCourse

parent 0b1cbe51
No related branches found
No related tags found
1 merge request!5Multi tenancy
......@@ -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();
}
}
}
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; }
}
}
......@@ -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}'";
......
......@@ -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);
}
}
......@@ -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;
}
......
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