diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index dd4de595a500ee8c7db2d63f7ea44554be2c910b..90c4e0573f50bae1a7edc46cf7eec814ee952a53 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -106,7 +106,7 @@ namespace Tsi1.Api.Controllers return Ok(); } - [Authorize(Roles = UserTypes.FacultyAdmin)] + [Authorize(Roles = UserTypes.FacultyAdmin + ", " + UserTypes.Professor)] [HttpPost("AddProfessorToCourse")] public async Task<IActionResult> AddProfessorToCourse(ProfessorCourseDto professorCourseDto) { diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs index bc1eeb80251a6193d27ca9a855965baeccd44026..b06b5c4c1c32236035ed1ac5f87fb5ed73fa6b03 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs @@ -149,7 +149,7 @@ namespace Tsi1.BusinessLayer.Services return result; } - public async Task<ServiceResult<bool>> AddProfessorToCourse(ProfessorCourseDto professorCourseDto) + public async Task<ServiceResult<bool>> AddProfessorToCourse(ProfessorCourseDto professorCourseDto, int userId, string userType) { var result = new ServiceResult<bool>(); @@ -181,6 +181,31 @@ namespace Tsi1.BusinessLayer.Services return result; } + var myUser = await _context.Users + .AsNoTracking() + .FirstOrDefaultAsync(x => x.Id == userId); + + if (myUser.TenantId != user.TenantId) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.InvalidTenant, myUser.TenantId)); + return result; + } + + if (userType == UserTypes.Professor) + { + var isProfessorCourse = await _context.ProfessorCourses + .AsNoTracking() + .AnyAsync(x => x.ProfessorId == myUser.ProfessorId); + + if (!isProfessorCourse) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.ProfessorCourseDoesNotExists, myUser.FirstName + " " + myUser.LastName, course.Name)); + return result; + } + } + var existingProfessorCourse = await _context.ProfessorCourses .FirstOrDefaultAsync(x => x.ProfessorId == user.ProfessorId && x.CourseId == course.Id);