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