using System.Linq; using System.Threading.Tasks; using InternshipSystem.Api.Queries; using InternshipSystem.Core; using InternshipSystem.Repository; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace InternshipSystem.Api.Controllers { [ApiController] [Route("document")] public class DocumentsController : ControllerBase { private InternshipDbContext Context { get; } public DocumentsController(InternshipDbContext context) { Context = context; } /// /// Fill out required document, /// /// Documents Scan and description, and Id of filled document /// /// If change was successfully registered /// If the provided query was malformed /// Id doesn't match any required document /// This action is only available for authorized student registered for current edition [HttpPost] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task AddDocumentToInternship([FromBody] DocumentPublishRequest document) { // TODO: // if not authorised then // return Unauthorized(); // TODO: // is request is valid?? // if not then // return BadRequest(); var studentInternship = await GetCurrentStudentInternship(); if (document.Id != null) { var doc = studentInternship.Documentation .Find(d => d.Id == document.Id); if (doc == null) return NotFound(); doc.Description = document.Description; doc.Scan = document.Scan; doc.Type = document.Type; doc.State = DocumentState.Submitted; } else { studentInternship.Documentation.Add( new Document() { Description = document.Description, Scan = document.Scan, Type = document.Type, State = DocumentState.Submitted }); } await Context.SaveChangesAsync(); return Ok(); } //TODO: rewrite when authentication will be implemented private async Task GetCurrentStudentInternship() { return Context.Editions .FirstAsync().Result .Internships .First(); } } }