add IntetnshipService
This commit is contained in:
parent
d81ebc98fb
commit
c401081dc8
@ -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>
|
||||
@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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>
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
16
src/InternshipSystem.Api/Services/IInternshipService.cs
Normal file
16
src/InternshipSystem.Api/Services/IInternshipService.cs
Normal 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);
|
||||
}
|
||||
}
|
114
src/InternshipSystem.Api/Services/InternshipService.cs
Normal file
114
src/InternshipSystem.Api/Services/InternshipService.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user