diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs new file mode 100644 index 0000000000000000000000000000000000000000..03c403f9a56ede02a157b37538f221bcf3c8f3fd --- /dev/null +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Tsi1.Api.Infrastructure; +using Tsi1.BusinessLayer.Dtos; +using Tsi1.BusinessLayer.Helpers; +using Tsi1.BusinessLayer.Interfaces; + +namespace Tsi1.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class CourseController : ControllerBase + { + private readonly ICourseService _courseService; + + public CourseController(ICourseService courseService) + { + _courseService = courseService; + } + + [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)] + [HttpGet("MyCourses")] + public async Task<IActionResult> MyCourses() + { + var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); + var userType = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value; + + var result = await _courseService.GetCoursePreviews(userId, userType); + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(result.Data); + } + + [Authorize(Roles = UserTypes.FacultyAdmin)] + [HttpPost("Create")] + public async Task<IActionResult> Create(CourseCreateDto newCourse) + { + var result = await _courseService.Create(newCourse); + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(); + } + } +} diff --git a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs index 56e1e6dc9226d4864088385d994062e3dfa88254..fb50bcde10ce784bd151b42eba21551aa2df2507 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/UserController.cs @@ -42,7 +42,8 @@ namespace Tsi1.Api.Controllers var claims = new[] { - new Claim(ClaimTypes.Name,user.Username), + new Claim("Id", user.Id.ToString()), + new Claim("Username", user.Username), new Claim(ClaimTypes.Role, user.UserType.Name) }; diff --git a/Tsi1.Api/Tsi1.Api/Infrastructure/JwtAuthManager.cs b/Tsi1.Api/Tsi1.Api/Infrastructure/JwtAuthManager.cs index 69fe71fbcabbcda7d2cc8fa475e3691df8333400..ef61c06d2147c7f65a2a0d34a8a0d806bf1dc167 100644 --- a/Tsi1.Api/Tsi1.Api/Infrastructure/JwtAuthManager.cs +++ b/Tsi1.Api/Tsi1.Api/Infrastructure/JwtAuthManager.cs @@ -114,6 +114,7 @@ namespace Tsi1.Api.Infrastructure ClockSkew = TimeSpan.FromMinutes(1) }, out var validatedToken); + return (principal, validatedToken as JwtSecurityToken); } diff --git a/Tsi1.Api/Tsi1.Api/Startup.cs b/Tsi1.Api/Tsi1.Api/Startup.cs index b90bb57de9fed6d8c5d07b97c07c54bd6e6bbacd..8dd2b590986be674b348bbee1eeefc22d26c280b 100644 --- a/Tsi1.Api/Tsi1.Api/Startup.cs +++ b/Tsi1.Api/Tsi1.Api/Startup.cs @@ -39,6 +39,7 @@ namespace Tsi1.Api services.AddDbContext<Tsi1Context>(x => x.UseNpgsql(Configuration.GetConnectionString("PostgreSql"))); services.AddScoped<IUserService, UserService>(); services.AddScoped<IUserTypeService, UserTypeService>(); + services.AddScoped<ICourseService, CourseService>(); services.AddCors(); diff --git a/Tsi1.Api/Tsi1.Api/Tsi1.Api.csproj.user b/Tsi1.Api/Tsi1.Api/Tsi1.Api.csproj.user index 85e159c74d1484be795adf84409475cb0f95c4c9..ecbf551267f5a15d0fb795acfc2ca3f808c020b0 100644 --- a/Tsi1.Api/Tsi1.Api/Tsi1.Api.csproj.user +++ b/Tsi1.Api/Tsi1.Api/Tsi1.Api.csproj.user @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> - <Controller_SelectedScaffolderID>ApiControllerWithActionsScaffolder</Controller_SelectedScaffolderID> + <Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID> <Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath> <WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth> </PropertyGroup> diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..ef582f9fa58c2f162524f00d267a04c789091325 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class CourseCreateDto + { + public string Name { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CoursePreviewDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CoursePreviewDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..de761a5eb908f333c2ceb7c394341f9febb165c8 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CoursePreviewDto.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class CoursePreviewDto + { + public int Id { get; set; } + public string Name { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs new file mode 100644 index 0000000000000000000000000000000000000000..df6907fa874b47fad6ce918464f4e0df12eb697e --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Tsi1.BusinessLayer.Dtos; +using Tsi1.BusinessLayer.Helpers; +using Tsi1.DataLayer.Entities; + +namespace Tsi1.BusinessLayer.Interfaces +{ + public interface ICourseService + { + Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType); + + Task<ServiceResult<Course>> Create(CourseCreateDto newCourse); + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs new file mode 100644 index 0000000000000000000000000000000000000000..05c175789cc8eb480a976c4db2076e781dcefa1d --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs @@ -0,0 +1,76 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tsi1.BusinessLayer.Dtos; +using Tsi1.BusinessLayer.Helpers; +using Tsi1.BusinessLayer.Interfaces; +using Tsi1.DataLayer; +using Tsi1.DataLayer.Entities; + +namespace Tsi1.BusinessLayer.Services +{ + public class CourseService : ICourseService + { + private readonly Tsi1Context _context; + + public CourseService(Tsi1Context context) + { + _context = context; + } + + public async Task<ServiceResult<Course>> Create(CourseCreateDto newCourse) + { + var result = new ServiceResult<Course>(); + + var course = new Course() + { + Name = newCourse.Name + }; + + _context.Courses.Add(course); + await _context.SaveChangesAsync(); + + result.Data = course; + return result; + } + + public async Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType) + { + var result = new ServiceResult<List<CoursePreviewDto>>(); + result.Data = new List<CoursePreviewDto>(); + + var courses = new List<Course>(); + + if (userType == UserTypes.Student) + { + courses = await _context.StudentCourses + .Include(x => x.Course) + .Select(x => x.Course) + .ToListAsync(); + } + else if (userType == UserTypes.Professor) + { + courses = await _context.ProfessorCourses + .Include(x => x.Course) + .Select(x => x.Course) + .ToListAsync(); + } + + foreach (var course in courses) + { + var item = new CoursePreviewDto() + { + Id = course.Id, + Name = course.Name + }; + + result.Data.Add(item); + } + + return result; + } + } +}