From d3691fb0a8d94d07f5e0b3e9b5d42a94d6ee0684 Mon Sep 17 00:00:00 2001 From: MaxchilKH Date: Sun, 13 Sep 2020 20:16:08 +0200 Subject: [PATCH] LOOOOOOOOOOOOL --- .../Controllers/AccessController.cs | 8 +--- .../Controllers/GutCasClient.cs | 2 +- .../InternshipRegistrationController.cs | 31 ++++++++------- .../Commands/UpdateRegistrationForm.cs | 13 ++++++- src/InternshipSystem.Core/Entity/Edition.cs | 9 +---- .../Entity/Internship/Internship.cs | 39 +++++-------------- .../Internship/InternshipRegistration.cs | 31 ++++++++++++--- .../InternshipSystem.Core.csproj | 4 ++ .../UglyOrmArtifacts/ProgramSubject.cs | 4 +- .../InternshipRegistrationValidator.cs | 34 ++++++++++++++++ .../DatabaseFiller.cs | 6 --- .../InternshipDbContext.cs | 8 ++-- 12 files changed, 111 insertions(+), 78 deletions(-) create mode 100644 src/InternshipSystem.Core/Validators/InternshipRegistrationValidator.cs diff --git a/src/InternshipSystem.Api/Controllers/AccessController.cs b/src/InternshipSystem.Api/Controllers/AccessController.cs index 69164e3..f734947 100644 --- a/src/InternshipSystem.Api/Controllers/AccessController.cs +++ b/src/InternshipSystem.Api/Controllers/AccessController.cs @@ -42,15 +42,9 @@ namespace InternshipSystem.Api.Controllers public async Task Authenticate(string code, CancellationToken cancellationToken) { var token = await _loginClient.GetCasTokenAsync(code, cancellationToken); - var casData = await _loginClient.GetProfileAsync(token, cancellationToken); - if (!long.TryParse(casData.PersonNumber, out var id)) - { - return BadRequest(); - } - - var student = await _context.Students.FirstOrDefaultAsync(s => s.Id == id); + var student = await _context.Students.FirstOrDefaultAsync(s => s.Id == long.Parse(casData.PersonNumber)); if (student == null) { diff --git a/src/InternshipSystem.Api/Controllers/GutCasClient.cs b/src/InternshipSystem.Api/Controllers/GutCasClient.cs index 08f0714..bee05ba 100644 --- a/src/InternshipSystem.Api/Controllers/GutCasClient.cs +++ b/src/InternshipSystem.Api/Controllers/GutCasClient.cs @@ -67,7 +67,7 @@ namespace InternshipSystem.Api.Controllers new JsonSerializerOptions { PropertyNameCaseInsensitive = true - }); + }, cancellationToken); return result.Attributes; } diff --git a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs index d6a2bd1..68a8203 100644 --- a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs +++ b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs @@ -1,13 +1,10 @@ using System.Threading; using System.Threading.Tasks; -using InternshipSystem.Api.Queries; using InternshipSystem.Api.Security; using InternshipSystem.Core.Commands; using InternshipSystem.Repository; using Microsoft.AspNetCore.Authorization; -using InternshipSystem.Api.Security; using InternshipSystem.Api.Services; -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -18,10 +15,12 @@ namespace InternshipSystem.Api.Controllers public class InternshipRegistrationController : ControllerBase { private readonly IInternshipService _internshipService; + private readonly InternshipDbContext _context; - public InternshipRegistrationController(IInternshipService internshipService) + public InternshipRegistrationController(IInternshipService internshipService, InternshipDbContext context) { _internshipService = internshipService; + _context = context; } /// @@ -35,19 +34,21 @@ namespace InternshipSystem.Api.Controllers [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [Authorize] - public async Task SubmitRegistrationForm([FromBody] RegistrationFormQuery registrationQuery, - [FromServices] User user, CancellationToken cancellationToken) + [Authorize(Policy = Policies.RegisteredOnly)] + public async Task SubmitRegistrationForm( + [FromBody] UpdateRegistrationForm registrationQuery, + [FromServices] User user, + CancellationToken cancellationToken) { - var validator = new RegistrationFormQuery.Validator(); - var validationResult = await validator.ValidateAsync(registrationQuery, cancellationToken); + var edition = await _context.Editions.FirstAsync(e => e.Id == user.EditionId, cancellationToken); - if (!validationResult.IsValid) - { - return BadRequest(validationResult.ToString()); - } - - return await _internshipService.SubmitRegistration(registrationQuery, user.PersonNumber, cancellationToken); + var internship = await _context + .Entry(edition) + .Collection(e => e.Internships) + .Query() + .FirstAsync(i => i.Student.Id == user.PersonNumber, cancellationToken); + + internship.UpdateInternshipRegistration(registrationQuery); } } } \ No newline at end of file diff --git a/src/InternshipSystem.Core/Commands/UpdateRegistrationForm.cs b/src/InternshipSystem.Core/Commands/UpdateRegistrationForm.cs index 4ff3a8d..2c79854 100644 --- a/src/InternshipSystem.Core/Commands/UpdateRegistrationForm.cs +++ b/src/InternshipSystem.Core/Commands/UpdateRegistrationForm.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using InternshipSystem.Core.Entity.Internship; namespace InternshipSystem.Core.Commands @@ -8,7 +9,17 @@ namespace InternshipSystem.Core.Commands public UpdateCompany? Company { get; set; } public DateTime? Start { get; set; } public DateTime? End { get; set; } - public InternshipType? Type { get; set; } + public InternshipType Type { get; set; } + public UpdateMentor Mentor { get; set; } + public List Subjects { get; set; } + } + + public struct UpdateMentor + { + public string FirstName { get; set; } + public string LastName { get; set; } + public string Email { get; set; } + public string PhoneNumber { get; set; } } public struct UpdateCompany diff --git a/src/InternshipSystem.Core/Entity/Edition.cs b/src/InternshipSystem.Core/Entity/Edition.cs index b6fa3cd..266eb1f 100644 --- a/src/InternshipSystem.Core/Entity/Edition.cs +++ b/src/InternshipSystem.Core/Entity/Edition.cs @@ -44,14 +44,9 @@ namespace InternshipSystem.Core Internships.Add(internship); } - public bool IsDateDuringEdition(DateTime start, DateTime end) + public void ValidateInternshipRegistration(InternshipRegistration internshipRegistration) { - return start >= EditionStart && end <= EditionFinish; - } - - public bool IsInternshiptypeAllowed(InternshipType internshipType) - { - return AllowedInternshipTypes.HasFlag(internshipType); + throw new NotImplementedException(); } } } \ No newline at end of file diff --git a/src/InternshipSystem.Core/Entity/Internship/Internship.cs b/src/InternshipSystem.Core/Entity/Internship/Internship.cs index 965835b..730603a 100644 --- a/src/InternshipSystem.Core/Entity/Internship/Internship.cs +++ b/src/InternshipSystem.Core/Entity/Internship/Internship.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using InternshipSystem.Core.Commands; +using InternshipSystem.Core.UglyOrmArtifacts; using InternshipSystem.Core.ValueObject; namespace InternshipSystem.Core @@ -11,8 +12,8 @@ namespace InternshipSystem.Core public long Id { get; set; } public Student Student { get; set; } public InternshipRegistration InternshipRegistration { get; set; } - public InternshipProgram InternshipProgram { get; set; } public Report Report { get; set; } + public List Approvals { get; set; } public List Documentation { get; set; } @@ -44,7 +45,6 @@ namespace InternshipSystem.Core internship.Student = student; internship.InternshipRegistration = InternshipRegistration.Create(); - internship.InternshipProgram = InternshipProgram.Create(); internship.Report = Report.Create(); internship.Approvals = new List(); internship.Documentation = new List(); @@ -54,35 +54,14 @@ namespace InternshipSystem.Core public void UpdateInternshipRegistration(UpdateRegistrationForm updateRegistration) { - var start = updateRegistration.Start ?? InternshipRegistration.Start; - var end = updateRegistration.End ?? InternshipRegistration.End; - - if (!Edition.IsDateDuringEdition(start, end)) - { - throw new ArgumentOutOfRangeException(nameof(InternshipRegistration.Start) + nameof(InternshipRegistration.End),"Date outside of edition boundaries"); - } - - var internshipType = updateRegistration.Type ?? InternshipRegistration.Type; - - if (!Edition.IsInternshiptypeAllowed(internshipType)) - { - throw new ArgumentException("Internship type not allowed for this edition", nameof(updateRegistration.Type)); - } - - var company = InternshipRegistration.Company; - if (company == null) - { - if (!updateRegistration.Company.HasValue) - { - throw new ArgumentException("Company"); - } - - company = Company.CreateCompany(updateRegistration.Company.Value); - - - } + InternshipRegistration.UpdateDates(updateRegistration.Start, updateRegistration.End); + InternshipRegistration.UpdateSubjects(updateRegistration.Subjects); + InternshipRegistration.UpdateInternshipType(updateRegistration.Type); - InternshipRegistration.Update(start, end, internshipType); + InternshipRegistration.UpdateCompany(updateRegistration.Company); + InternshipRegistration.UpdateMentor(updateRegistration.Mentor); + + Edition.ValidateInternshipRegistration(InternshipRegistration); } } } \ No newline at end of file diff --git a/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs b/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs index dc4b68a..ad46aeb 100644 --- a/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs +++ b/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; +using InternshipSystem.Core.Commands; using InternshipSystem.Core.Entity.Internship; +using InternshipSystem.Core.UglyOrmArtifacts; namespace InternshipSystem.Core { @@ -10,6 +13,9 @@ namespace InternshipSystem.Core public BranchOffice BranchAddress { get; set; } public DateTime Start { get; set; } public DateTime End { get; set; } + public Mentor Mentor { get; set; } + + public List ChosenSubjects { get; set; } public InternshipType Type { get; set; } public DocumentState State { get; set; } @@ -24,15 +30,30 @@ namespace InternshipSystem.Core End = end; Type = internshipType; } - - public void UpdateCompany(Company newCompany) + + public void UpdateCompany(UpdateCompany? updateRegistrationCompany) { - Company = newCompany; + throw new NotImplementedException(); } - public void UpdateBranch(BranchOffice branch) + public void UpdateDates(DateTime? updateRegistrationStart, DateTime? updateRegistrationEnd) { - BranchAddress = branch; + throw new NotImplementedException(); + } + + public void UpdateMentor(UpdateMentor updateRegistrationMentor) + { + throw new NotImplementedException(); + } + + public void UpdateSubjects(List updateRegistrationSubjects) + { + throw new NotImplementedException(); + } + + public void UpdateInternshipType(InternshipType updateRegistrationType) + { + throw new NotImplementedException(); } } } \ No newline at end of file diff --git a/src/InternshipSystem.Core/InternshipSystem.Core.csproj b/src/InternshipSystem.Core/InternshipSystem.Core.csproj index 6de04cb..68ef27b 100644 --- a/src/InternshipSystem.Core/InternshipSystem.Core.csproj +++ b/src/InternshipSystem.Core/InternshipSystem.Core.csproj @@ -5,4 +5,8 @@ latest + + + + diff --git a/src/InternshipSystem.Core/UglyOrmArtifacts/ProgramSubject.cs b/src/InternshipSystem.Core/UglyOrmArtifacts/ProgramSubject.cs index a3db121..4c5794d 100644 --- a/src/InternshipSystem.Core/UglyOrmArtifacts/ProgramSubject.cs +++ b/src/InternshipSystem.Core/UglyOrmArtifacts/ProgramSubject.cs @@ -4,8 +4,8 @@ namespace InternshipSystem.Core.UglyOrmArtifacts { public class ProgramSubject { - public long InternshipProgramId { get; set; } - public InternshipProgram Program { get; set; } + public long InternshipId { get; set; } + public InternshipRegistration InternshipRegistration { get; set; } public long InternshipSubjectId { get; set; } public InternshipSubject Subject { get; set; } } diff --git a/src/InternshipSystem.Core/Validators/InternshipRegistrationValidator.cs b/src/InternshipSystem.Core/Validators/InternshipRegistrationValidator.cs new file mode 100644 index 0000000..56bbfbf --- /dev/null +++ b/src/InternshipSystem.Core/Validators/InternshipRegistrationValidator.cs @@ -0,0 +1,34 @@ +using FluentValidation; +using FluentValidation.Validators; + +namespace InternshipSystem.Core.Validators +{ + public class InternshipRegistrationValidator : AbstractValidator + { + public InternshipRegistrationValidator(Edition edition) + { + RuleFor(ir => ir.Start) + .GreaterThanOrEqualTo(edition.EditionStart) + .LessThanOrEqualTo(edition.EditionFinish); + + RuleFor(ir => ir.End) + .GreaterThanOrEqualTo(ir => ir.Start) + .LessThanOrEqualTo(edition.EditionFinish); + + RuleFor(ir => ir.Company) + .SetValidator(new CompanyValidator()); + + RuleFor(ir => ir.BranchAddress) + .SetValidator(new BranchAddressValidator()); + + } + } + + public class BranchAddressValidator : AbstractValidator + { + } + + public class CompanyValidator : AbstractValidator + { + } +} \ No newline at end of file diff --git a/src/InternshipSystem.Repository/DatabaseFiller.cs b/src/InternshipSystem.Repository/DatabaseFiller.cs index ef31643..bb69c70 100644 --- a/src/InternshipSystem.Repository/DatabaseFiller.cs +++ b/src/InternshipSystem.Repository/DatabaseFiller.cs @@ -237,9 +237,6 @@ namespace InternshipSystem.Repository .First(c => c.Name.Equals("Intel")) .Branches .First(), - }, - InternshipProgram = new InternshipProgram - { Mentor = new Mentor { FirstName = "Horacy", @@ -286,9 +283,6 @@ namespace InternshipSystem.Repository .First(c => c.Name.Equals("Asseco Poland")) .Branches .First(), - }, - InternshipProgram = new InternshipProgram - { Mentor = new Mentor { FirstName = "Henryk", diff --git a/src/InternshipSystem.Repository/InternshipDbContext.cs b/src/InternshipSystem.Repository/InternshipDbContext.cs index 0e729fb..e8db3b5 100644 --- a/src/InternshipSystem.Repository/InternshipDbContext.cs +++ b/src/InternshipSystem.Repository/InternshipDbContext.cs @@ -27,18 +27,18 @@ namespace InternshipSystem.Repository modelBuilder.Entity() .OwnsOne(bo => bo.Address); - modelBuilder.Entity() + modelBuilder.Entity() .OwnsOne(ip => ip.Mentor); modelBuilder.Entity(builder => { builder - .HasKey(subject => new { subject.InternshipProgramId, subject.InternshipSubjectId }); + .HasKey(subject => new {subject.InternshipId, subject.InternshipSubjectId }); builder - .HasOne(k => k.Program) + .HasOne(k => k.InternshipRegistration) .WithMany(model => model.ChosenSubjects) - .HasForeignKey(subject => subject.InternshipProgramId); + .HasForeignKey(subject => subject.InternshipId); builder .HasOne(k => k.Subject)