document mapping and validation
This commit is contained in:
parent
fd37591201
commit
f0dba347c5
14
src/InternshipSystem.Api/ApiProfile.cs
Normal file
14
src/InternshipSystem.Api/ApiProfile.cs
Normal 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>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using InternshipSystem.Api.Queries;
|
||||
using InternshipSystem.Api.Validators;
|
||||
using InternshipSystem.Core;
|
||||
using InternshipSystem.Repository;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@ -14,16 +17,18 @@ namespace InternshipSystem.Api.Controllers
|
||||
public class DocumentsController : ControllerBase
|
||||
{
|
||||
private InternshipDbContext Context { get; }
|
||||
private IMapper Mapper { get; }
|
||||
|
||||
public DocumentsController(InternshipDbContext context)
|
||||
public DocumentsController(InternshipDbContext context, IMapper mapper)
|
||||
{
|
||||
Context = context;
|
||||
Mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill out required document,
|
||||
/// </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>
|
||||
/// <response code="200">If change was successfully registered</response>
|
||||
/// <response code="400">If the provided query was malformed</response>
|
||||
@ -34,52 +39,51 @@ namespace InternshipSystem.Api.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<ActionResult> AddDocumentToInternship([FromBody] DocumentPublishRequest document)
|
||||
public async Task<ActionResult> AddDocumentToInternship([FromBody] DocumentPublishRequest documentRequest)
|
||||
{
|
||||
// TODO:
|
||||
// if not authorised then
|
||||
// return Unauthorized();
|
||||
var validator = new DocumentPublishRequestValidator();
|
||||
var validationResult = await validator.ValidateAsync(documentRequest);
|
||||
|
||||
// TODO:
|
||||
// is request is valid??
|
||||
// if not then
|
||||
// return BadRequest();
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
return BadRequest(validationResult.ToString());
|
||||
}
|
||||
|
||||
var studentInternship = await GetCurrentStudentInternship();
|
||||
if (document.Id != null)
|
||||
{
|
||||
var doc = studentInternship.Documentation
|
||||
.Find(d => d.Id == document.Id);
|
||||
if (doc == null)
|
||||
|
||||
var document = Mapper.Map<Document>(documentRequest);
|
||||
|
||||
if (documentRequest.Id.HasValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
studentInternship.UpdateDocument(document);
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
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
|
||||
});
|
||||
studentInternship.AddNewDocument(document);
|
||||
}
|
||||
|
||||
await Context.SaveChangesAsync();
|
||||
return Ok();
|
||||
}
|
||||
|
||||
//TODO: rewrite when authentication will be implemented
|
||||
private async Task<Internship> GetCurrentStudentInternship()
|
||||
{
|
||||
return Context.Editions
|
||||
.FirstAsync().Result
|
||||
.Internships
|
||||
.First();
|
||||
// TODO: rewrite when authentication will be implemented
|
||||
var edition = await Context
|
||||
.Editions
|
||||
.FirstAsync();
|
||||
|
||||
return await Context.Entry(edition)
|
||||
.Collection(e => e.Internships)
|
||||
.Query()
|
||||
.FirstAsync();
|
||||
}
|
||||
}
|
||||
}
|
@ -8,6 +8,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<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="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
|
||||
<PackageReference Include="Serilog" Version="2.9.0" />
|
||||
|
@ -4,7 +4,7 @@ namespace InternshipSystem.Api.Queries
|
||||
{
|
||||
public class DocumentPublishRequest
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public long? Id { get; set; }
|
||||
public string Description { get; set; }
|
||||
public byte[] Scan { get; set; }
|
||||
public DocumentType Type { get; set; }
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using AutoMapper;
|
||||
using InternshipSystem.Repository;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@ -30,6 +31,7 @@ namespace InternshipSystem.Api
|
||||
options.IncludeXmlComments(xmlPath);
|
||||
})
|
||||
.AddScoped<DatabaseFiller>()
|
||||
.AddAutoMapper(cfg => cfg.AddProfile<ApiProfile>())
|
||||
.AddControllers()
|
||||
;
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using InternshipSystem.Core.ValueObject;
|
||||
|
||||
namespace InternshipSystem.Core
|
||||
{
|
||||
@ -11,5 +13,22 @@ namespace InternshipSystem.Core
|
||||
public Report Report { get; set; }
|
||||
public List<Document> Approvals { 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -5,4 +5,8 @@
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="10.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Loading…
Reference in New Issue
Block a user