Merge branch 'master' of http://git.kadet.net/system-praktyk/system-praktyk-api into fix/tytokurwaniepotrafisznicporzadniezrobic KEK Co-authored-by: MaxchilKH <m.w.bohdanowicz@gmail.com>
113 lines
4.6 KiB
C#
113 lines
4.6 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 InternshipSystem.Core.Entity.Internship;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace InternshipSystem.Api.Controllers
|
|
{
|
|
[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 result = await useCase.UpdateInternshipRegistration(registrationCommand, cancellationToken);
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return Ok(result);
|
|
}
|
|
catch (ArgumentException e)
|
|
{
|
|
return BadRequest(e.Message);
|
|
}
|
|
|
|
}
|
|
|
|
[HttpGet]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[Authorize(Policy = Policies.RegisteredOnly)]
|
|
public async Task<ActionResult<Internship>> GetCurrentEditionInternship([FromServices] User user, CancellationToken cancellationToken)
|
|
{
|
|
var edition = await _context.Editions
|
|
.FindAsync(user.EditionId);
|
|
|
|
var internship = await _context.Entry(edition)
|
|
.Collection(e => e.Internships)
|
|
.Query()
|
|
.Include(i => i.Student)
|
|
.Include(i => i.InternshipRegistration)
|
|
.Include(i => i.InternshipRegistration.Company)
|
|
.Include(i => i.InternshipRegistration.BranchAddress)
|
|
.Include(i => i.InternshipRegistration.Type)
|
|
.Include(i => i.Report)
|
|
.Include(i => i.Approvals)
|
|
.Include(i => i.Documentation)
|
|
.SingleAsync(i => i.Student.Id == user.PersonNumber, cancellationToken);
|
|
|
|
return Ok(internship);
|
|
}
|
|
}
|
|
} |