diff --git a/src/InternshipSystem.Api/Controllers/EditionController.cs b/src/InternshipSystem.Api/Controllers/EditionController.cs index 67c2f81..b592984 100644 --- a/src/InternshipSystem.Api/Controllers/EditionController.cs +++ b/src/InternshipSystem.Api/Controllers/EditionController.cs @@ -88,6 +88,5 @@ namespace InternshipSystem.Api.Controllers return Ok(edition); } - } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs index cb48ea4..1d06be1 100644 --- a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs +++ b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs @@ -2,9 +2,13 @@ using System.Threading; using System.Threading.Tasks; using InternshipSystem.Api.Queries; +using InternshipSystem.Api.Security; +using InternshipSystem.Core.Commands; using InternshipSystem.Repository; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; namespace InternshipSystem.Api.Controllers { @@ -21,7 +25,7 @@ namespace InternshipSystem.Api.Controllers /// /// Validate and add filled internship registration form /// - /// Internship registration data + /// 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 @@ -29,7 +33,21 @@ namespace InternshipSystem.Api.Controllers [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] - public async Task SubmitRegistrationForm([FromBody] RegistrationFormQuery registrationQuery, CancellationToken cancellationToken) => - throw new NotImplementedException(); + [Authorize(Policy = Policies.RegisteredOnly)] + public async Task SubmitRegistrationForm( + [FromBody] UpdateRegistrationForm updateRegistration, + User user, + CancellationToken cancellationToken) + { + var edition = await Context.Editions.FindAsync(user.EditionId.Value); + + var internship = await Context + .Entry(edition) + .Collection(e => e.Internships) + .Query() + .FirstAsync(i => i.Student.Id == user.PersonNumber, cancellationToken); + + internship.UpdateInternshipRegistration(updateRegistration); + } } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Controllers/RegistrationController.cs b/src/InternshipSystem.Api/Controllers/RegistrationController.cs index 26931fc..3991b51 100644 --- a/src/InternshipSystem.Api/Controllers/RegistrationController.cs +++ b/src/InternshipSystem.Api/Controllers/RegistrationController.cs @@ -47,7 +47,7 @@ namespace InternshipSystem.Api.Controllers var student = await _context.Students.FindAsync(user.PersonNumber); edition.RegisterInternship(student); - await _context.SaveChangesAsync(); + await _context.SaveChangesAsync(token); return Ok(); } diff --git a/src/InternshipSystem.Api/Queries/RegistrationFormQuery.cs b/src/InternshipSystem.Api/Queries/RegistrationFormQuery.cs deleted file mode 100644 index fe30ded..0000000 --- a/src/InternshipSystem.Api/Queries/RegistrationFormQuery.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using InternshipSystem.Core; -using InternshipSystem.Core.Entity.Internship; - -namespace InternshipSystem.Api.Queries -{ - public class RegistrationFormQuery - { - public Company Company { get; set; } - public BranchOffice BranchAddress { get; set; } - public DateTime Start { get; set; } - public DateTime End { get; set; } - public InternshipType Type { get; set; } - } -} \ No newline at end of file diff --git a/src/InternshipSystem.Core/Commands/UpdateRegistrationForm.cs b/src/InternshipSystem.Core/Commands/UpdateRegistrationForm.cs new file mode 100644 index 0000000..4ff3a8d --- /dev/null +++ b/src/InternshipSystem.Core/Commands/UpdateRegistrationForm.cs @@ -0,0 +1,33 @@ +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/Company.cs b/src/InternshipSystem.Core/Entity/Company.cs index ddbeeb4..12a9168 100644 --- a/src/InternshipSystem.Core/Entity/Company.cs +++ b/src/InternshipSystem.Core/Entity/Company.cs @@ -1,5 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; +using InternshipSystem.Core.Commands; namespace InternshipSystem.Core { @@ -8,16 +8,13 @@ namespace InternshipSystem.Core public long Id { get; set; } public Nip Nip { get; set; } public string Name { get; set; } - public RangeOfActivity Range { get; set; } public List Branches { get; set; } - public Uri SiteAddress { get; set; } - public Company CreateCompany(string nip, RangeOfActivity range, string name) + public Company CreateCompany(string nip, string name) { return new Company { Nip = nip, - Range = range, Name = name }; } @@ -25,5 +22,9 @@ namespace InternshipSystem.Core public void AddBranchAddress(BranchAddress branch) { } + + public static Company CreateCompany(UpdateCompany updateCompany) + { + } } } \ No newline at end of file diff --git a/src/InternshipSystem.Core/Entity/Edition.cs b/src/InternshipSystem.Core/Entity/Edition.cs index 64172af..3c28a4b 100644 --- a/src/InternshipSystem.Core/Entity/Edition.cs +++ b/src/InternshipSystem.Core/Entity/Edition.cs @@ -13,6 +13,9 @@ namespace InternshipSystem.Core public DateTime ReportingStart { get; set; } public Course Course { get; set; } public List Internships { get; set; } + + public InternshipType AllowedInternshipTypes { get; set; } + public List AvailableSubjects { get; set; } public bool IsOpen => EditionFinish < DateTime.Today; @@ -33,5 +36,15 @@ namespace InternshipSystem.Core Internships.Add(internship); } + + public bool IsDateDuringEdition(DateTime start, DateTime end) + { + return start >= EditionStart && end <= EditionFinish; + } + + public bool IsInternshiptypeAllowed(InternshipType internshipType) + { + return AllowedInternshipTypes.HasFlag(internshipType); + } } } \ 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 b9981d8..15c5272 100644 --- a/src/InternshipSystem.Core/Entity/Internship/Internship.cs +++ b/src/InternshipSystem.Core/Entity/Internship/Internship.cs @@ -1,5 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; +using InternshipSystem.Core.Commands; using InternshipSystem.Core.ValueObject; namespace InternshipSystem.Core @@ -13,6 +15,9 @@ namespace InternshipSystem.Core public Report Report { get; set; } public List Approvals { get; set; } public List Documentation { get; set; } + + public Edition Edition { get; set; } + public float? Grade { get; set; } public void UpdateDocument(Document document) @@ -46,5 +51,38 @@ 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 e12679a..b0b447a 100644 --- a/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs +++ b/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs @@ -17,5 +17,12 @@ namespace InternshipSystem.Core { return new InternshipRegistration(); } + + public void Update(DateTime start, DateTime end, InternshipType internshipType) + { + Start = start; + End = end; + Type = internshipType; + } } } \ No newline at end of file diff --git a/src/InternshipSystem.Core/Entity/Internship/InternshipType.cs b/src/InternshipSystem.Core/Entity/Internship/InternshipType.cs index d9fb97f..5e85e23 100644 --- a/src/InternshipSystem.Core/Entity/Internship/InternshipType.cs +++ b/src/InternshipSystem.Core/Entity/Internship/InternshipType.cs @@ -1,9 +1,12 @@ -namespace InternshipSystem.Core.Entity.Internship +using System; + +namespace InternshipSystem.Core.Entity.Internship { - public class InternshipType + [Flags] + public enum InternshipType : long { - public long Id { get; set; } - public string Type { get; set; } - public string Description { get; set; } + None, + UOP, + UOZ } } \ No newline at end of file diff --git a/src/InternshipSystem.Core/Entity/Report.cs b/src/InternshipSystem.Core/Entity/Report.cs index 35f60a5..ab5df11 100644 --- a/src/InternshipSystem.Core/Entity/Report.cs +++ b/src/InternshipSystem.Core/Entity/Report.cs @@ -1,9 +1,13 @@ -namespace InternshipSystem.Core +using System; + +namespace InternshipSystem.Core { public class Report { public long Id { get; set; } public DocumentState State { get; set; } + public RangeOfActivity Range { get; set; } + public Uri SiteAddress { get; set; } public static Report Create() { diff --git a/src/InternshipSystem.Repository/DatabaseFiller.cs b/src/InternshipSystem.Repository/DatabaseFiller.cs index bee6e4b..99a270e 100644 --- a/src/InternshipSystem.Repository/DatabaseFiller.cs +++ b/src/InternshipSystem.Repository/DatabaseFiller.cs @@ -162,10 +162,7 @@ namespace InternshipSystem.Repository InternshipRegistration = new InternshipRegistration { Company = Context.Companies.First(c => c.Id.Equals(1)), //Intel - Type = new InternshipType - { - Type = "UOP" - }, + Type = InternshipType.UOP, Start = new DateTime(2000, 7, 1), End = new DateTime(2000, 8, 30), State = DocumentState.Submitted, @@ -215,10 +212,7 @@ namespace InternshipSystem.Repository InternshipRegistration = new InternshipRegistration { Company = Context.Companies.First(c => c.Id.Equals(2)), //Asseco - Type = new InternshipType - { - Type = "UZ" - }, + Type = InternshipType.UOZ, Start = new DateTime(2000, 7, 1), End = new DateTime(2000, 8, 30), State = DocumentState.Submitted,