Add document the beginning of implementation

This commit is contained in:
mborzyszkowski 2020-08-17 18:59:57 +02:00
parent 48ec0e2439
commit b6669351c4

View File

@ -1,12 +1,16 @@
using System;
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; }
@ -30,7 +34,50 @@ namespace InternshipSystem.Api.Controllers
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> AddDocumentToInternship([FromBody] DocumentPublishRequest document) =>
throw new NotImplementedException();
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
});
}
return Ok();
}
//TODO: rewrite when authentication will be implemented
private async Task<Internship> GetCurrentStudentInternship()
{
return Context.Editions
.FirstAsync().Result
.Internships
.First();
}
}
}