diff --git a/src/InternshipSystem.Api/Controllers/CompaniesController.cs b/src/InternshipSystem.Api/Controllers/CompaniesController.cs index 430e81d..b23aaef 100644 --- a/src/InternshipSystem.Api/Controllers/CompaniesController.cs +++ b/src/InternshipSystem.Api/Controllers/CompaniesController.cs @@ -38,7 +38,7 @@ namespace InternshipSystem.Api.Controllers .ToListAsync(cancellationToken); /// - /// Get companies matching provided paginated query + /// Get company branches matching provided paginated query /// /// Paginated query description /// diff --git a/src/InternshipSystem.Api/Controllers/DocumentsController.cs b/src/InternshipSystem.Api/Controllers/DocumentsController.cs index dc9a22a..3406f9b 100644 --- a/src/InternshipSystem.Api/Controllers/DocumentsController.cs +++ b/src/InternshipSystem.Api/Controllers/DocumentsController.cs @@ -1,17 +1,11 @@ -using System; -using System.Diagnostics; -using System.Linq; -using System.Threading; +using System.Threading; using System.Threading.Tasks; -using AutoMapper; using InternshipSystem.Api.Queries; using InternshipSystem.Api.Security; -using InternshipSystem.Core; -using InternshipSystem.Repository; +using InternshipSystem.Api.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; namespace InternshipSystem.Api.Controllers { @@ -19,13 +13,11 @@ namespace InternshipSystem.Api.Controllers [Route("document")] public class DocumentsController : ControllerBase { - private InternshipDbContext Context { get; } - private IMapper Mapper { get; } + private readonly IInternshipService _internshipService; - public DocumentsController(InternshipDbContext context, IMapper mapper) + public DocumentsController(IInternshipService internshipService) { - Context = context; - Mapper = mapper; + _internshipService = internshipService; } /// @@ -54,34 +46,8 @@ namespace InternshipSystem.Api.Controllers } var personNumber = long.Parse(User.FindFirst(InternshipClaims.PersonNumber).Value); - - 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 document = Mapper.Map(documentRequest); - - if (documentRequest.Id.HasValue) - { - try - { - internship.UpdateDocument(document); - } - catch (InvalidOperationException) - { - return NotFound(); - } - } - else - { - internship.AddNewDocument(document); - } - - await Context.SaveChangesAsync(); - return Ok(); + return await _internshipService.AddDocumentToInternship(documentRequest, personNumber, cancellationToken); } } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs index 2926d6f..44c8eb6 100644 --- a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs +++ b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs @@ -1,29 +1,22 @@ -using System; -using System.Linq; -using System.Threading; +using System.Threading; using System.Threading.Tasks; -using AutoMapper; using InternshipSystem.Api.Queries; using InternshipSystem.Api.Security; -using InternshipSystem.Core; -using InternshipSystem.Repository; +using InternshipSystem.Api.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; namespace InternshipSystem.Api.Controllers { [Route("internshipRegistration")] public class InternshipRegistrationController : ControllerBase { - private InternshipDbContext Context { get; } - private IMapper Mapper { get; } + private readonly IInternshipService _internshipService; - public InternshipRegistrationController(InternshipDbContext context, IMapper mapper) + public InternshipRegistrationController(IInternshipService internshipService) { - Context = context; - Mapper = mapper; + _internshipService = internshipService; } /// @@ -50,60 +43,8 @@ namespace InternshipSystem.Api.Controllers } var personNumber = long.Parse(User.FindFirst(InternshipClaims.PersonNumber).Value); - - 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.HasValue && edition.IsInternshipTypeAllowed(registrationQuery.Type.Value)) - { - return BadRequest("Edition doesn't have this type of employment in available employments type"); - } - - internshipRegistration.Type = registrationQuery.Type ?? internshipRegistration.Type; - - await Context.SaveChangesAsync(cancellationToken); - return Ok(); + return await _internshipService.SubmitRegistration(registrationQuery, personNumber, cancellationToken); } } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Controllers/RegistrationController.cs b/src/InternshipSystem.Api/Controllers/RegistrationController.cs index 8ecc086..89d2cb2 100644 --- a/src/InternshipSystem.Api/Controllers/RegistrationController.cs +++ b/src/InternshipSystem.Api/Controllers/RegistrationController.cs @@ -1,9 +1,7 @@ using System; using System.Threading; using System.Threading.Tasks; -using InternshipSystem.Api.Result; using InternshipSystem.Api.Security; -using InternshipSystem.Core; using InternshipSystem.Repository; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; diff --git a/src/InternshipSystem.Api/Services/IInternshipService.cs b/src/InternshipSystem.Api/Services/IInternshipService.cs new file mode 100644 index 0000000..614e053 --- /dev/null +++ b/src/InternshipSystem.Api/Services/IInternshipService.cs @@ -0,0 +1,16 @@ +using System.Threading; +using System.Threading.Tasks; +using InternshipSystem.Api.Queries; +using Microsoft.AspNetCore.Mvc; + +namespace InternshipSystem.Api.Services +{ + public interface IInternshipService + { + Task SubmitRegistration(RegistrationFormQuery registrationQuery, long personNumber, + CancellationToken cancellationToken); + + Task AddDocumentToInternship(DocumentPublishRequest documentRequest, long personNumber, + CancellationToken cancellationToken); + } +} \ No newline at end of file diff --git a/src/InternshipSystem.Api/Services/InternshipService.cs b/src/InternshipSystem.Api/Services/InternshipService.cs new file mode 100644 index 0000000..4bcaa03 --- /dev/null +++ b/src/InternshipSystem.Api/Services/InternshipService.cs @@ -0,0 +1,114 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using AutoMapper; +using InternshipSystem.Api.Queries; +using InternshipSystem.Core; +using InternshipSystem.Repository; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace InternshipSystem.Api.Services +{ + public class InternshipService : IInternshipService + { + private readonly InternshipDbContext _context; + private IMapper Mapper { get; } + + public InternshipService(InternshipDbContext context, IMapper mapper) + { + _context = context; + 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.HasValue && edition.IsInternshipTypeAllowed(registrationQuery.Type.Value)) + { + 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) + { + 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 document = Mapper.Map(documentRequest); + + if (documentRequest.Id.HasValue) + { + try + { + internship.UpdateDocument(document); + } + catch (InvalidOperationException) + { + return new NotFoundResult(); + } + } + else + { + internship.AddNewDocument(document); + } + + await _context.SaveChangesAsync(cancellationToken); + return new OkResult(); + } + } +} \ No newline at end of file diff --git a/src/InternshipSystem.Api/Startup.cs b/src/InternshipSystem.Api/Startup.cs index af8297f..ab34720 100644 --- a/src/InternshipSystem.Api/Startup.cs +++ b/src/InternshipSystem.Api/Startup.cs @@ -6,6 +6,8 @@ using InternshipSystem.Api.Extensions; using InternshipSystem.Api.ModelBinders; using InternshipSystem.Api.Options; using InternshipSystem.Api.Security; +using InternshipSystem.Api.Services; +using InternshipSystem.Core; using InternshipSystem.Repository; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -36,6 +38,7 @@ namespace InternshipSystem.Api options.IncludeXmlComments(xmlPath); }) .AddScoped() + .AddScoped() .AddScoped() .AddAutoMapper(cfg => cfg.AddProfile()) .AddStudentAuthentication() diff --git a/src/InternshipSystem.Core/Entity/Edition.cs b/src/InternshipSystem.Core/Entity/Edition.cs index 53f64ad..d6ee476 100644 --- a/src/InternshipSystem.Core/Entity/Edition.cs +++ b/src/InternshipSystem.Core/Entity/Edition.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using InternshipSystem.Core.Entity.Internship; using InternshipSystem.Core.UglyOrmArtifacts; @@ -14,7 +15,7 @@ namespace InternshipSystem.Core public Course Course { get; set; } public List Internships { get; set; } public List AvailableSubjects { get; set; } - public List AvailableInternshipTypes { get; set; } + public List AvailableInternshipTypes { get; set; } public bool IsOpen => EditionFinish < DateTime.Today; @@ -30,7 +31,7 @@ namespace InternshipSystem.Core public bool IsInternshipTypeAllowed(InternshipType registrationQueryType) { - return AvailableInternshipTypes.Contains(registrationQueryType); + return AvailableInternshipTypes.Select(it => it.InternshipType).Contains(registrationQueryType); } public void RegisterInternship(Student student) diff --git a/src/InternshipSystem.Core/Entity/Internship/InternshipType.cs b/src/InternshipSystem.Core/Entity/Internship/InternshipType.cs index 0f554e0..e810e05 100644 --- a/src/InternshipSystem.Core/Entity/Internship/InternshipType.cs +++ b/src/InternshipSystem.Core/Entity/Internship/InternshipType.cs @@ -2,13 +2,13 @@ { public enum InternshipType { - FreeInternship, - GraduateInternship, - FreeApprenticeship, - PaidApprenticeship, - ForeignInternship, - UOP, - UD, - UZ + FreeInternship = 0, + GraduateInternship = 1, + FreeApprenticeship = 2, + PaidApprenticeship = 3, + ForeignInternship = 4, + UOP = 5, + UD = 6, + UZ = 7, } } \ No newline at end of file diff --git a/src/InternshipSystem.Core/UglyOrmArtifacts/EditionInternshipType.cs b/src/InternshipSystem.Core/UglyOrmArtifacts/EditionInternshipType.cs new file mode 100644 index 0000000..f8c03c2 --- /dev/null +++ b/src/InternshipSystem.Core/UglyOrmArtifacts/EditionInternshipType.cs @@ -0,0 +1,11 @@ +using System; +using InternshipSystem.Core.Entity.Internship; + +namespace InternshipSystem.Core.UglyOrmArtifacts +{ + public class EditionInternshipType + { + public long Id { get; set; } + public InternshipType InternshipType { get; set; } + } +} \ No newline at end of file diff --git a/src/InternshipSystem.Repository/DatabaseFiller.cs b/src/InternshipSystem.Repository/DatabaseFiller.cs index 1b898d1..c3f135a 100644 --- a/src/InternshipSystem.Repository/DatabaseFiller.cs +++ b/src/InternshipSystem.Repository/DatabaseFiller.cs @@ -139,12 +139,12 @@ namespace InternshipSystem.Repository { Name = "Informatyka", }, - AvailableInternshipTypes = new List + AvailableInternshipTypes = new List { - InternshipType.UOP, - InternshipType.UZ, - InternshipType.UD, - InternshipType.FreeInternship + new EditionInternshipType() { InternshipType = InternshipType.UOP }, + new EditionInternshipType() { InternshipType = InternshipType.UZ }, + new EditionInternshipType() { InternshipType = InternshipType.UD }, + new EditionInternshipType() { InternshipType = InternshipType.FreeInternship }, }, Internships = new List(), }