56 lines
2.3 KiB
C#
56 lines
2.3 KiB
C#
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();
|
|
}
|
|
}
|
|
} |