From 59840ffd5c0a4d7a983feb39c3c4415b9ce64554 Mon Sep 17 00:00:00 2001 From: MaxchilKH Date: Wed, 30 Sep 2020 21:21:03 +0200 Subject: [PATCH 1/3] finaly --- .../Commands/UpdateRegistrationForm.cs | 52 +++++++ .../InternshipRegistrationController.cs | 53 ++++--- .../Services/IInternshipService.cs | 3 - .../Services/InternshipService.cs | 57 -------- .../UpdateInternshipRegistrationUseCase.cs | 129 ++++++++++++++++++ .../Commands/UpdateRegistrationForm.cs | 33 ----- .../Entity/BranchOffice.cs | 27 +++- src/InternshipSystem.Core/Entity/Company.cs | 22 +-- .../Entity/Internship/Internship.cs | 34 ----- .../Internship/InternshipRegistration.cs | 51 +++++-- .../InternshipSystem.Core.csproj | 4 + .../UglyOrmArtifacts/ProgramSubject.cs | 4 +- .../ValueObject/BranchAddress.cs | 22 ++- .../ValueObject/DocumentState.cs | 2 +- .../ValueObject/Mentor.cs | 28 +++- .../InternshipDbContext.cs | 8 +- 16 files changed, 347 insertions(+), 182 deletions(-) create mode 100644 src/InternshipSystem.Api/Commands/UpdateRegistrationForm.cs create mode 100644 src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs delete mode 100644 src/InternshipSystem.Core/Commands/UpdateRegistrationForm.cs diff --git a/src/InternshipSystem.Api/Commands/UpdateRegistrationForm.cs b/src/InternshipSystem.Api/Commands/UpdateRegistrationForm.cs new file mode 100644 index 0000000..605d012 --- /dev/null +++ b/src/InternshipSystem.Api/Commands/UpdateRegistrationForm.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using IdentityServer4.Extensions; +using InternshipSystem.Core; +using InternshipSystem.Core.Entity.Internship; + +namespace InternshipSystem.Api.Commands +{ + public class UpdateRegistrationForm + { + public UpdateCompany? Company { get; set; } + public DateTime? Start { get; set; } + public DateTime? End { get; set; } + public UpdateMentor? Mentor { get; set; } + public List Subjects { get; set; } + public InternshipType Type { 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 + { + public long? Id { get; set; } + public string Nip { get; set; } + public string Name { get; set; } + public UpdateBranchOffice? BranchOffice { get; set; } + + public bool IsCustomUpdate => !Nip.IsNullOrEmpty() || !Name.IsNullOrEmpty(); + } + + public struct UpdateBranchOffice + { + public long? Id { get; set; } + public string Street { get; set; } + public string Building { get; set; } + public string City { get; set; } + public string PostalCode { get; set; } + public string Country { get; set; } + + public bool IsCustomUpdate => !string.IsNullOrEmpty(Street) || + !string.IsNullOrEmpty(Building) || + !string.IsNullOrEmpty(City) || + !string.IsNullOrEmpty(PostalCode) || + !string.IsNullOrEmpty(Country); + } +} \ No newline at end of file diff --git a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs index d6a2bd1..bcaf82f 100644 --- a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs +++ b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs @@ -1,13 +1,11 @@ -using System.Threading; +using System.Linq; +using System.Threading; using System.Threading.Tasks; -using InternshipSystem.Api.Queries; +using InternshipSystem.Api.Commands; using InternshipSystem.Api.Security; -using InternshipSystem.Core.Commands; +using InternshipSystem.Api.UseCases; 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; @@ -17,17 +15,16 @@ namespace InternshipSystem.Api.Controllers [Route("internshipRegistration")] public class InternshipRegistrationController : ControllerBase { - private readonly IInternshipService _internshipService; + private readonly InternshipDbContext _dbContext; - public InternshipRegistrationController(IInternshipService internshipService) + public InternshipRegistrationController(InternshipDbContext dbContext) { - _internshipService = internshipService; + _dbContext = dbContext; } - + /// /// Validate and add filled internship registration form /// - /// Internship registration data /// If registration form was successfully added /// If the provided registration query was malformed /// This action is only available for authorized student registered for current edition @@ -35,19 +32,33 @@ 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( + UpdateRegistrationForm registrationCommand, + [FromServices] User user, + CancellationToken cancellationToken) { - var validator = new RegistrationFormQuery.Validator(); - var validationResult = await validator.ValidateAsync(registrationQuery, cancellationToken); + var edition = await _dbContext.Editions.FirstAsync(e => e.Id == user.EditionId, cancellationToken); - if (!validationResult.IsValid) - { - return BadRequest(validationResult.ToString()); - } + var internshipRegistration = + await _dbContext + .Entry(edition) + .Collection(e => e.Internships) + .Query() + .Where(i => i.Student.Id == user.PersonNumber) + .Select(i => i.InternshipRegistration) + .Include(r => r.BranchAddress) + .Include(r => r.Company) + .ThenInclude(c => c.Branches) + .FirstAsync(cancellationToken); + + var useCase = new UpdateInternshipRegistrationUseCase(_dbContext, internshipRegistration, edition, user); + + var result = await useCase.UpdateInternshipRegistration(registrationCommand, cancellationToken); + + await _dbContext.SaveChangesAsync(cancellationToken); - return await _internshipService.SubmitRegistration(registrationQuery, user.PersonNumber, cancellationToken); + return Ok(result); } } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Services/IInternshipService.cs b/src/InternshipSystem.Api/Services/IInternshipService.cs index 614e053..12c86bc 100644 --- a/src/InternshipSystem.Api/Services/IInternshipService.cs +++ b/src/InternshipSystem.Api/Services/IInternshipService.cs @@ -7,9 +7,6 @@ namespace InternshipSystem.Api.Services { public interface IInternshipService { - Task SubmitRegistration(RegistrationFormQuery registrationQuery, long personNumber, - CancellationToken cancellationToken); - Task AddDocumentToInternship(DocumentPublishRequest documentRequest, long personNumber, CancellationToken cancellationToken); } diff --git a/src/InternshipSystem.Api/Services/InternshipService.cs b/src/InternshipSystem.Api/Services/InternshipService.cs index 51eafc5..1cda9f0 100644 --- a/src/InternshipSystem.Api/Services/InternshipService.cs +++ b/src/InternshipSystem.Api/Services/InternshipService.cs @@ -21,63 +21,6 @@ namespace InternshipSystem.Api.Services Mapper = mapper; } - public async Task SubmitRegistration(RegistrationFormQuery registrationQuery, long personNumber, - CancellationToken cancellationToken) - { - var edition = await _context.Editions.FindAsync(personNumber); - - var internship = await _context.Entry(edition) - .Collection(e => e.Internships) - .Query() - .SingleAsync(i => i.Student.Id == personNumber, cancellationToken); - - var internshipRegistration = internship.InternshipRegistration; - - if (registrationQuery.Company != null) - { - var company = registrationQuery.Company.Id.HasValue - ? await _context.Companies.SingleAsync(c => c.Id == registrationQuery.Company.Id, - cancellationToken: cancellationToken) - : Company.CreateCompany(registrationQuery.Company.Nip, registrationQuery.Company.Name); - - internshipRegistration.UpdateCompany(company); - } - - var officeForm = registrationQuery.BranchOffice; - if (officeForm != null) - { - BranchOffice branch; - - if (officeForm.Id.HasValue) - { - branch = await _context.Entry(internshipRegistration.Company) - .Collection(c => c.Branches) - .Query() - .SingleAsync(o => o.Id == officeForm.Id, cancellationToken: cancellationToken); - } - else - { - branch = BranchOffice.CreateBranch(officeForm.Country, officeForm.City, officeForm.PostalCode, - officeForm.Street, officeForm.Building); - internshipRegistration.Company.AddBranchOffice(branch); - } - - internshipRegistration.UpdateBranch(branch); - } - - internshipRegistration.Start = registrationQuery.Start ?? internshipRegistration.Start; - internshipRegistration.End = registrationQuery.End ?? internshipRegistration.End; - - if (registrationQuery.Type != null && edition.IsInternshipTypeAllowed(registrationQuery.Type)) - { - return new BadRequestObjectResult("Edition doesn't have this type of employment in available employments type"); - } - - internshipRegistration.Type = registrationQuery.Type ?? internshipRegistration.Type; - - await _context.SaveChangesAsync(cancellationToken); - return new OkResult(); - } public async Task AddDocumentToInternship(DocumentPublishRequest documentRequest, long personNumber, CancellationToken cancellationToken) diff --git a/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs b/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs new file mode 100644 index 0000000..1eca45a --- /dev/null +++ b/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs @@ -0,0 +1,129 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using IdentityServer4.Extensions; +using InternshipSystem.Api.Commands; +using InternshipSystem.Api.Security; +using InternshipSystem.Core; +using InternshipSystem.Core.UglyOrmArtifacts; +using InternshipSystem.Repository; +using Microsoft.EntityFrameworkCore; + +namespace InternshipSystem.Api.UseCases +{ + public class UpdateInternshipRegistrationUseCase + { + private readonly InternshipDbContext _dbContext; + private readonly Edition _edition; + private readonly User _user; + private readonly InternshipRegistration subjectRegistration; + + public UpdateInternshipRegistrationUseCase(InternshipDbContext dbContext, + InternshipRegistration internshipRegistration, Edition edition, User user) + { + _dbContext = dbContext; + _edition = edition; + _user = user; + subjectRegistration = internshipRegistration; + } + + public async Task UpdateInternshipRegistration( + UpdateRegistrationForm registrationCommand, + CancellationToken cancellationToken) + { + subjectRegistration.Start = registrationCommand.Start ?? subjectRegistration.Start; + subjectRegistration.End = registrationCommand.End ?? subjectRegistration.End; + subjectRegistration.Type = registrationCommand.Type ?? subjectRegistration.Type; + + if (registrationCommand.Mentor.HasValue) + { + UpdateMentor(registrationCommand.Mentor.Value); + } + + if (!registrationCommand.Subjects.IsNullOrEmpty()) + { + UpdateSubjects(registrationCommand.Subjects); + } + + if (registrationCommand.Company.HasValue) + { + await UpdateCompanyAndBranch(registrationCommand.Company.Value, cancellationToken); + } + + return subjectRegistration.ValidateStatus(_edition); + } + + private async Task UpdateCompanyAndBranch(UpdateCompany companyUpdate, CancellationToken cancellationToken) + { + var company = subjectRegistration.Company; + + if (companyUpdate.Id.HasValue) + { + company = await _dbContext.Companies + .Include(c => c.Branches) + .FirstAsync(c => c.Id == companyUpdate.Id.Value, cancellationToken); + } + else if (companyUpdate.IsCustomUpdate) + { + company = await _dbContext.Companies + .Include(c => c.Branches) + .SingleOrDefaultAsync(c => c.Provider == _user.PersonNumber, cancellationToken) + ?? Company.CreateCompany(companyUpdate.Nip, companyUpdate.Name, _user.PersonNumber); + + company.Name = companyUpdate.Name ?? company.Name; + company.Nip = companyUpdate.Nip ?? company.Nip; + } + + if (companyUpdate.BranchOffice.HasValue) + { + var branchUpdate = companyUpdate.BranchOffice.Value; + + var branch = subjectRegistration.BranchAddress; + + if (branchUpdate.Id.HasValue) + { + branch = company.Branches.First(b => b.Id == branchUpdate.Id.Value); + } + else if (branchUpdate.IsCustomUpdate) + { + branch = company.Branches.FirstOrDefault(b => b.Provider == _user.PersonNumber); + + if (branch == null) + { + branch = BranchOffice.CreateBranch(branchUpdate.Country, branchUpdate.City, branchUpdate.PostalCode, + branchUpdate.Street, branchUpdate.Building, _user.PersonNumber); + company.AddBranchOffice(branch); + } + + branch.Address.Country = branchUpdate.Country ?? branch.Address.Country; + branch.Address.City = branchUpdate.City ?? branch.Address.City; + branch.Address.PostalCode = branchUpdate.PostalCode ?? branch.Address.PostalCode; + branch.Address.Street = branchUpdate.Country ?? branch.Address.Street; + branch.Address.Building = branchUpdate.Building ?? branch.Address.Building; + } + + subjectRegistration.BranchAddress = branch; + } + + subjectRegistration.Company = company; + } + + private void UpdateSubjects(IEnumerable subjects) + { + subjectRegistration.Subjects = + subjects + .Select(i => new ProgramSubject + { + Registration = subjectRegistration, + InternshipSubjectId = i.Id + }) + .ToList(); + } + + private void UpdateMentor(UpdateMentor mentorUpdate) + { + subjectRegistration.Mentor.UpdateInformation(mentorUpdate.FirstName, mentorUpdate.LastName, mentorUpdate.Email, mentorUpdate.PhoneNumber); + } + } +} \ No newline at end of file diff --git a/src/InternshipSystem.Core/Commands/UpdateRegistrationForm.cs b/src/InternshipSystem.Core/Commands/UpdateRegistrationForm.cs deleted file mode 100644 index 4ff3a8d..0000000 --- a/src/InternshipSystem.Core/Commands/UpdateRegistrationForm.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using InternshipSystem.Core.Entity.Internship; - -namespace InternshipSystem.Core.Commands -{ - public class UpdateRegistrationForm - { - public UpdateCompany? Company { get; set; } - public DateTime? Start { get; set; } - public DateTime? End { get; set; } - public InternshipType? Type { get; set; } - } - - public struct UpdateCompany - { - public long? Id { get; set; } - public string Nip { get; set; } - public string Name { get; set; } - public UpdateBranchOffice? BranchOffice { get; set; } - - public bool IsUpdate => Id.HasValue; - } - - public struct UpdateBranchOffice - { - public long? Id { get; set; } - public string Street { get; set; } - public string Building { get; set; } - public string City { get; set; } - public string PostalCode { get; set; } - public string Country { get; set; } - } -} \ No newline at end of file diff --git a/src/InternshipSystem.Core/Entity/BranchOffice.cs b/src/InternshipSystem.Core/Entity/BranchOffice.cs index 78e41c8..0ef4726 100644 --- a/src/InternshipSystem.Core/Entity/BranchOffice.cs +++ b/src/InternshipSystem.Core/Entity/BranchOffice.cs @@ -1,31 +1,48 @@ -namespace InternshipSystem.Core +using FluentValidation; +using FluentValidation.Validators; + +namespace InternshipSystem.Core { public class BranchOffice { public BranchOffice() { } - private BranchOffice(BranchAddress address) + + private BranchOffice(BranchAddress address, long provider) { Address = address; + Provider = provider; } public long Id { get; set; } public BranchAddress Address { get; set; } - public static BranchOffice CreateBranch(string country, string city, string postalCode, string street, string building) + public long Provider { get; set; } + + public static BranchOffice CreateBranch(string country, string city, string postalCode, string street, + string building, long provider = 0) { var address = new BranchAddress { - Building = building, + Building = building, City = city, Country = country, Street = street, PostalCode = postalCode }; - return new BranchOffice(address); + return new BranchOffice(address, provider); + } + + public class Validator : AbstractValidator + { + public Validator() + { + RuleFor(x => x.Address) + .SetValidator(new BranchAddress.Validator()); + } } } } \ No newline at end of file diff --git a/src/InternshipSystem.Core/Entity/Company.cs b/src/InternshipSystem.Core/Entity/Company.cs index d722dfd..a34d9da 100644 --- a/src/InternshipSystem.Core/Entity/Company.cs +++ b/src/InternshipSystem.Core/Entity/Company.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; -using InternshipSystem.Core.Commands; +using FluentValidation; +using FluentValidation.Validators; namespace InternshipSystem.Core { @@ -11,25 +12,28 @@ namespace InternshipSystem.Core public string Name { get; set; } public List Branches { get; set; } - public static Company CreateCompany(string nip, string name) => + public long Provider { get; set; } + + public static Company CreateCompany(string nip, string name, long provider = 0) => new Company { Nip = nip, - Name = name + Name = name, + Provider = provider }; - public void AddBranchAddress(BranchAddress branch) - { - } - public void AddBranchOffice(BranchOffice createBranch) { Branches.Add(createBranch); } - public static Company CreateCompany(UpdateCompany updateCompany) + public class Validator : AbstractValidator { - throw new NotImplementedException(); + public Validator() + { + RuleFor(x => x.Nip).NotNull(); + RuleFor(x => x.Name).NotNull(); + } } } } \ 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 be8d8a6..24bd08b 100644 --- a/src/InternshipSystem.Core/Entity/Internship/Internship.cs +++ b/src/InternshipSystem.Core/Entity/Internship/Internship.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using InternshipSystem.Core.Commands; using InternshipSystem.Core.ValueObject; namespace InternshipSystem.Core @@ -51,38 +50,5 @@ namespace InternshipSystem.Core return internship; } - - 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.Update(start, end, internshipType); - } } } \ 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..b485088 100644 --- a/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs +++ b/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs @@ -1,5 +1,9 @@ using System; +using System.Collections.Generic; +using FluentValidation; +using FluentValidation.Results; using InternshipSystem.Core.Entity.Internship; +using InternshipSystem.Core.UglyOrmArtifacts; namespace InternshipSystem.Core { @@ -10,6 +14,8 @@ 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 Subjects { get; set; } public InternshipType Type { get; set; } public DocumentState State { get; set; } @@ -18,21 +24,44 @@ namespace InternshipSystem.Core return new InternshipRegistration(); } - public void Update(DateTime start, DateTime end, InternshipType internshipType) + + public string ValidateStatus(Edition edition) { - Start = start; - End = end; - Type = internshipType; - } - - public void UpdateCompany(Company newCompany) - { - Company = newCompany; + var validator = new Validator(edition); + + var result = validator.Validate(this); + + State = result.IsValid ? DocumentState.Submitted : DocumentState.Draft; + + return result.ToString(); } - public void UpdateBranch(BranchOffice branch) + public class Validator : AbstractValidator { - BranchAddress = branch; + public Validator(Edition edition) + { + RuleFor(x => x.Company) + .SetValidator(new Company.Validator()) + .NotNull(); + RuleFor(x => x.BranchAddress) + .SetValidator(new BranchOffice.Validator()) + .NotNull(); + RuleFor(x => x.Mentor) + .SetValidator(new Mentor.Validate()) + .NotNull(); + RuleFor(x => x.Subjects) + .NotEmpty(); + RuleFor(x => x.Type) + .NotNull(); + RuleFor(x => x.Start) + .GreaterThanOrEqualTo(edition.EditionStart) + .LessThan(x => x.End) + .NotEmpty(); + RuleFor(x => x.End) + .LessThanOrEqualTo(edition.EditionFinish) + .GreaterThan(x => x.Start) + .NotEmpty(); + } } } } \ 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..0a2e7dd 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 InternshipRegistrationId { get; set; } + public InternshipRegistration Registration { get; set; } public long InternshipSubjectId { get; set; } public InternshipSubject Subject { get; set; } } diff --git a/src/InternshipSystem.Core/ValueObject/BranchAddress.cs b/src/InternshipSystem.Core/ValueObject/BranchAddress.cs index 5e80133..f3be361 100644 --- a/src/InternshipSystem.Core/ValueObject/BranchAddress.cs +++ b/src/InternshipSystem.Core/ValueObject/BranchAddress.cs @@ -1,4 +1,7 @@ -namespace InternshipSystem.Core +using FluentValidation; +using FluentValidation.Validators; + +namespace InternshipSystem.Core { public class BranchAddress { @@ -7,5 +10,22 @@ public string City { get; set; } public string PostalCode { get; set; } public string Country { get; set; } + + public class Validator : AbstractValidator + { + public Validator() + { + RuleFor(x => x.Country) + .NotEmpty(); + RuleFor(x => x.City) + .NotEmpty(); + RuleFor(x => x.PostalCode) + .NotEmpty(); + RuleFor(x => x.Street) + .NotEmpty(); + RuleFor(x => x.Building) + .NotEmpty(); + } + } } } \ No newline at end of file diff --git a/src/InternshipSystem.Core/ValueObject/DocumentState.cs b/src/InternshipSystem.Core/ValueObject/DocumentState.cs index 2a8d863..465aa58 100644 --- a/src/InternshipSystem.Core/ValueObject/DocumentState.cs +++ b/src/InternshipSystem.Core/ValueObject/DocumentState.cs @@ -2,7 +2,7 @@ { public enum DocumentState { - NotSubmitted, + Draft, Submitted, Accepted, Rejected diff --git a/src/InternshipSystem.Core/ValueObject/Mentor.cs b/src/InternshipSystem.Core/ValueObject/Mentor.cs index d7be213..bae5499 100644 --- a/src/InternshipSystem.Core/ValueObject/Mentor.cs +++ b/src/InternshipSystem.Core/ValueObject/Mentor.cs @@ -1,4 +1,6 @@ -namespace InternshipSystem.Core +using FluentValidation; + +namespace InternshipSystem.Core { public class Mentor { @@ -6,5 +8,29 @@ public string LastName { get; set; } public string Email { get; set; } public string PhoneNumber { get; set; } + + public void UpdateInformation(string firstName, string lastName, string email, string phoneNumber) + { + FirstName = firstName ?? FirstName; + LastName = lastName ?? LastName; + Email = email ?? Email; + PhoneNumber = phoneNumber ?? PhoneNumber; + } + + public class Validate : AbstractValidator + { + public Validate() + { + RuleFor(x => x.FirstName) + .NotEmpty(); + RuleFor(x => x.LastName) + .NotEmpty(); + RuleFor(x => x.Email) + .NotEmpty(); + RuleFor(x => x.PhoneNumber) + .NotEmpty(); + + } + } } } \ No newline at end of file diff --git a/src/InternshipSystem.Repository/InternshipDbContext.cs b/src/InternshipSystem.Repository/InternshipDbContext.cs index 0e729fb..f9d8576 100644 --- a/src/InternshipSystem.Repository/InternshipDbContext.cs +++ b/src/InternshipSystem.Repository/InternshipDbContext.cs @@ -33,12 +33,12 @@ namespace InternshipSystem.Repository modelBuilder.Entity(builder => { builder - .HasKey(subject => new { subject.InternshipProgramId, subject.InternshipSubjectId }); + .HasKey(subject => new { InternshipProgramId = subject.InternshipRegistrationId, subject.InternshipSubjectId }); builder - .HasOne(k => k.Program) - .WithMany(model => model.ChosenSubjects) - .HasForeignKey(subject => subject.InternshipProgramId); + .HasOne(k => k.Registration) + .WithMany(model => model.Subjects) + .HasForeignKey(subject => subject.InternshipRegistrationId); builder .HasOne(k => k.Subject) -- 2.45.2 From 29e4928a736fcecc7f25ed8754283c1cae00ac03 Mon Sep 17 00:00:00 2001 From: MaxchilKH Date: Wed, 30 Sep 2020 21:38:45 +0200 Subject: [PATCH 2/3] gtfo --- .../Services/IInternshipService.cs | 3 +-- .../Services/InternshipService.cs | 3 +-- test/InternshipSystem.Api.Test/InternshipSystem.cs | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/InternshipSystem.Api/Services/IInternshipService.cs b/src/InternshipSystem.Api/Services/IInternshipService.cs index 042eef0..e3a7294 100644 --- a/src/InternshipSystem.Api/Services/IInternshipService.cs +++ b/src/InternshipSystem.Api/Services/IInternshipService.cs @@ -8,7 +8,6 @@ namespace InternshipSystem.Api.Services { public interface IInternshipService { - Task AddDocumentToInternship(DocumentPublishRequest documentRequest, long personNumber, - CancellationToken cancellationToken); + Task AddDocumentToInternship(DocumentPublishRequest documentRequest, User user, CancellationToken cancellationToken); } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Services/InternshipService.cs b/src/InternshipSystem.Api/Services/InternshipService.cs index b7766cd..3ea6a7a 100644 --- a/src/InternshipSystem.Api/Services/InternshipService.cs +++ b/src/InternshipSystem.Api/Services/InternshipService.cs @@ -21,8 +21,7 @@ namespace InternshipSystem.Api.Services _context = context; Mapper = mapper; } - - + public async Task AddDocumentToInternship(DocumentPublishRequest documentRequest, User user, CancellationToken cancellationToken) { diff --git a/test/InternshipSystem.Api.Test/InternshipSystem.cs b/test/InternshipSystem.Api.Test/InternshipSystem.cs index 2033085..3e3632f 100644 --- a/test/InternshipSystem.Api.Test/InternshipSystem.cs +++ b/test/InternshipSystem.Api.Test/InternshipSystem.cs @@ -2,7 +2,9 @@ using System; using System.Collections.Generic; using System.Text.Json; using InternshipSystem.Api.Controllers; +using InternshipSystem.Repository; using Machine.Specifications; +using Microsoft.EntityFrameworkCore; namespace InternshipSystem.Api.Test { @@ -56,4 +58,16 @@ namespace InternshipSystem.Api.Test private static JsonSerializerOptions options; private static CasUserProfile result; } + + class When_writing_tests_only_for_debug_because_i_gave_up_on_code_quality + { + private Establish context = () => + { + var db = new InternshipDbContext(new DbContextOptionsBuilder() + .UseNpgsql("Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=szwoniu") + .Options); + + var e + }; + } } -- 2.45.2 From ccc92437045afac517ed1d276ffed99c615885a4 Mon Sep 17 00:00:00 2001 From: MaxchilKH Date: Fri, 2 Oct 2020 19:52:42 +0200 Subject: [PATCH 3/3] XDDDEEEE --- .../Commands/UpdateRegistrationForm.cs | 2 +- .../InternshipRegistrationController.cs | 5 +- .../UpdateInternshipRegistrationUseCase.cs | 4 +- .../Entity/Internship/Internship.cs | 2 - .../Entity/Internship/InternshipProgram.cs | 19 ---- .../DatabaseFiller.cs | 10 +- .../InternshipDbContext.cs | 5 +- ....cs => 20201002175217_Initial.Designer.cs} | 103 +++++++---------- ...4840_init.cs => 20201002175217_Initial.cs} | 90 ++++++--------- .../InternshipDbContextModelSnapshot.cs | 99 ++++++---------- .../InternshipSystem.cs | 107 +++++++++++++++++- 11 files changed, 224 insertions(+), 222 deletions(-) delete mode 100644 src/InternshipSystem.Core/Entity/Internship/InternshipProgram.cs rename src/InternshipSystem.Repository/Migrations/{20200927114840_init.Designer.cs => 20201002175217_Initial.Designer.cs} (92%) rename src/InternshipSystem.Repository/Migrations/{20200927114840_init.cs => 20201002175217_Initial.cs} (92%) diff --git a/src/InternshipSystem.Api/Commands/UpdateRegistrationForm.cs b/src/InternshipSystem.Api/Commands/UpdateRegistrationForm.cs index 605d012..ddb6e5b 100644 --- a/src/InternshipSystem.Api/Commands/UpdateRegistrationForm.cs +++ b/src/InternshipSystem.Api/Commands/UpdateRegistrationForm.cs @@ -12,7 +12,7 @@ namespace InternshipSystem.Api.Commands public DateTime? Start { get; set; } public DateTime? End { get; set; } public UpdateMentor? Mentor { get; set; } - public List Subjects { get; set; } + public List Subjects { get; set; } public InternshipType Type { get; set; } } diff --git a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs index 0fd4a25..9c7a49e 100644 --- a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs +++ b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs @@ -51,8 +51,10 @@ namespace InternshipSystem.Api.Controllers .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) - .ThenInclude(c => c.Branches) + .Include(r => r.Company.Branches) .FirstAsync(cancellationToken); var useCase = new UpdateInternshipRegistrationUseCase(_context, internshipRegistration, edition, user); @@ -82,7 +84,6 @@ namespace InternshipSystem.Api.Controllers .Include(i => i.InternshipRegistration.Company) .Include(i => i.InternshipRegistration.BranchAddress) .Include(i => i.InternshipRegistration.Type) - .Include(i => i.InternshipProgram) .Include(i => i.Report) .Include(i => i.Approvals) .Include(i => i.Documentation) diff --git a/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs b/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs index 1eca45a..c7d1793 100644 --- a/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs +++ b/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs @@ -109,14 +109,14 @@ namespace InternshipSystem.Api.UseCases subjectRegistration.Company = company; } - private void UpdateSubjects(IEnumerable subjects) + private void UpdateSubjects(IEnumerable subjects) { subjectRegistration.Subjects = subjects .Select(i => new ProgramSubject { Registration = subjectRegistration, - InternshipSubjectId = i.Id + InternshipSubjectId = i }) .ToList(); } diff --git a/src/InternshipSystem.Core/Entity/Internship/Internship.cs b/src/InternshipSystem.Core/Entity/Internship/Internship.cs index 24bd08b..f511d1d 100644 --- a/src/InternshipSystem.Core/Entity/Internship/Internship.cs +++ b/src/InternshipSystem.Core/Entity/Internship/Internship.cs @@ -10,7 +10,6 @@ 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; } @@ -43,7 +42,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(); diff --git a/src/InternshipSystem.Core/Entity/Internship/InternshipProgram.cs b/src/InternshipSystem.Core/Entity/Internship/InternshipProgram.cs deleted file mode 100644 index f5fc545..0000000 --- a/src/InternshipSystem.Core/Entity/Internship/InternshipProgram.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections.Generic; -using InternshipSystem.Core.Entity.Internship; -using InternshipSystem.Core.UglyOrmArtifacts; - -namespace InternshipSystem.Core -{ - public class InternshipProgram - { - public long Id { get; set; } - public Mentor Mentor { get; set; } - public DocumentState State { get; set; } - public List ChosenSubjects { get; set; } - - public static InternshipProgram Create() - { - return new InternshipProgram(); - } - } -} \ No newline at end of file diff --git a/src/InternshipSystem.Repository/DatabaseFiller.cs b/src/InternshipSystem.Repository/DatabaseFiller.cs index 64b0621..fba4bf7 100644 --- a/src/InternshipSystem.Repository/DatabaseFiller.cs +++ b/src/InternshipSystem.Repository/DatabaseFiller.cs @@ -245,9 +245,6 @@ namespace InternshipSystem.Repository .First(c => c.Name.Equals("Intel")) .Branches .First(), - }, - InternshipProgram = new InternshipProgram - { Mentor = new Mentor { FirstName = "Horacy", @@ -255,7 +252,7 @@ namespace InternshipSystem.Repository Email = "howos@intel.com", PhoneNumber = "605-555-555", }, - ChosenSubjects = new List + Subjects = new List { new ProgramSubject { @@ -294,9 +291,6 @@ namespace InternshipSystem.Repository .First(c => c.Name.Equals("Asseco Poland")) .Branches .First(), - }, - InternshipProgram = new InternshipProgram - { Mentor = new Mentor { FirstName = "Henryk", @@ -304,7 +298,7 @@ namespace InternshipSystem.Repository Email = "hepol@asseco.pl", PhoneNumber = "555-525-545", }, - ChosenSubjects = new List + Subjects = new List { new ProgramSubject { diff --git a/src/InternshipSystem.Repository/InternshipDbContext.cs b/src/InternshipSystem.Repository/InternshipDbContext.cs index f9d8576..4d7560e 100644 --- a/src/InternshipSystem.Repository/InternshipDbContext.cs +++ b/src/InternshipSystem.Repository/InternshipDbContext.cs @@ -27,8 +27,9 @@ namespace InternshipSystem.Repository modelBuilder.Entity() .OwnsOne(bo => bo.Address); - modelBuilder.Entity() - .OwnsOne(ip => ip.Mentor); + + modelBuilder.Entity() + .OwnsOne(ir => ir.Mentor); modelBuilder.Entity(builder => { diff --git a/src/InternshipSystem.Repository/Migrations/20200927114840_init.Designer.cs b/src/InternshipSystem.Repository/Migrations/20201002175217_Initial.Designer.cs similarity index 92% rename from src/InternshipSystem.Repository/Migrations/20200927114840_init.Designer.cs rename to src/InternshipSystem.Repository/Migrations/20201002175217_Initial.Designer.cs index 79cf8f6..872b214 100644 --- a/src/InternshipSystem.Repository/Migrations/20200927114840_init.Designer.cs +++ b/src/InternshipSystem.Repository/Migrations/20201002175217_Initial.Designer.cs @@ -10,8 +10,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace InternshipSystem.Repository.Migrations { [DbContext(typeof(InternshipDbContext))] - [Migration("20200927114840_init")] - partial class init + [Migration("20201002175217_Initial")] + partial class Initial { protected override void BuildTargetModel(ModelBuilder modelBuilder) { @@ -33,6 +33,10 @@ namespace InternshipSystem.Repository.Migrations .HasColumnName("company_id") .HasColumnType("bigint"); + b.Property("Provider") + .HasColumnName("provider") + .HasColumnType("bigint"); + b.HasKey("Id") .HasName("pk_branch_office"); @@ -58,6 +62,10 @@ namespace InternshipSystem.Repository.Migrations .HasColumnName("nip") .HasColumnType("text"); + b.Property("Provider") + .HasColumnName("provider") + .HasColumnType("bigint"); + b.HasKey("Id") .HasName("pk_companies"); @@ -240,10 +248,6 @@ namespace InternshipSystem.Repository.Migrations .HasColumnName("grade") .HasColumnType("real"); - b.Property("InternshipProgramId") - .HasColumnName("internship_program_id") - .HasColumnType("bigint"); - b.Property("InternshipRegistrationId") .HasColumnName("internship_registration_id") .HasColumnType("bigint"); @@ -262,9 +266,6 @@ namespace InternshipSystem.Repository.Migrations b.HasIndex("EditionId") .HasName("ix_internship_edition_id"); - b.HasIndex("InternshipProgramId") - .HasName("ix_internship_internship_program_id"); - b.HasIndex("InternshipRegistrationId") .HasName("ix_internship_internship_registration_id"); @@ -277,24 +278,6 @@ namespace InternshipSystem.Repository.Migrations b.ToTable("internship"); }); - modelBuilder.Entity("InternshipSystem.Core.InternshipProgram", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnName("id") - .HasColumnType("bigint") - .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - - b.Property("State") - .HasColumnName("state") - .HasColumnType("integer"); - - b.HasKey("Id") - .HasName("pk_internship_program"); - - b.ToTable("internship_program"); - }); - modelBuilder.Entity("InternshipSystem.Core.InternshipRegistration", b => { b.Property("Id") @@ -461,15 +444,15 @@ namespace InternshipSystem.Repository.Migrations modelBuilder.Entity("InternshipSystem.Core.UglyOrmArtifacts.ProgramSubject", b => { - b.Property("InternshipProgramId") - .HasColumnName("internship_program_id") + b.Property("InternshipRegistrationId") + .HasColumnName("internship_registration_id") .HasColumnType("bigint"); b.Property("InternshipSubjectId") .HasColumnName("internship_subject_id") .HasColumnType("bigint"); - b.HasKey("InternshipProgramId", "InternshipSubjectId") + b.HasKey("InternshipRegistrationId", "InternshipSubjectId") .HasName("pk_program_subject"); b.HasIndex("InternshipSubjectId") @@ -565,11 +548,6 @@ namespace InternshipSystem.Repository.Migrations .HasForeignKey("EditionId") .HasConstraintName("fk_internship_editions_edition_id"); - b.HasOne("InternshipSystem.Core.InternshipProgram", "InternshipProgram") - .WithMany() - .HasForeignKey("InternshipProgramId") - .HasConstraintName("fk_internship_internship_program_internship_program_id"); - b.HasOne("InternshipSystem.Core.InternshipRegistration", "InternshipRegistration") .WithMany() .HasForeignKey("InternshipRegistrationId") @@ -586,11 +564,26 @@ namespace InternshipSystem.Repository.Migrations .HasConstraintName("fk_internship_students_student_id"); }); - modelBuilder.Entity("InternshipSystem.Core.InternshipProgram", b => + modelBuilder.Entity("InternshipSystem.Core.InternshipRegistration", b => { + b.HasOne("InternshipSystem.Core.BranchOffice", "BranchAddress") + .WithMany() + .HasForeignKey("BranchAddressId") + .HasConstraintName("fk_internship_registration_branch_office_branch_address_id"); + + b.HasOne("InternshipSystem.Core.Company", "Company") + .WithMany() + .HasForeignKey("CompanyId") + .HasConstraintName("fk_internship_registration_companies_company_id"); + + b.HasOne("InternshipSystem.Core.Entity.Internship.InternshipType", "Type") + .WithMany() + .HasForeignKey("TypeId") + .HasConstraintName("fk_internship_registration_internship_types_type_id"); + b.OwnsOne("InternshipSystem.Core.Mentor", "Mentor", b1 => { - b1.Property("InternshipProgramId") + b1.Property("InternshipRegistrationId") .ValueGeneratedOnAdd() .HasColumnName("id") .HasColumnType("bigint") @@ -612,35 +605,17 @@ namespace InternshipSystem.Repository.Migrations .HasColumnName("phone_number") .HasColumnType("text"); - b1.HasKey("InternshipProgramId") - .HasName("pk_internship_program"); + b1.HasKey("InternshipRegistrationId") + .HasName("pk_internship_registration"); - b1.ToTable("internship_program"); + b1.ToTable("internship_registration"); b1.WithOwner() - .HasForeignKey("InternshipProgramId") - .HasConstraintName("fk_mentor_internship_program_internship_program_id"); + .HasForeignKey("InternshipRegistrationId") + .HasConstraintName("fk_mentor_internship_registration_internship_registration_id"); }); }); - modelBuilder.Entity("InternshipSystem.Core.InternshipRegistration", b => - { - b.HasOne("InternshipSystem.Core.BranchOffice", "BranchAddress") - .WithMany() - .HasForeignKey("BranchAddressId") - .HasConstraintName("fk_internship_registration_branch_office_branch_address_id"); - - b.HasOne("InternshipSystem.Core.Company", "Company") - .WithMany() - .HasForeignKey("CompanyId") - .HasConstraintName("fk_internship_registration_companies_company_id"); - - b.HasOne("InternshipSystem.Core.Entity.Internship.InternshipType", "Type") - .WithMany() - .HasForeignKey("TypeId") - .HasConstraintName("fk_internship_registration_internship_types_type_id"); - }); - modelBuilder.Entity("InternshipSystem.Core.UglyOrmArtifacts.EditionSubject", b => { b.HasOne("InternshipSystem.Core.Edition", "Edition") @@ -660,10 +635,10 @@ namespace InternshipSystem.Repository.Migrations modelBuilder.Entity("InternshipSystem.Core.UglyOrmArtifacts.ProgramSubject", b => { - b.HasOne("InternshipSystem.Core.InternshipProgram", "Program") - .WithMany("ChosenSubjects") - .HasForeignKey("InternshipProgramId") - .HasConstraintName("fk_program_subject_internship_program_internship_program_id") + b.HasOne("InternshipSystem.Core.InternshipRegistration", "Registration") + .WithMany("Subjects") + .HasForeignKey("InternshipRegistrationId") + .HasConstraintName("fk_program_subject_internship_registration_internship_registrat") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); diff --git a/src/InternshipSystem.Repository/Migrations/20200927114840_init.cs b/src/InternshipSystem.Repository/Migrations/20201002175217_Initial.cs similarity index 92% rename from src/InternshipSystem.Repository/Migrations/20200927114840_init.cs rename to src/InternshipSystem.Repository/Migrations/20201002175217_Initial.cs index 623be9c..3e03b42 100644 --- a/src/InternshipSystem.Repository/Migrations/20200927114840_init.cs +++ b/src/InternshipSystem.Repository/Migrations/20201002175217_Initial.cs @@ -4,7 +4,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace InternshipSystem.Repository.Migrations { - public partial class init : Migration + public partial class Initial : Migration { protected override void Up(MigrationBuilder migrationBuilder) { @@ -15,7 +15,8 @@ namespace InternshipSystem.Repository.Migrations id = table.Column(nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), nip = table.Column(nullable: true), - name = table.Column(nullable: true) + name = table.Column(nullable: true), + provider = table.Column(nullable: false) }, constraints: table => { @@ -35,23 +36,6 @@ namespace InternshipSystem.Repository.Migrations table.PrimaryKey("pk_course", x => x.id); }); - migrationBuilder.CreateTable( - name: "internship_program", - columns: table => new - { - id = table.Column(nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - first_name = table.Column(nullable: true), - last_name = table.Column(nullable: true), - email = table.Column(nullable: true), - phone_number = table.Column(nullable: true), - state = table.Column(nullable: false) - }, - constraints: table => - { - table.PrimaryKey("pk_internship_program", x => x.id); - }); - migrationBuilder.CreateTable( name: "internship_subject", columns: table => new @@ -127,6 +111,7 @@ namespace InternshipSystem.Repository.Migrations city = table.Column(nullable: true), postal_code = table.Column(nullable: true), country = table.Column(nullable: true), + provider = table.Column(nullable: false), company_id = table.Column(nullable: true) }, constraints: table => @@ -140,30 +125,6 @@ namespace InternshipSystem.Repository.Migrations onDelete: ReferentialAction.Restrict); }); - migrationBuilder.CreateTable( - name: "program_subject", - columns: table => new - { - internship_program_id = table.Column(nullable: false), - internship_subject_id = table.Column(nullable: false) - }, - constraints: table => - { - table.PrimaryKey("pk_program_subject", x => new { x.internship_program_id, x.internship_subject_id }); - table.ForeignKey( - name: "fk_program_subject_internship_program_internship_program_id", - column: x => x.internship_program_id, - principalTable: "internship_program", - principalColumn: "id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "fk_program_subject_internship_subject_internship_subject_id", - column: x => x.internship_subject_id, - principalTable: "internship_subject", - principalColumn: "id", - onDelete: ReferentialAction.Cascade); - }); - migrationBuilder.CreateTable( name: "internship_registration", columns: table => new @@ -174,6 +135,10 @@ namespace InternshipSystem.Repository.Migrations branch_address_id = table.Column(nullable: true), start = table.Column(nullable: false), end = table.Column(nullable: false), + first_name = table.Column(nullable: true), + last_name = table.Column(nullable: true), + email = table.Column(nullable: true), + phone_number = table.Column(nullable: true), type_id = table.Column(nullable: true), state = table.Column(nullable: false) }, @@ -194,6 +159,30 @@ namespace InternshipSystem.Repository.Migrations onDelete: ReferentialAction.Restrict); }); + migrationBuilder.CreateTable( + name: "program_subject", + columns: table => new + { + internship_registration_id = table.Column(nullable: false), + internship_subject_id = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("pk_program_subject", x => new { x.internship_registration_id, x.internship_subject_id }); + table.ForeignKey( + name: "fk_program_subject_internship_registration_internship_registrat", + column: x => x.internship_registration_id, + principalTable: "internship_registration", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "fk_program_subject_internship_subject_internship_subject_id", + column: x => x.internship_subject_id, + principalTable: "internship_subject", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateTable( name: "editions", columns: table => new @@ -248,7 +237,6 @@ namespace InternshipSystem.Repository.Migrations .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), student_id = table.Column(nullable: true), internship_registration_id = table.Column(nullable: true), - internship_program_id = table.Column(nullable: true), report_id = table.Column(nullable: true), edition_id = table.Column(nullable: true), grade = table.Column(nullable: true) @@ -262,12 +250,6 @@ namespace InternshipSystem.Repository.Migrations principalTable: "editions", principalColumn: "id", onDelete: ReferentialAction.Restrict); - table.ForeignKey( - name: "fk_internship_internship_program_internship_program_id", - column: x => x.internship_program_id, - principalTable: "internship_program", - principalColumn: "id", - onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "fk_internship_internship_registration_internship_registration_", column: x => x.internship_registration_id, @@ -376,11 +358,6 @@ namespace InternshipSystem.Repository.Migrations table: "internship", column: "edition_id"); - migrationBuilder.CreateIndex( - name: "ix_internship_internship_program_id", - table: "internship", - column: "internship_program_id"); - migrationBuilder.CreateIndex( name: "ix_internship_internship_registration_id", table: "internship", @@ -462,9 +439,6 @@ namespace InternshipSystem.Repository.Migrations migrationBuilder.DropTable( name: "internship_subject"); - migrationBuilder.DropTable( - name: "internship_program"); - migrationBuilder.DropTable( name: "internship_registration"); diff --git a/src/InternshipSystem.Repository/Migrations/InternshipDbContextModelSnapshot.cs b/src/InternshipSystem.Repository/Migrations/InternshipDbContextModelSnapshot.cs index 529a966..e2dc33c 100644 --- a/src/InternshipSystem.Repository/Migrations/InternshipDbContextModelSnapshot.cs +++ b/src/InternshipSystem.Repository/Migrations/InternshipDbContextModelSnapshot.cs @@ -31,6 +31,10 @@ namespace InternshipSystem.Repository.Migrations .HasColumnName("company_id") .HasColumnType("bigint"); + b.Property("Provider") + .HasColumnName("provider") + .HasColumnType("bigint"); + b.HasKey("Id") .HasName("pk_branch_office"); @@ -56,6 +60,10 @@ namespace InternshipSystem.Repository.Migrations .HasColumnName("nip") .HasColumnType("text"); + b.Property("Provider") + .HasColumnName("provider") + .HasColumnType("bigint"); + b.HasKey("Id") .HasName("pk_companies"); @@ -238,10 +246,6 @@ namespace InternshipSystem.Repository.Migrations .HasColumnName("grade") .HasColumnType("real"); - b.Property("InternshipProgramId") - .HasColumnName("internship_program_id") - .HasColumnType("bigint"); - b.Property("InternshipRegistrationId") .HasColumnName("internship_registration_id") .HasColumnType("bigint"); @@ -260,9 +264,6 @@ namespace InternshipSystem.Repository.Migrations b.HasIndex("EditionId") .HasName("ix_internship_edition_id"); - b.HasIndex("InternshipProgramId") - .HasName("ix_internship_internship_program_id"); - b.HasIndex("InternshipRegistrationId") .HasName("ix_internship_internship_registration_id"); @@ -275,24 +276,6 @@ namespace InternshipSystem.Repository.Migrations b.ToTable("internship"); }); - modelBuilder.Entity("InternshipSystem.Core.InternshipProgram", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnName("id") - .HasColumnType("bigint") - .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - - b.Property("State") - .HasColumnName("state") - .HasColumnType("integer"); - - b.HasKey("Id") - .HasName("pk_internship_program"); - - b.ToTable("internship_program"); - }); - modelBuilder.Entity("InternshipSystem.Core.InternshipRegistration", b => { b.Property("Id") @@ -459,15 +442,15 @@ namespace InternshipSystem.Repository.Migrations modelBuilder.Entity("InternshipSystem.Core.UglyOrmArtifacts.ProgramSubject", b => { - b.Property("InternshipProgramId") - .HasColumnName("internship_program_id") + b.Property("InternshipRegistrationId") + .HasColumnName("internship_registration_id") .HasColumnType("bigint"); b.Property("InternshipSubjectId") .HasColumnName("internship_subject_id") .HasColumnType("bigint"); - b.HasKey("InternshipProgramId", "InternshipSubjectId") + b.HasKey("InternshipRegistrationId", "InternshipSubjectId") .HasName("pk_program_subject"); b.HasIndex("InternshipSubjectId") @@ -563,11 +546,6 @@ namespace InternshipSystem.Repository.Migrations .HasForeignKey("EditionId") .HasConstraintName("fk_internship_editions_edition_id"); - b.HasOne("InternshipSystem.Core.InternshipProgram", "InternshipProgram") - .WithMany() - .HasForeignKey("InternshipProgramId") - .HasConstraintName("fk_internship_internship_program_internship_program_id"); - b.HasOne("InternshipSystem.Core.InternshipRegistration", "InternshipRegistration") .WithMany() .HasForeignKey("InternshipRegistrationId") @@ -584,11 +562,26 @@ namespace InternshipSystem.Repository.Migrations .HasConstraintName("fk_internship_students_student_id"); }); - modelBuilder.Entity("InternshipSystem.Core.InternshipProgram", b => + modelBuilder.Entity("InternshipSystem.Core.InternshipRegistration", b => { + b.HasOne("InternshipSystem.Core.BranchOffice", "BranchAddress") + .WithMany() + .HasForeignKey("BranchAddressId") + .HasConstraintName("fk_internship_registration_branch_office_branch_address_id"); + + b.HasOne("InternshipSystem.Core.Company", "Company") + .WithMany() + .HasForeignKey("CompanyId") + .HasConstraintName("fk_internship_registration_companies_company_id"); + + b.HasOne("InternshipSystem.Core.Entity.Internship.InternshipType", "Type") + .WithMany() + .HasForeignKey("TypeId") + .HasConstraintName("fk_internship_registration_internship_types_type_id"); + b.OwnsOne("InternshipSystem.Core.Mentor", "Mentor", b1 => { - b1.Property("InternshipProgramId") + b1.Property("InternshipRegistrationId") .ValueGeneratedOnAdd() .HasColumnName("id") .HasColumnType("bigint") @@ -610,35 +603,17 @@ namespace InternshipSystem.Repository.Migrations .HasColumnName("phone_number") .HasColumnType("text"); - b1.HasKey("InternshipProgramId") - .HasName("pk_internship_program"); + b1.HasKey("InternshipRegistrationId") + .HasName("pk_internship_registration"); - b1.ToTable("internship_program"); + b1.ToTable("internship_registration"); b1.WithOwner() - .HasForeignKey("InternshipProgramId") - .HasConstraintName("fk_mentor_internship_program_internship_program_id"); + .HasForeignKey("InternshipRegistrationId") + .HasConstraintName("fk_mentor_internship_registration_internship_registration_id"); }); }); - modelBuilder.Entity("InternshipSystem.Core.InternshipRegistration", b => - { - b.HasOne("InternshipSystem.Core.BranchOffice", "BranchAddress") - .WithMany() - .HasForeignKey("BranchAddressId") - .HasConstraintName("fk_internship_registration_branch_office_branch_address_id"); - - b.HasOne("InternshipSystem.Core.Company", "Company") - .WithMany() - .HasForeignKey("CompanyId") - .HasConstraintName("fk_internship_registration_companies_company_id"); - - b.HasOne("InternshipSystem.Core.Entity.Internship.InternshipType", "Type") - .WithMany() - .HasForeignKey("TypeId") - .HasConstraintName("fk_internship_registration_internship_types_type_id"); - }); - modelBuilder.Entity("InternshipSystem.Core.UglyOrmArtifacts.EditionSubject", b => { b.HasOne("InternshipSystem.Core.Edition", "Edition") @@ -658,10 +633,10 @@ namespace InternshipSystem.Repository.Migrations modelBuilder.Entity("InternshipSystem.Core.UglyOrmArtifacts.ProgramSubject", b => { - b.HasOne("InternshipSystem.Core.InternshipProgram", "Program") - .WithMany("ChosenSubjects") - .HasForeignKey("InternshipProgramId") - .HasConstraintName("fk_program_subject_internship_program_internship_program_id") + b.HasOne("InternshipSystem.Core.InternshipRegistration", "Registration") + .WithMany("Subjects") + .HasForeignKey("InternshipRegistrationId") + .HasConstraintName("fk_program_subject_internship_registration_internship_registrat") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); diff --git a/test/InternshipSystem.Api.Test/InternshipSystem.cs b/test/InternshipSystem.Api.Test/InternshipSystem.cs index 3e3632f..6041122 100644 --- a/test/InternshipSystem.Api.Test/InternshipSystem.cs +++ b/test/InternshipSystem.Api.Test/InternshipSystem.cs @@ -1,10 +1,16 @@ -using System; using System.Collections.Generic; +using System.Linq; using System.Text.Json; +using System.Threading; +using InternshipSystem.Api.Commands; using InternshipSystem.Api.Controllers; +using InternshipSystem.Api.Security; +using InternshipSystem.Api.UseCases; +using InternshipSystem.Core; using InternshipSystem.Repository; using Machine.Specifications; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; namespace InternshipSystem.Api.Test { @@ -59,15 +65,112 @@ namespace InternshipSystem.Api.Test private static CasUserProfile result; } + class When_doint_whatever + { + private Establish context = () => + { + var db = new InternshipDbContext(new DbContextOptionsBuilder() + .UseLoggerFactory(LoggerFactory.Create(b => b.AddConsole())) + .UseNpgsql("Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=szwoniu") + .Options); + + var company = Company.CreateCompany("a", "b"); + + var internship = db.Editions.First(); + + + db.Companies.Add(company); + + db.SaveChanges(); + }; + + private It should_whatev = () => true.ShouldBeTrue(); + } + class When_writing_tests_only_for_debug_because_i_gave_up_on_code_quality { private Establish context = () => { var db = new InternshipDbContext(new DbContextOptionsBuilder() + .UseLoggerFactory(LoggerFactory.Create(b => b.AddConsole())) .UseNpgsql("Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=szwoniu") .Options); + + var ed = db.Editions.First(); + + var user = new User + { + PersonNumber = 1 + }; - var e + var ir = db.Entry(ed) + .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) + .First(); + + var useCase = new UpdateInternshipRegistrationUseCase(db, ir, ed, user); + + var update = new UpdateRegistrationForm + { + Mentor = new UpdateMentor + { + FirstName = "Cwalina" + }, + Subjects = new List + { + 1, + 2, + 3 + }, + Company = new UpdateCompany + { + Name = "a", + BranchOffice = new UpdateBranchOffice + { + Street = "b" + } + }, + + }; + + var task = useCase.UpdateInternshipRegistration(update, CancellationToken.None); + + task.Wait(); + + var result = task.Result; + + db.SaveChanges(); + + ir = db.Entry(ed) + .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) + .First(); }; + + private It should_nop = () => true.ShouldBeTrue(); } } -- 2.45.2