doroboty (#53)

XDDDEEEE

gtfo

Merge branch 'master' of http://git.kadet.net/system-praktyk/system-praktyk-api into doroboty

finaly

Co-authored-by: MaxchilKH <m.w.bohdanowicz@gmail.com>
This commit is contained in:
maxchil 2020-10-02 19:56:53 +02:00
parent 0b27330931
commit 8514e593fa
22 changed files with 583 additions and 406 deletions

View File

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

View File

@ -1,9 +1,12 @@
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.Api.UseCases;
using InternshipSystem.Repository;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using InternshipSystem.Api.Services;
using InternshipSystem.Core;
using Microsoft.AspNetCore.Http;
@ -15,19 +18,16 @@ namespace InternshipSystem.Api.Controllers
[Route("internshipRegistration")]
public class InternshipRegistrationController : ControllerBase
{
private readonly IInternshipService _internshipService;
private readonly InternshipDbContext _context;
public InternshipRegistrationController(IInternshipService internshipService, InternshipDbContext context)
{
_internshipService = internshipService;
_context = context;
}
public InternshipRegistrationController(InternshipDbContext dbContext)
{
_context = dbContext;
}
/// <summary>
/// Validate and add filled internship registration form
/// </summary>
/// <param name="updateRegistration">Internship registration data</param>
/// <response code="200">If registration form was successfully added</response>
/// <response code="400">If the provided registration query was malformed</response>
/// <response code="401">This action is only available for authorized student registered for current edition</response>
@ -36,18 +36,34 @@ namespace InternshipSystem.Api.Controllers
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[Authorize(Policy = Policies.RegisteredOnly)]
public async Task<ActionResult> SubmitRegistrationForm([FromBody] RegistrationFormQuery registrationQuery,
[FromServices] User user, CancellationToken cancellationToken)
public async Task<ActionResult> SubmitRegistrationForm(
UpdateRegistrationForm registrationCommand,
[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());
}
var internshipRegistration =
await _context
.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.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 await _internshipService.SubmitRegistration(registrationQuery, user, cancellationToken);
return Ok(result);
}
[HttpGet]
@ -68,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)

View File

@ -8,10 +8,6 @@ namespace InternshipSystem.Api.Services
{
public interface IInternshipService
{
Task<ActionResult> SubmitRegistration(RegistrationFormQuery registrationQuery, User user,
CancellationToken cancellationToken);
Task<ActionResult> AddDocumentToInternship(DocumentPublishRequest documentRequest, User user,
CancellationToken cancellationToken);
Task<ActionResult> AddDocumentToInternship(DocumentPublishRequest documentRequest, User user, CancellationToken cancellationToken);
}
}

View File

@ -21,70 +21,7 @@ namespace InternshipSystem.Api.Services
_context = context;
Mapper = mapper;
}
public async Task<ActionResult> SubmitRegistration(RegistrationFormQuery registrationQuery, User user,
CancellationToken cancellationToken)
{
var edition = await _context.Editions
.Include(e => e.AvailableInternshipTypes)
.FirstOrDefaultAsync(e => e.Id.Equals(user.EditionId), cancellationToken: cancellationToken);
var internship = await _context.Entry(edition)
.Collection(e => e.Internships)
.Query()
.Include(i => i.InternshipRegistration)
.Include(i => i.InternshipRegistration.Company)
.Include(i => i.InternshipRegistration.BranchAddress)
.SingleAsync(i => i.Student.Id == user.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 != null ? edition.AvailableInternshipTypes.Find(ai => ai.Id.Equals(registrationQuery.Type.Id)) : internshipRegistration.Type;
await _context.SaveChangesAsync(cancellationToken);
return new OkResult();
}
public async Task<ActionResult> AddDocumentToInternship(DocumentPublishRequest documentRequest, User user,
CancellationToken cancellationToken)
{

View File

@ -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<string> 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<long> subjects)
{
subjectRegistration.Subjects =
subjects
.Select(i => new ProgramSubject
{
Registration = subjectRegistration,
InternshipSubjectId = i
})
.ToList();
}
private void UpdateMentor(UpdateMentor mentorUpdate)
{
subjectRegistration.Mentor.UpdateInformation(mentorUpdate.FirstName, mentorUpdate.LastName, mentorUpdate.Email, mentorUpdate.PhoneNumber);
}
}
}

View File

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

View File

@ -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<BranchOffice>
{
public Validator()
{
RuleFor(x => x.Address)
.SetValidator(new BranchAddress.Validator());
}
}
}
}

View File

@ -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<BranchOffice> 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<Company>
{
throw new NotImplementedException();
public Validator()
{
RuleFor(x => x.Nip).NotNull();
RuleFor(x => x.Name).NotNull();
}
}
}
}

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using InternshipSystem.Core.Commands;
using InternshipSystem.Core.ValueObject;
namespace InternshipSystem.Core
@ -11,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<Document> Approvals { get; set; }
public List<Document> Documentation { get; set; }
@ -44,45 +42,11 @@ 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>();
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);
}
}
}

View File

