99 lines
4.8 KiB
C#
99 lines
4.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace InternshipSystem.Api.Controllers
|
|
{
|
|
[Route("students")]
|
|
[ApiController]
|
|
public class StudentsController : ControllerBase
|
|
{
|
|
private readonly InternshipDbContext _context;
|
|
|
|
public StudentsController(InternshipDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
[HttpGet("current")]
|
|
[Authorize]
|
|
public async Task<ActionResult<Student>> GetCurrentStudentData([FromServices] User user, CancellationToken cancellationToken)
|
|
{
|
|
return await _context.Students.FindAsync(user.PersonNumber);
|
|
}
|
|
|
|
[HttpPut("current")]
|
|
[Authorize]
|
|
public async Task<ActionResult> UpdateCurrentStudentData([FromBody] CurrentStudentForm studentNewData, [FromServices] User user, CancellationToken cancellationToken)
|
|
{
|
|
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($"Student updated successfully");
|
|
}
|
|
|
|
[HttpGet("{studentPersonNumber}")]
|
|
[Authorize]
|
|
public async Task<ActionResult<Student>> GetStudentByPersonNumber(long studentPersonNumber, CancellationToken cancellationToken) =>
|
|
await _context.Students.FindAsync(studentPersonNumber);
|
|
|
|
[HttpGet]
|
|
[Authorize]
|
|
public async Task<ActionResult<IReadOnlyCollection<Student>>> GetStudents([FromBody] StudentSearchQuery searchQuery, CancellationToken cancellationToken) =>
|
|
await _context.Students
|
|
.Where(s => !searchQuery.AlbumNumber.HasValue || s.AlbumNumber.Equals(searchQuery.AlbumNumber))
|
|
.Where(s => string.IsNullOrEmpty(searchQuery.FirstName) || s.FirstName.ToLower().Contains(searchQuery.FirstName.ToLower()))
|
|
.Where(s => string.IsNullOrEmpty(searchQuery.LastName) || s.LastName.ToLower().Contains(searchQuery.LastName.ToLower()))
|
|
.OrderBy(s => s.AlbumNumber)
|
|
.Skip(searchQuery.Page * searchQuery.PerPage)
|
|
.Take(searchQuery.PerPage)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
[HttpPut]
|
|
[Authorize]
|
|
public async Task<ActionResult> UpdateStudentData([FromBody] StudentForm studentNewData, CancellationToken cancellationToken)
|
|
{
|
|
var validator = new StudentForm.Validator();
|
|
var validationResult = await validator.ValidateAsync(studentNewData, cancellationToken);
|
|
|
|
if (!validationResult.IsValid)
|
|
{
|
|
return BadRequest(validationResult.ToString());
|
|
}
|
|
|
|
var currentStudent = await _context.Students.FindAsync(studentNewData.Id);
|
|
|
|
if (currentStudent == null)
|
|
{
|
|
return NotFound($"Student with id: {studentNewData.Id} does not exist");
|
|
}
|
|
|
|
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($"Student updated successfully");
|
|
}
|
|
}
|
|
} |