LOOOOOOOOOOOOL

This commit is contained in:
MaxchilKH 2020-09-13 20:16:08 +02:00
parent 54dfcaa7e7
commit d3691fb0a8
12 changed files with 111 additions and 78 deletions

View File

@ -42,15 +42,9 @@ namespace InternshipSystem.Api.Controllers
public async Task<ActionResult> 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)
{

View File

@ -67,7 +67,7 @@ namespace InternshipSystem.Api.Controllers
new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
}, cancellationToken);
return result.Attributes;
}

View File

@ -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;
}
/// <summary>
@ -35,19 +34,21 @@ namespace InternshipSystem.Api.Controllers
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[Authorize]
public async Task<ActionResult> SubmitRegistrationForm([FromBody] RegistrationFormQuery registrationQuery,
[FromServices] User user, CancellationToken cancellationToken)
[Authorize(Policy = Policies.RegisteredOnly)]
public async Task<ActionResult> 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);
}
}
}

View File

@ -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<InternshipSubject> 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

View File

@ -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();
}
}
}

View File

@ -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<Document> Approvals { get; set; }
public List<Document> 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<Document>();
internship.Documentation = new List<Document>();
@ -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);
}
}
}

View File

@ -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<ProgramSubject> 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<InternshipSubject> updateRegistrationSubjects)
{
throw new NotImplementedException();
}
public void UpdateInternshipType(InternshipType updateRegistrationType)
{
throw new NotImplementedException();
}
}
}

View File

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

View File

@ -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; }
}

View File

@ -0,0 +1,34 @@
using FluentValidation;
using FluentValidation.Validators;
namespace InternshipSystem.Core.Validators
{
public class InternshipRegistrationValidator : AbstractValidator<InternshipRegistration>
{
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<BranchOffice>
{
}
public class CompanyValidator : AbstractValidator<Company>
{
}
}

View File

@ -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",

View File

@ -27,18 +27,18 @@ namespace InternshipSystem.Repository
modelBuilder.Entity<BranchOffice>()
.OwnsOne(bo => bo.Address);
modelBuilder.Entity<InternshipProgram>()
modelBuilder.Entity<InternshipRegistration>()
.OwnsOne(ip => ip.Mentor);
modelBuilder.Entity<ProgramSubject>(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)