document mapping and validation

This commit is contained in:
mborzyszkowski 2020-08-17 20:14:52 +02:00
parent fd37591201
commit f0dba347c5
8 changed files with 95 additions and 34 deletions

View File

@ -0,0 +1,14 @@
using AutoMapper;
using InternshipSystem.Api.Queries;
using InternshipSystem.Core;
namespace InternshipSystem.Api
{
public class ApiProfile : Profile
{
public ApiProfile()
{
CreateMap<DocumentPublishRequest, Document>();
}
}
}

View File

@ -1,6 +1,9 @@
using System.Linq; using System;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoMapper;
using InternshipSystem.Api.Queries; using InternshipSystem.Api.Queries;
using InternshipSystem.Api.Validators;
using InternshipSystem.Core; using InternshipSystem.Core;
using InternshipSystem.Repository; using InternshipSystem.Repository;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -14,16 +17,18 @@ namespace InternshipSystem.Api.Controllers
public class DocumentsController : ControllerBase public class DocumentsController : ControllerBase
{ {
private InternshipDbContext Context { get; } private InternshipDbContext Context { get; }
private IMapper Mapper { get; }
public DocumentsController(InternshipDbContext context) public DocumentsController(InternshipDbContext context, IMapper mapper)
{ {
Context = context; Context = context;
Mapper = mapper;
} }
/// <summary> /// <summary>
/// Fill out required document, /// Fill out required document,
/// </summary> /// </summary>
/// <param name="document">Documents Scan and description, and Id of filled document</param> /// <param name="documentRequest">Documents Scan and description, and Id of filled document</param>
/// <returns></returns> /// <returns></returns>
/// <response code="200">If change was successfully registered</response> /// <response code="200">If change was successfully registered</response>
/// <response code="400">If the provided query was malformed</response> /// <response code="400">If the provided query was malformed</response>
@ -34,52 +39,51 @@ namespace InternshipSystem.Api.Controllers
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> AddDocumentToInternship([FromBody] DocumentPublishRequest document) public async Task<ActionResult> AddDocumentToInternship([FromBody] DocumentPublishRequest documentRequest)
{ {
// TODO: var validator = new DocumentPublishRequestValidator();
// if not authorised then var validationResult = await validator.ValidateAsync(documentRequest);
// return Unauthorized();
// TODO: if (!validationResult.IsValid)
// is request is valid?? {
// if not then return BadRequest(validationResult.ToString());
// return BadRequest(); }
var studentInternship = await GetCurrentStudentInternship(); var studentInternship = await GetCurrentStudentInternship();
if (document.Id != null)
var document = Mapper.Map<Document>(documentRequest);
if (documentRequest.Id.HasValue)
{
try
{
studentInternship.UpdateDocument(document);
}
catch (InvalidOperationException)
{ {
var doc = studentInternship.Documentation
.Find(d => d.Id == document.Id);
if (doc == null)
return NotFound(); return NotFound();
doc.Description = document.Description; }
doc.Scan = document.Scan;
doc.Type = document.Type;
doc.State = DocumentState.Submitted;
} }
else else
{ {
studentInternship.Documentation.Add( studentInternship.AddNewDocument(document);
new Document()
{
Description = document.Description,
Scan = document.Scan,
Type = document.Type,
State = DocumentState.Submitted
});
} }
await Context.SaveChangesAsync(); await Context.SaveChangesAsync();
return Ok(); return Ok();
} }
//TODO: rewrite when authentication will be implemented
private async Task<Internship> GetCurrentStudentInternship() private async Task<Internship> GetCurrentStudentInternship()
{ {
return Context.Editions // TODO: rewrite when authentication will be implemented
.FirstAsync().Result var edition = await Context
.Internships .Editions
.First(); .FirstAsync();
return await Context.Entry(edition)
.Collection(e => e.Internships)
.Query()
.FirstAsync();
} }
} }
} }

View File

@ -8,6 +8,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="10.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.0.1" />
<PackageReference Include="FluentValidation" Version="9.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.6" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.6" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
<PackageReference Include="Serilog" Version="2.9.0" /> <PackageReference Include="Serilog" Version="2.9.0" />

View File

@ -4,7 +4,7 @@ namespace InternshipSystem.Api.Queries
{ {
public class DocumentPublishRequest public class DocumentPublishRequest
{ {
public long Id { get; set; } public long? Id { get; set; }
public string Description { get; set; } public string Description { get; set; }
public byte[] Scan { get; set; } public byte[] Scan { get; set; }
public DocumentType Type { get; set; } public DocumentType Type { get; set; }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using AutoMapper;
using InternshipSystem.Repository; using InternshipSystem.Repository;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@ -30,6 +31,7 @@ namespace InternshipSystem.Api
options.IncludeXmlComments(xmlPath); options.IncludeXmlComments(xmlPath);
}) })
.AddScoped<DatabaseFiller>() .AddScoped<DatabaseFiller>()
.AddAutoMapper(cfg => cfg.AddProfile<ApiProfile>())
.AddControllers() .AddControllers()
; ;

View File

@ -0,0 +1,15 @@
using FluentValidation;
using InternshipSystem.Api.Queries;
using InternshipSystem.Core;
namespace InternshipSystem.Api.Validators
{
public class DocumentPublishRequestValidator : AbstractValidator<DocumentPublishRequest>
{
public DocumentPublishRequestValidator()
{
RuleFor(document => document.Scan).NotEmpty();
RuleFor(document => document.Type).NotEmpty();
}
}
}

View File

@ -1,4 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using InternshipSystem.Core.ValueObject;
namespace InternshipSystem.Core namespace InternshipSystem.Core
{ {
@ -11,5 +13,22 @@ namespace InternshipSystem.Core
public Report Report { get; set; } public Report Report { get; set; }
public List<Document> Approvals { get; set; } public List<Document> Approvals { get; set; }
public List<Document> Documentation { get; set; } public List<Document> Documentation { get; set; }
public void UpdateDocument(Document document)
{
var oldDocument = Documentation.First(d => d.Id == document.Id);
oldDocument.Description = document.Description;
oldDocument.Scan = document.Scan;
oldDocument.Type = document.Type;
oldDocument.State = DocumentState.Submitted;
}
public void AddNewDocument(Document document)
{
document.State = DocumentState.Submitted;
Documentation.Add(document);
}
} }
} }

View File

@ -5,4 +5,8 @@
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.0.0" />
</ItemGroup>
</Project> </Project>