From a06991a318b20ff0a8d6cd4d804a0838d766ffb8 Mon Sep 17 00:00:00 2001 From: mborzyszkowski Date: Sat, 12 Sep 2020 16:03:05 +0200 Subject: [PATCH] Rewrite InternshipType --- .../Controllers/AdminController.cs | 8 ++ .../Controllers/InternshipTypesController.cs | 61 ++++++++++ .../Queries/RegistrationFormQuery.cs | 2 +- .../Services/InternshipService.cs | 2 +- src/InternshipSystem.Core/Entity/Edition.cs | 4 +- .../Entity/Internship/InternshipType.cs | 15 +-- .../Entity/StaticPage.cs | 2 +- .../UglyOrmArtifacts/EditionInternshipType.cs | 11 -- .../DatabaseFiller.cs | 113 ++++++++++-------- .../InternshipDbContext.cs | 1 + 10 files changed, 141 insertions(+), 78 deletions(-) create mode 100644 src/InternshipSystem.Api/Controllers/InternshipTypesController.cs delete mode 100644 src/InternshipSystem.Core/UglyOrmArtifacts/EditionInternshipType.cs diff --git a/src/InternshipSystem.Api/Controllers/AdminController.cs b/src/InternshipSystem.Api/Controllers/AdminController.cs index ce73595..0c943a5 100644 --- a/src/InternshipSystem.Api/Controllers/AdminController.cs +++ b/src/InternshipSystem.Api/Controllers/AdminController.cs @@ -20,6 +20,7 @@ namespace InternshipSystem.Api.Controllers public async Task Fill() { await FillerService.FillCompanies(); + await FillerService.FillInternshipTypes(); await FillerService.FillEditions(); await FillerService.FillStaticPages(); return Ok(); @@ -31,6 +32,13 @@ namespace InternshipSystem.Api.Controllers await FillerService.FillCompanies(); return Ok(); } + + [HttpPost("fill/internshipTypes")] + public async Task FillInternshipTypesAsync() + { + await FillerService.FillInternshipTypes(); + return Ok(); + } [HttpPost("fill/editions")] public async Task FillEditionsAsync() diff --git a/src/InternshipSystem.Api/Controllers/InternshipTypesController.cs b/src/InternshipSystem.Api/Controllers/InternshipTypesController.cs new file mode 100644 index 0000000..d4afa36 --- /dev/null +++ b/src/InternshipSystem.Api/Controllers/InternshipTypesController.cs @@ -0,0 +1,61 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using InternshipSystem.Api.Security; +using InternshipSystem.Core.Entity.Internship; +using InternshipSystem.Repository; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace InternshipSystem.Api.Controllers +{ + [ApiController] + [Route("internshipType")] + public class InternshipTypesController : ControllerBase + { + + public InternshipTypesController(InternshipDbContext context) + { + Context = context; + } + private InternshipDbContext Context { get; } + + /// + /// Get all internship types + /// + /// List of internship types + [HttpGet] + [ProducesResponseType(StatusCodes.Status200OK)] + public async Task>> GetAllInternshipTypes(CancellationToken cancellationToken) => + await Context.InternshipTypes + .ToListAsync(cancellationToken); + + /// + /// Get static page + /// + /// List of internship types for edition + [HttpGet("forCurrentEdition")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [Authorize] + public async Task>> GetInternshipTypesForEdition([FromServices] User user, CancellationToken cancellationToken) + { + var edition = + await Context.Editions + .Include(e => e.AvailableInternshipTypes) + .Where(e => e.Id.Equals(user.EditionId)) + .FirstOrDefaultAsync(cancellationToken: cancellationToken); + + if (edition == null) + { + return NotFound(); + } + + return Ok(edition.AvailableInternshipTypes); + } + } +} \ No newline at end of file diff --git a/src/InternshipSystem.Api/Queries/RegistrationFormQuery.cs b/src/InternshipSystem.Api/Queries/RegistrationFormQuery.cs index 77122c9..c1aad76 100644 --- a/src/InternshipSystem.Api/Queries/RegistrationFormQuery.cs +++ b/src/InternshipSystem.Api/Queries/RegistrationFormQuery.cs @@ -10,7 +10,7 @@ namespace InternshipSystem.Api.Queries public BranchOfficeForm BranchOffice { get; set; } public DateTime? Start { get; set; } public DateTime? End { get; set; } - public InternshipType? Type { get; set; } + public InternshipType Type { get; set; } public class Validator : AbstractValidator { diff --git a/src/InternshipSystem.Api/Services/InternshipService.cs b/src/InternshipSystem.Api/Services/InternshipService.cs index 4bcaa03..51eafc5 100644 --- a/src/InternshipSystem.Api/Services/InternshipService.cs +++ b/src/InternshipSystem.Api/Services/InternshipService.cs @@ -68,7 +68,7 @@ namespace InternshipSystem.Api.Services internshipRegistration.Start = registrationQuery.Start ?? internshipRegistration.Start; internshipRegistration.End = registrationQuery.End ?? internshipRegistration.End; - if (registrationQuery.Type.HasValue && edition.IsInternshipTypeAllowed(registrationQuery.Type.Value)) + if (registrationQuery.Type != null && edition.IsInternshipTypeAllowed(registrationQuery.Type)) { return new BadRequestObjectResult("Edition doesn't have this type of employment in available employments type"); } diff --git a/src/InternshipSystem.Core/Entity/Edition.cs b/src/InternshipSystem.Core/Entity/Edition.cs index d6ee476..16fd29c 100644 --- a/src/InternshipSystem.Core/Entity/Edition.cs +++ b/src/InternshipSystem.Core/Entity/Edition.cs @@ -15,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; @@ -31,7 +31,7 @@ namespace InternshipSystem.Core public bool IsInternshipTypeAllowed(InternshipType registrationQueryType) { - return AvailableInternshipTypes.Select(it => it.InternshipType).Contains(registrationQueryType); + return AvailableInternshipTypes.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 e810e05..5065f12 100644 --- a/src/InternshipSystem.Core/Entity/Internship/InternshipType.cs +++ b/src/InternshipSystem.Core/Entity/Internship/InternshipType.cs @@ -1,14 +1,9 @@ namespace InternshipSystem.Core.Entity.Internship { - public enum InternshipType - { - FreeInternship = 0, - GraduateInternship = 1, - FreeApprenticeship = 2, - PaidApprenticeship = 3, - ForeignInternship = 4, - UOP = 5, - UD = 6, - UZ = 7, + public class InternshipType + { + public long Id { get; set; } + public string Type { get; set; } + public string Description { get; set; } } } \ No newline at end of file diff --git a/src/InternshipSystem.Core/Entity/StaticPage.cs b/src/InternshipSystem.Core/Entity/StaticPage.cs index 9cb91e9..b4329bd 100644 --- a/src/InternshipSystem.Core/Entity/StaticPage.cs +++ b/src/InternshipSystem.Core/Entity/StaticPage.cs @@ -2,7 +2,7 @@ { public class StaticPage { - public long? Id { get; set; } + public long Id { get; set; } public string AccessName { get; set; } public string Title { get; set; } public string Content { get; set; } diff --git a/src/InternshipSystem.Core/UglyOrmArtifacts/EditionInternshipType.cs b/src/InternshipSystem.Core/UglyOrmArtifacts/EditionInternshipType.cs deleted file mode 100644 index f8c03c2..0000000 --- a/src/InternshipSystem.Core/UglyOrmArtifacts/EditionInternshipType.cs +++ /dev/null @@ -1,11 +0,0 @@ -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 2d35976..bbb1693 100644 --- a/src/InternshipSystem.Repository/DatabaseFiller.cs +++ b/src/InternshipSystem.Repository/DatabaseFiller.cs @@ -102,12 +102,62 @@ namespace InternshipSystem.Repository await Context.SaveChangesAsync(); } + public async Task FillInternshipTypes() + { + var internshipTypes = new List + { + new InternshipType + { + Type = "FreeInternship", + Description = "Praktyka bezpłatna", + }, + new InternshipType + { + Type = "GraduateInternship", + Description = "Praktyka absolwencka", + }, + new InternshipType + { + Type = "FreeApprenticeship", + Description = "Praktyka bezpłatna", + }, + new InternshipType + { + Type = "PaidApprenticeship", + Description = "np. przemysłowy", + }, + new InternshipType + { + Type = "ForeignInternship", + Description = "np. IAESTE, ERASMUS", + }, + new InternshipType + { + Type = "UOP", + Description = "umowa o pracę", + }, + new InternshipType + { + Type = "UD", + Description = "umowa o dzieło", + }, + new InternshipType + { + Type = "UZ", + Description = "umowa zlecenie", + }, + }; + await Context.InternshipTypes.AddRangeAsync(internshipTypes); + await Context.SaveChangesAsync(); + } + public async Task FillEditions() { var editions = new List { new Edition { + Id = Guid.Parse("138da8a3-855c-4b17-9bd2-5f357679efa9"), EditionStart = new DateTime(2000, 5, 10), EditionFinish = new DateTime(2000, 11, 10), ReportingStart = new DateTime(2000, 9, 30), @@ -139,12 +189,12 @@ namespace InternshipSystem.Repository { Name = "Informatyka", }, - AvailableInternshipTypes = new List + AvailableInternshipTypes = new List { - new EditionInternshipType() { InternshipType = InternshipType.UOP }, - new EditionInternshipType() { InternshipType = InternshipType.UZ }, - new EditionInternshipType() { InternshipType = InternshipType.UD }, - new EditionInternshipType() { InternshipType = InternshipType.FreeInternship }, + Context.InternshipTypes.First(t => t.Type.Equals("UOP")), + Context.InternshipTypes.First(t => t.Type.Equals("UZ")), + Context.InternshipTypes.First(t => t.Type.Equals("UD")), + Context.InternshipTypes.First(t => t.Type.Equals("FreeInternship")), }, Internships = new List(), } @@ -166,15 +216,15 @@ namespace InternshipSystem.Repository }, InternshipRegistration = new InternshipRegistration { - Company = Context.Companies.First(c => c.Id.Equals(1)), //Intel - Type = InternshipType.UOP, + Company = Context.Companies.First(c => c.Name.Equals("Intel")), + Type = Context.InternshipTypes.First(t => t.Type.Equals("UOP")), Start = new DateTime(2000, 7, 1), End = new DateTime(2000, 8, 30), State = DocumentState.Submitted, BranchAddress = Context.Companies .Include(c => c.Branches) - .First(c => c.Id.Equals(1)) + .First(c => c.Name.Equals("Intel")) .Branches .First(), }, @@ -216,15 +266,15 @@ namespace InternshipSystem.Repository }, InternshipRegistration = new InternshipRegistration { - Company = Context.Companies.First(c => c.Id.Equals(2)), //Asseco - Type = InternshipType.UZ, + Company = Context.Companies.First(c => c.Name.Equals("Asseco Poland")), + Type = Context.InternshipTypes.First(t => t.Type.Equals("UZ")), Start = new DateTime(2000, 7, 1), End = new DateTime(2000, 8, 30), State = DocumentState.Submitted, BranchAddress = Context.Companies .Include(c => c.Branches) - .First(c => c.Id.Equals(2)) + .First(c => c.Name.Equals("Asseco Poland")) .Branches .First(), }, @@ -278,46 +328,5 @@ namespace InternshipSystem.Repository await Context.StaticPages.AddRangeAsync(staticPages); await Context.SaveChangesAsync(); } - - // new InternshipType - // { - // Type = "FreeInternship", - // Description = "Praktyka bezpłatna", - // }, - // new InternshipType - // { - // Type = "GraduateInternship" - // }, - // new InternshipType - // { - // Type = "FreeApprenticeship" - // }, - // new InternshipType - // { - // Type = "PaidApprenticeship", - // Description = "np. przemysłowy" - // }, - // new InternshipType - // { - // Type = "ForeignInternship", - // Description = "np. IAESTE, ERASMUS" - // }, - // new InternshipType - // { - // Type = "UOP" - // }, - // new InternshipType - // { - // Type = "UD" - // }, - // new InternshipType - // { - // Type = "UZ" - // }, - // new InternshipType - // { - // Type = "Other", - // Description = "Należy wprowadzić samodzielnie" - // } } } \ No newline at end of file diff --git a/src/InternshipSystem.Repository/InternshipDbContext.cs b/src/InternshipSystem.Repository/InternshipDbContext.cs index f95f1f5..9ff73e3 100644 --- a/src/InternshipSystem.Repository/InternshipDbContext.cs +++ b/src/InternshipSystem.Repository/InternshipDbContext.cs @@ -10,6 +10,7 @@ namespace InternshipSystem.Repository public DbSet Companies { get; set; } public DbSet Editions { get; set; } public DbSet StaticPages { get; set; } + public DbSet InternshipTypes { get; set; } public DbSet Students { get; set; } public InternshipDbContext(DbContextOptions options)