add IntetnshipService #36
@ -1,17 +1,11 @@
|
|||||||
using System;
|
using System.Threading;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AutoMapper;
|
|
||||||
using InternshipSystem.Api.Queries;
|
using InternshipSystem.Api.Queries;
|
||||||
using InternshipSystem.Api.Security;
|
using InternshipSystem.Api.Security;
|
||||||
using InternshipSystem.Core;
|
using InternshipSystem.Api.Services;
|
||||||
using InternshipSystem.Repository;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace InternshipSystem.Api.Controllers
|
namespace InternshipSystem.Api.Controllers
|
||||||
{
|
{
|
||||||
@ -19,13 +13,11 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
[Route("document")]
|
[Route("document")]
|
||||||
public class DocumentsController : ControllerBase
|
public class DocumentsController : ControllerBase
|
||||||
{
|
{
|
||||||
private InternshipDbContext Context { get; }
|
private readonly IInternshipService _internshipService;
|
||||||
private IMapper Mapper { get; }
|
|
||||||
|
|
||||||
public DocumentsController(InternshipDbContext context, IMapper mapper)
|
public DocumentsController(IInternshipService internshipService)
|
||||||
{
|
{
|
||||||
Context = context;
|
_internshipService = internshipService;
|
||||||
Mapper = mapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -55,33 +47,7 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
|
|
||||||
var personNumber = long.Parse(User.FindFirst(InternshipClaims.PersonNumber).Value);
|
var personNumber = long.Parse(User.FindFirst(InternshipClaims.PersonNumber).Value);
|
||||||
|
|
||||||
var edition = await Context.Editions.FindAsync(personNumber);
|
return await _internshipService.AddDocumentToInternship(documentRequest, personNumber, cancellationToken);
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,29 +1,22 @@
|
|||||||
using System;
|
using System.Threading;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AutoMapper;
|
|
||||||
using InternshipSystem.Api.Queries;
|
using InternshipSystem.Api.Queries;
|
||||||
using InternshipSystem.Api.Security;
|
using InternshipSystem.Api.Security;
|
||||||
using InternshipSystem.Core;
|
using InternshipSystem.Api.Services;
|
||||||
using InternshipSystem.Repository;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace InternshipSystem.Api.Controllers
|
namespace InternshipSystem.Api.Controllers
|
||||||
{
|
{
|
||||||
[Route("internshipRegistration")]
|
[Route("internshipRegistration")]
|
||||||
public class InternshipRegistrationController : ControllerBase
|
public class InternshipRegistrationController : ControllerBase
|
||||||
{
|
{
|
||||||
private InternshipDbContext Context { get; }
|
private readonly IInternshipService _internshipService;
|
||||||
private IMapper Mapper { get; }
|
|
||||||
|
|
||||||
public InternshipRegistrationController(InternshipDbContext context, IMapper mapper)
|
public InternshipRegistrationController(IInternshipService internshipService)
|
||||||
{
|
{
|
||||||
Context = context;
|
_internshipService = internshipService;
|
||||||
Mapper = mapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -51,59 +44,7 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
|
|
||||||
var personNumber = long.Parse(User.FindFirst(InternshipClaims.PersonNumber).Value);
|
var personNumber = long.Parse(User.FindFirst(InternshipClaims.PersonNumber).Value);
|
||||||
|
|
||||||
var edition = await Context.Editions.FindAsync(personNumber);
|
return await _internshipService.SubmitRegistration(registrationQuery, personNumber, cancellationToken);
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using InternshipSystem.Api.Result;
|
|
||||||
using InternshipSystem.Api.Security;
|
using InternshipSystem.Api.Security;
|
||||||
using InternshipSystem.Core;
|
|
||||||
using InternshipSystem.Repository;
|
using InternshipSystem.Repository;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
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.ModelBinders;
|
||||||
using InternshipSystem.Api.Options;
|
using InternshipSystem.Api.Options;
|
||||||
using InternshipSystem.Api.Security;
|
using InternshipSystem.Api.Security;
|
||||||
|
using InternshipSystem.Api.Services;
|
||||||
|
using InternshipSystem.Core;
|
||||||
using InternshipSystem.Repository;
|
using InternshipSystem.Repository;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
@ -36,6 +38,7 @@ namespace InternshipSystem.Api
|
|||||||
options.IncludeXmlComments(xmlPath);
|
options.IncludeXmlComments(xmlPath);
|
||||||
})
|
})
|
||||||
.AddScoped<DatabaseFiller>()
|
.AddScoped<DatabaseFiller>()
|
||||||
|
.AddScoped<IInternshipService, InternshipService>()
|
||||||
.AddScoped<JwtTokenService>()
|
.AddScoped<JwtTokenService>()
|
||||||
.AddAutoMapper(cfg => cfg.AddProfile<ApiProfile>())
|
.AddAutoMapper(cfg => cfg.AddProfile<ApiProfile>())
|
||||||
.AddStudentAuthentication()
|
.AddStudentAuthentication()
|
||||||
|
Loading…
Reference in New Issue
Block a user