diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index 28125eb09428f62e9b29f1f1c9bbcd1af9d27839..85233b6259047a0e6311e32adb7732f0ac0f10c5 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -57,5 +57,21 @@ namespace Tsi1.Api.Controllers return Ok(); } + + + [Authorize(Roles = UserTypes.Student)] + [HttpPost("Matriculate/{courseId}")] + public async Task<IActionResult> Matriculate(int courseId) + { + 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); + } + + return Ok(); + } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs index d99c8411b5ba655306a387f6ce5221f96d259382..bf60de00b7af55ba51b2d760bddcad9091e123bd 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs @@ -13,5 +13,7 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(string userType, int tenantId); Task<ServiceResult<Course>> Create(CourseCreateDto newCourse); + + Task<ServiceResult<bool>> Matriculate(int userId, int courseId); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs index c81f9a261b3f02beac916124f54b70fe57900614..19bb7a7811ce7a18b75fd647038e69928a148506 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs @@ -1,5 +1,6 @@ using AutoMapper; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.FileProviders; using System; using System.Collections.Generic; using System.Linq; @@ -32,8 +33,17 @@ namespace Tsi1.BusinessLayer.Services var course = _mapper.Map<Course>(newCourse); _context.Courses.Add(course); - await _context.SaveChangesAsync(); - + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateException) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.DuplicateCourseName, newCourse.Name); + return result; + } + result.Data = course; return result; } @@ -65,5 +75,42 @@ namespace Tsi1.BusinessLayer.Services return result; } + + public async Task<ServiceResult<bool>> Matriculate(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.HasError = true; + result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId); + return result; + } + + var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == courseId); + + if (course == null) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.CourseDoesNotExist, courseId); + return result; + } + + var studentCourse = new StudentCourse + { + CourseId = courseId, + StudentId = user.Student.Id + }; + + _context.StudentCourses.Add(studentCourse); + + await _context.SaveChangesAsync(); + + return result; + } } }