@ -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<ProgramSubject> ChosenSubjects { get; set; }
public static InternshipProgram Create()
{
return new InternshipProgram();
}
}
}

View File

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

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 InternshipRegistrationId { get; set; }
public InternshipRegistration Registration { get; set; }
public long InternshipSubjectId { get; set; }
public InternshipSubject Subject { get; set; }
}

View File

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

View File

@ -3,7 +3,7 @@
public enum DocumentState
{
// Oczekujaca
NotSubmitted,
Draft,
// Oczekuje na akceptacje
Submitted,
// Zaakceptowana

View File

@ -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<Mentor>
{
public Validate()
{
RuleFor(x => x.FirstName)
.NotEmpty();
RuleFor(x => x.LastName)
.NotEmpty();
RuleFor(x => x.Email)
.NotEmpty();
RuleFor(x => x.PhoneNumber)
.NotEmpty();
}
}
}
}

View File

@ -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<ProgramSubject>
Subjects = new List<ProgramSubject>
{
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<ProgramSubject>
Subjects = new List<ProgramSubject>
{
new ProgramSubject
{

View File

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

View File

@ -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<long>("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<long>("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<long?>("InternshipProgramId")
.HasColumnName("internship_program_id")
.HasColumnType("bigint");
b.Property<long?>("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<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id")
.HasColumnType("bigint")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int>("State")
.HasColumnName("state")
.HasColumnType("integer");
b.HasKey("Id")
.HasName("pk_internship_program");
b.ToTable("internship_program");
});
modelBuilder.Entity("InternshipSystem.Core.InternshipRegistration", b =>
{
b.Property<long>("Id")
@ -461,15 +444,15 @@ namespace InternshipSystem.Repository.Migrations
modelBuilder.Entity("InternshipSystem.Core.UglyOrmArtifacts.ProgramSubject", b =>
{
b.Property<long>("InternshipProgramId")
.HasColumnName("internship_program_id")
b.Property<long>("InternshipRegistrationId")
.HasColumnName("internship_registration_id")
.HasColumnType("bigint");
b.Property<long>("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<long>("InternshipProgramId")
b1.Property<long>("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();

View File

@ -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<long>(nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
nip = table.Column<string>(nullable: true),
name = table.Column<string>(nullable: true)
name = table.Column<string>(nullable: true),
provider = table.Column<long>(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<long>(nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
first_name = table.Column<string>(nullable: true),
last_name = table.Column<string>(nullable: true),
email = table.Column<string>(nullable: true),
phone_number = table.Column<string>(nullable: true),
state = table.Column<int>(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<string>(nullable: true),
postal_code = table.Column<string>(nullable: true),
country = table.Column<string>(nullable: true),
provider = table.Column<long>(nullable: false),
company_id = table.Column<long>(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<long>(nullable: false),
internship_subject_id = table.Column<long>(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<long>(nullable: true),
start = table.Column<DateTime>(nullable: false),
end = table.Column<DateTime>(nullable: false),
first_name = table.Column<string>(nullable: true),
last_name = table.Column<string>(nullable: true),
email = table.Column<string>(nullable: true),
phone_number = table.Column<string>(nullable: true),
type_id = table.Column<long>(nullable: true),
state = table.Column<int>(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<long>(nullable: false),
internship_subject_id = table.Column<long>(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<long>(nullable: true),
internship_registration_id = table.Column<long>(nullable: true),
internship_program_id = table.Column<long>(nullable: true),
report_id = table.Column<long>(nullable: true),
edition_id = table.Column<Guid>(nullable: true),
grade = table.Column<float>(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");

View File

@ -31,6 +31,10 @@ namespace InternshipSystem.Repository.Migrations
.HasColumnName("company_id")
.HasColumnType("bigint");
b.Property<long>("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<long>("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<long?>("InternshipProgramId")
.HasColumnName("internship_program_id")
.HasColumnType("bigint");
b.Property<long?>("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<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("id")
.HasColumnType("bigint")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int>("State")
.HasColumnName("state")
.HasColumnType("integer");
b.HasKey("Id")
.HasName("pk_internship_program");
b.ToTable("internship_program");
});
modelBuilder.Entity("InternshipSystem.Core.InternshipRegistration", b =>
{
b.Property<long>("Id")
@ -459,15 +442,15 @@ namespace InternshipSystem.Repository.Migrations
modelBuilder.Entity("InternshipSystem.Core.UglyOrmArtifacts.ProgramSubject", b =>
{
b.Property<long>("InternshipProgramId")
.HasColumnName("internship_program_id")
b.Property<long>("InternshipRegistrationId")
.HasColumnName("internship_registration_id")
.HasColumnType("bigint");
b.Property<long>("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<long>("InternshipProgramId")
b1.Property<long>("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();

View File

@ -1,8 +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
{
@ -56,4 +64,113 @@ namespace InternshipSystem.Api.Test
private static JsonSerializerOptions options;
private static CasUserProfile result;
}
class When_doint_whatever
{
private Establish context = () =>
{
var db = new InternshipDbContext(new DbContextOptionsBuilder<InternshipDbContext>()
.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<InternshipDbContext>()
.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 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<long>
{
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();
}
}