86 lines
3.5 KiB
C#
86 lines
3.5 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using InternshipSystem.Api.Commands;
|
|
using InternshipSystem.Api.Security;
|
|
using InternshipSystem.Api.UseCases;
|
|
using InternshipSystem.Repository;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace InternshipSystem.Api.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("internshipRegistration")]
|
|
public class InternshipRegistrationController : ControllerBase
|
|
{
|
|
private readonly InternshipDbContext _context;
|
|
|
|
public InternshipRegistrationController(InternshipDbContext dbContext)
|
|
{
|
|
_context = dbContext;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate and add filled internship registration form
|
|
/// </summary>
|
|
/// <response code="200">If registration form was successfully added</response>
|
|
/// <response code="400">If the provided registration query was malformed</response>
|
|
/// <response code="401">This action is only available for authorized student registered for current edition</response>
|
|
[HttpPut]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[Authorize(Policy = Policies.RegisteredOnly)]
|
|
public async Task<ActionResult> SubmitRegistrationForm(
|
|
[FromBody] UpdateRegistrationForm registrationCommand,
|
|
[FromServices] User user,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var edition = await _context.Editions
|
|
.Include(e => e.AvailableInternshipTypes)
|
|
.ThenInclude(t => t.InternshipType)
|
|
.Include(e => e.AvailableSubjects)
|
|
.ThenInclude(t => t.Subject)
|
|
.FirstAsync(e => e.Id == user.EditionId, cancellationToken);
|
|
|
|
var internshipRegistration =
|
|
await _context
|
|
.Entry(edition)
|
|
.Collection(e => e.Internships)
|
|
.Query()
|
|
.Include(i => i.InternshipRegistration)
|
|
.ThenInclude(r => r.BranchAddress)
|
|
.Include(i => i.InternshipRegistration)
|
|
.ThenInclude(r => r.Company)
|
|
.Include(i => i.InternshipRegistration)
|
|
.ThenInclude(c => c.Company.Branches)
|
|
.Include(i => i.InternshipRegistration)
|
|
.ThenInclude(c => c.Type)
|
|
.Include(i => i.InternshipRegistration)
|
|
.ThenInclude(c => c.Subjects)
|
|
.Where(i => i.Student.Id == user.PersonNumber)
|
|
.Select(i => i.InternshipRegistration)
|
|
.FirstAsync(cancellationToken);
|
|
|
|
|
|
var useCase = new UpdateInternshipRegistrationUseCase(_context, internshipRegistration, edition, user);
|
|
|
|
try
|
|
{
|
|
var (status, result) = await useCase.UpdateInternshipRegistration(registrationCommand, cancellationToken);
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new JsonResult(new {Status = status, Errors = result});
|
|
}
|
|
catch (ArgumentException e)
|
|
{
|
|
return BadRequest(e.Message);
|
|
}
|
|
}
|
|
}
|
|
} |