From b4b05b7ef37a40f590648ed48685d2616414e208 Mon Sep 17 00:00:00 2001 From: maxchil Date: Fri, 2 Oct 2020 23:21:08 +0200 Subject: [PATCH] fix/tytokurwaniepotrafisznicporzadniezrobic (#55) Merge branch 'master' of http://git.kadet.net/system-praktyk/system-praktyk-api into fix/tytokurwaniepotrafisznicporzadniezrobic KEK Co-authored-by: MaxchilKH --- .../InternshipRegistrationController.cs | 40 +++++++++++++------ .../InternshipSystem.Api.csproj | 1 + src/InternshipSystem.Api/Startup.cs | 6 ++- .../UpdateInternshipRegistrationUseCase.cs | 11 ++++- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs index f2b0203..89a14b0 100644 --- a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs +++ b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Threading; using System.Threading.Tasks; using InternshipSystem.Api.Commands; @@ -35,7 +36,7 @@ namespace InternshipSystem.Api.Controllers [ProducesResponseType(StatusCodes.Status401Unauthorized)] [Authorize(Policy = Policies.RegisteredOnly)] public async Task SubmitRegistrationForm( - UpdateRegistrationForm registrationCommand, + [FromBody] UpdateRegistrationForm registrationCommand, [FromServices] User user, CancellationToken cancellationToken) { @@ -51,22 +52,36 @@ namespace InternshipSystem.Api.Controllers .Entry(edition) .Collection(e => e.Internships) .Query() + .Include(i => i.InternshipRegistration) + .ThenInclude(r => r.BranchAddress) + .Include(i => i.InternshipRegistration) + .ThenInclude(r => r.Company) + .Include(i => i.InternshipRegistration) + .ThenInclude(c => c.Company.Branches) + .Include(i => i.InternshipRegistration) + .ThenInclude(c => c.Type) + .Include(i => i.InternshipRegistration) + .ThenInclude(c => c.Subjects) .Where(i => i.Student.Id == user.PersonNumber) .Select(i => i.InternshipRegistration) - .Include(r => r.BranchAddress) - .Include(r => r.Type) - .Include(r => r.Subjects) - .Include(r => r.Company) - .Include(r => r.Company.Branches) .FirstAsync(cancellationToken); + var useCase = new UpdateInternshipRegistrationUseCase(_context, internshipRegistration, edition, user); - - var result = await useCase.UpdateInternshipRegistration(registrationCommand, cancellationToken); - - await _context.SaveChangesAsync(cancellationToken); - return Ok(result); + try + { + var result = await useCase.UpdateInternshipRegistration(registrationCommand, cancellationToken); + + await _context.SaveChangesAsync(cancellationToken); + + return Ok(result); + } + catch (ArgumentException e) + { + return BadRequest(e.Message); + } + } [HttpGet] @@ -92,7 +107,6 @@ namespace InternshipSystem.Api.Controllers .Include(i => i.Documentation) .SingleAsync(i => i.Student.Id == user.PersonNumber, cancellationToken); - internship.Edition = null; return Ok(internship); } } diff --git a/src/InternshipSystem.Api/InternshipSystem.Api.csproj b/src/InternshipSystem.Api/InternshipSystem.Api.csproj index d5c17c0..81e1ed3 100644 --- a/src/InternshipSystem.Api/InternshipSystem.Api.csproj +++ b/src/InternshipSystem.Api/InternshipSystem.Api.csproj @@ -12,6 +12,7 @@ + diff --git a/src/InternshipSystem.Api/Startup.cs b/src/InternshipSystem.Api/Startup.cs index 3dfb538..916c84e 100644 --- a/src/InternshipSystem.Api/Startup.cs +++ b/src/InternshipSystem.Api/Startup.cs @@ -16,6 +16,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; +using Newtonsoft.Json; namespace InternshipSystem.Api { @@ -46,7 +47,7 @@ namespace InternshipSystem.Api .AddScoped() .AddScoped() .AddAutoMapper(cfg => cfg.AddProfile()); - + services .AddSwaggerGen(options => { @@ -55,7 +56,8 @@ namespace InternshipSystem.Api var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); options.IncludeXmlComments(xmlPath); }) - .AddControllers(o => { o.ModelBinderProviders.Insert(0, new UserBinderProvider()); }); + .AddControllers(o => { o.ModelBinderProviders.Insert(0, new UserBinderProvider()); }) + .AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) diff --git a/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs b/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs index 5589172..c3b49a5 100644 --- a/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs +++ b/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -84,6 +85,8 @@ namespace InternshipSystem.Api.UseCases company.Name = companyUpdate.Name ?? company.Name; company.Nip = companyUpdate.Nip ?? company.Nip; + + subjectRegistration.BranchAddress = null; } if (companyUpdate.BranchOffice.HasValue) @@ -122,6 +125,12 @@ namespace InternshipSystem.Api.UseCases private void UpdateSubjects(IEnumerable subjects) { + + if (!_edition.AreSubjectsAvailable(subjects)) + { + throw new ArgumentException("subjects chosen are not available in this edition"); + } + subjectRegistration.Subjects = subjects .Select(i => new ProgramSubject