add IntetnshipService #36

Merged
Zonar merged 2 commits from InternshipRegistration into master 2020-09-05 20:52:08 +02:00
11 changed files with 173 additions and 123 deletions

View File

@ -38,7 +38,7 @@ namespace InternshipSystem.Api.Controllers
.ToListAsync(cancellationToken);
/// <summary>
/// Get companies matching provided paginated query
/// Get company branches matching provided paginated query
/// </summary>
/// <param name="searchQuery">Paginated query description</param>
/// <param name="companyId"></param>

View File

@ -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;
}
/// <summary>
@ -55,33 +47,7 @@ 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<Document>(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);
}
}
}

View File

@ -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;
}
/// <summary>
@ -51,59 +44,7 @@ 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);
}
}
}

View File

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

View File

@ -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<ActionResult> SubmitRegistration(RegistrationFormQuery registrationQuery, long personNumber,
CancellationToken cancellationToken);
Task<ActionResult> AddDocumentToInternship(DocumentPublishRequest documentRequest, long personNumber,
CancellationToken cancellationToken);
}
}

View File

@ -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<ActionResult> 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<ActionResult> 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<Document>(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();
}
}
}

View File

@ -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<DatabaseFiller>()
.AddScoped<IInternshipService, InternshipService>()
.AddScoped<JwtTokenService>()
.AddAutoMapper(cfg => cfg.AddProfile<ApiProfile>())
.AddStudentAuthentication()

View File

@ -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<Internship> Internships { get; set; }
public List<EditionSubject> AvailableSubjects { get; set; }
public List<InternshipType> AvailableInternshipTypes { get; set; }
public List<EditionInternshipType> 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)

View File

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

View File

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

View File

@ -139,12 +139,12 @@ namespace InternshipSystem.Repository
{
Name = "Informatyka",
},
AvailableInternshipTypes = new List<InternshipType>
AvailableInternshipTypes = new List<EditionInternshipType>
{
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<Internship>(),
}