From b6669351c4c9f02fcacd3ca07cc49d5b50cebf1f Mon Sep 17 00:00:00 2001 From: mborzyszkowski Date: Mon, 17 Aug 2020 18:59:57 +0200 Subject: [PATCH] Add document the beginning of implementation --- .../Controllers/DocumentsController.cs | 53 +++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/src/InternshipSystem.Api/Controllers/DocumentsController.cs b/src/InternshipSystem.Api/Controllers/DocumentsController.cs index 5172d9b..d3c6394 100644 --- a/src/InternshipSystem.Api/Controllers/DocumentsController.cs +++ b/src/InternshipSystem.Api/Controllers/DocumentsController.cs @@ -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 AddDocumentToInternship([FromBody] DocumentPublishRequest document) => - throw new NotImplementedException(); + 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 + }); + } + return Ok(); + } + + //TODO: rewrite when authentication will be implemented + private async Task GetCurrentStudentInternship() + { + return Context.Editions + .FirstAsync().Result + .Internships + .First(); + } } } \ No newline at end of file