Student update current data and get current data
This commit is contained in:
parent
fa0448bef6
commit
da052c19aa
@ -4,7 +4,9 @@ using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FluentValidation;
|
||||
using InternshipSystem.Api.Options;
|
||||
using InternshipSystem.Api.Queries;
|
||||
using InternshipSystem.Api.Security;
|
||||
using InternshipSystem.Core;
|
||||
using InternshipSystem.Repository;
|
||||
@ -50,7 +52,7 @@ namespace InternshipSystem.Api.Controllers
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var student = await _context.Students.FirstOrDefaultAsync(s => s.Id == id);
|
||||
var student = await _context.Students.FirstOrDefaultAsync(s => s.Id == id, cancellationToken: cancellationToken);
|
||||
|
||||
if (student == null)
|
||||
{
|
||||
@ -104,7 +106,7 @@ namespace InternshipSystem.Api.Controllers
|
||||
|
||||
return Ok(_tokenService.generateToken(newIdentity));
|
||||
}
|
||||
|
||||
|
||||
private Student CreateStudentWithCasData(CasUserData casData)
|
||||
{
|
||||
var id = long.Parse(casData.PersonNumber);
|
||||
|
56
src/InternshipSystem.Api/Controllers/StudentController.cs
Normal file
56
src/InternshipSystem.Api/Controllers/StudentController.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using InternshipSystem.Api.Queries;
|
||||
using InternshipSystem.Api.Security;
|
||||
using InternshipSystem.Core;
|
||||
using InternshipSystem.Repository;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace InternshipSystem.Api.Controllers
|
||||
{
|
||||
[Route("student")]
|
||||
[ApiController]
|
||||
public class StudentController : ControllerBase
|
||||
{
|
||||
private readonly InternshipDbContext _context;
|
||||
|
||||
public StudentController(InternshipDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpGet("current")]
|
||||
[Authorize]
|
||||
public async Task<Student> GetCurrentStudentData([FromServices] User user, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Students.FindAsync(user.PersonNumber);
|
||||
}
|
||||
|
||||
[HttpPut("updateCurrent")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult> UpdateStudentData([FromBody] StudentForm studentNewData, [FromServices] User user, CancellationToken cancellationToken)
|
||||
{
|
||||
var validator = new StudentForm.Validator();
|
||||
var validationResult = await validator.ValidateAsync(studentNewData, cancellationToken);
|
||||
|
||||
if (!validationResult.IsValid || !user.PersonNumber.Equals(studentNewData.Id))
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var currentStudent = await _context.Students.FindAsync(user.PersonNumber);
|
||||
|
||||
currentStudent.AlbumNumber = studentNewData.AlbumNumber ?? currentStudent.AlbumNumber;
|
||||
currentStudent.FirstName = string.IsNullOrEmpty(studentNewData.FirstName) ? currentStudent.FirstName : studentNewData.FirstName;
|
||||
currentStudent.LastName = string.IsNullOrEmpty(studentNewData.LastName) ? currentStudent.LastName : studentNewData.LastName;
|
||||
currentStudent.Email = string.IsNullOrEmpty(studentNewData.Email) ? currentStudent.Email : studentNewData.Email;
|
||||
currentStudent.Course = string.IsNullOrEmpty(studentNewData.Course) ? currentStudent.Course : studentNewData.Course;
|
||||
currentStudent.Semester = studentNewData.Semester ?? currentStudent.Semester;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
24
src/InternshipSystem.Api/Queries/StudentForm.cs
Normal file
24
src/InternshipSystem.Api/Queries/StudentForm.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using FluentValidation;
|
||||
using InternshipSystem.Core;
|
||||
|
||||
namespace InternshipSystem.Api.Queries
|
||||
{
|
||||
public class StudentForm
|
||||
{
|
||||
public long? Id { get; set; }
|
||||
public int? AlbumNumber { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Course { get; set; }
|
||||
public int? Semester { get; set; }
|
||||
|
||||
public class Validator : AbstractValidator<StudentForm>
|
||||
{
|
||||
public Validator()
|
||||
{
|
||||
RuleFor(c => c.Id).NotNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -9,6 +9,8 @@ namespace InternshipSystem.Core
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Course { get; set; }
|
||||
public int? Semester { get; set; }
|
||||
|
||||
public static Student CreateStudent(long id, string firstName, string lastName, string email, in int albumNumber)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user