system-praktyk-api/src/InternshipSystem.Api/Controllers/DocumentsController.cs
2020-08-17 19:05:30 +02:00

85 lines
3.0 KiB
C#

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;
}
/// <summary>
/// Fill out required document,
/// </summary>
/// <param name="document">Documents Scan and description, and Id of filled document</param>
/// <returns></returns>
/// <response code="200">If change was successfully registered</response>
/// <response code="400">If the provided query was malformed</response>
/// <response code="404">Id doesn't match any required document</response>
/// <response code="401">This action is only available for authorized student registered for current edition</response>
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> 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<Internship> GetCurrentStudentInternship()
{
return Context.Editions
.FirstAsync().Result
.Internships
.First();
}
}
}