Skip to content
Snippets Groups Projects
Commit 62fb48a0 authored by Lucca Santangelo's avatar Lucca Santangelo
Browse files

myCourses y create course

parent 3f55ac90
No related branches found
No related tags found
No related merge requests found
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();
}
}
}
......@@ -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)
};
......
......@@ -114,6 +114,7 @@ namespace Tsi1.Api.Infrastructure
ClockSkew = TimeSpan.FromMinutes(1)
},
out var validatedToken);
return (principal, validatedToken as JwtSecurityToken);
}
......
......@@ -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();
......
<?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>
......
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class CourseCreateDto
{
public string Name { get; set; }
}
}
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; }
}
}
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);
}
}
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;
}
}
}
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