Files
jobtrackingapp/JobTrackerApi/Controllers/ApplicationAssetsController.cs
T

172 lines
8.5 KiB
C#

using JobTrackerApi.Models;
using JobTrackerApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace JobTrackerApi.Controllers;
// Phase 5.4 — Application Assets. Connects existing career outputs to one application.
//
// Deliberately thin: CV variant CRUD, preview, PDF export, themes and version history all stay on
// /api/cv (CvVariantController), and documents stay on /api/attachments. These routes only cover what
// is genuinely application-scoped — which variant this application uses, what to tailor, and the
// cover letter with its history. Every route is tenant-scoped and returns 404 for another user's
// application. docs/architecture/overview.md.
[ApiController]
[Route("api/jobapplications/{jobId:int}")]
[Authorize(AuthenticationSchemes = "local")]
public sealed class ApplicationAssetsController : ControllerBase
{
public sealed record AttachVariantRequest(int? VariantId);
public sealed record SaveCoverLetterRequest(string? Text, string? Source, string? AiAction, int? ExpectedVersion = null, CoverLetterDocumentSettings? DocumentSettings = null);
public sealed record PreviewCoverLetterRequest(string? Text, CoverLetterDocumentSettings? DocumentSettings = null);
public sealed record RestoreCoverLetterRequest(int? ExpectedVersion);
private readonly UserManager<ApplicationUser> _users;
private readonly IApplicationAssetsService _assets;
private readonly ISubmittedApplicationPackageService _packages;
private readonly ICvPdfExporter? _pdf;
public ApplicationAssetsController(UserManager<ApplicationUser> users, IApplicationAssetsService assets, ISubmittedApplicationPackageService packages, ICvPdfExporter? pdf = null)
{
_users = users;
_assets = assets;
_packages = packages;
_pdf = pdf;
}
[HttpGet("cv")]
public async Task<ActionResult<ApplicationCvDto>> GetCv(int jobId, CancellationToken ct)
{
var userId = await CurrentUserIdAsync();
if (userId is null) return Unauthorized();
var result = await _assets.GetCvAsync(userId, jobId, ct);
return result is null ? NotFound() : Ok(result);
}
[HttpPut("cv")]
public async Task<ActionResult<ApplicationCvDto>> AttachVariant(int jobId, [FromBody] AttachVariantRequest request, CancellationToken ct)
{
var userId = await CurrentUserIdAsync();
if (userId is null) return Unauthorized();
var result = await _assets.AttachVariantAsync(userId, jobId, request?.VariantId, ct);
return result is null ? NotFound() : Ok(result);
}
[HttpGet("tailoring")]
public async Task<ActionResult<TailoringPlanDto>> GetTailoring(int jobId, CancellationToken ct)
{
var userId = await CurrentUserIdAsync();
if (userId is null) return Unauthorized();
var result = await _assets.GetTailoringPlanAsync(userId, jobId, ct);
return result is null ? NotFound() : Ok(result);
}
[HttpGet("cover-letter")]
public async Task<ActionResult<CoverLetterDto>> GetCoverLetter(int jobId, CancellationToken ct)
{
var userId = await CurrentUserIdAsync();
if (userId is null) return Unauthorized();
var result = await _assets.GetCoverLetterAsync(userId, jobId, ct);
return result is null ? NotFound() : Ok(result);
}
[HttpPut("cover-letter")]
public async Task<ActionResult<CoverLetterDto>> SaveCoverLetter(int jobId, [FromBody] SaveCoverLetterRequest request, CancellationToken ct)
{
var userId = await CurrentUserIdAsync();
if (userId is null) return Unauthorized();
if (request.DocumentSettings is { SchemaVersion: not 1 }) return BadRequest("Unsupported letter settings schema.");
var result = await _assets.SaveCoverLetterAsync(
userId, jobId, request?.Text, request?.Source ?? CoverLetterSources.Manual, request?.AiAction, request?.ExpectedVersion, ct, request?.DocumentSettings);
return result is null ? NotFound() : Ok(result);
}
[HttpPost("cover-letter/preview")]
public async Task<ActionResult<CoverLetterRenderDto>> PreviewCoverLetter(int jobId, [FromBody] PreviewCoverLetterRequest request, CancellationToken ct)
{
var userId = await CurrentUserIdAsync();
if (userId is null) return Unauthorized();
if (request.DocumentSettings is { SchemaVersion: not 1 }) return BadRequest("Unsupported letter settings schema.");
var result = await _assets.PreviewCoverLetterAsync(userId, jobId, request.Text, request.DocumentSettings, ct);
return result is null ? NotFound() : Ok(result);
}
[HttpPost("cover-letter/export-pdf")]
public async Task<IActionResult> ExportCoverLetterPdf(int jobId, CancellationToken ct, [FromQuery] int? expectedVersion = null)
{
var userId = await CurrentUserIdAsync();
if (userId is null) return Unauthorized();
try
{
var render = await _assets.RenderSavedCoverLetterAsync(userId, jobId, expectedVersion, ct);
if (render is null) return NotFound();
var artifact = await (_pdf ?? throw new InvalidOperationException("Letter PDF export is not configured."))
.ExportAsync(userId, new TailoredCvRenderResult(render.ThemeId, render.SuggestedFileName, render.Html), ct);
return File(artifact.Bytes, "application/pdf", artifact.FileName);
}
catch (DbUpdateConcurrencyException)
{
return Conflict(new { code = "revision_conflict", message = "The application changed. Save and review the current letter before exporting." });
}
}
[HttpPost("cover-letter/versions/{version:int}/restore")]
public async Task<ActionResult<CoverLetterDto>> RestoreCoverLetter(int jobId, int version, [FromBody] RestoreCoverLetterRequest? request, CancellationToken ct)
{
var userId = await CurrentUserIdAsync();
if (userId is null) return Unauthorized();
var result = await _assets.RestoreCoverLetterAsync(userId, jobId, version, request?.ExpectedVersion, ct);
return result is null ? NotFound() : Ok(result);
}
[HttpGet("submitted-packages")]
public async Task<ActionResult<IReadOnlyList<SubmittedPackageDto>>> ListSubmittedPackages(int jobId, CancellationToken ct)
{
var userId = await CurrentUserIdAsync();
if (userId is null) return Unauthorized();
var result = await _packages.ListAsync(userId, jobId, ct);
return result is null ? NotFound() : Ok(result);
}
[HttpPost("submitted-packages")]
public async Task<ActionResult<SubmittedPackageDto>> CaptureSubmittedPackage(int jobId, CancellationToken ct)
{
var user = await _users.GetUserAsync(User);
if (user is null) return Unauthorized();
var result = await _packages.CaptureAsync(user.Id, jobId, Person(user), ct);
return result is null ? NotFound() : Ok(result);
}
[HttpGet("submitted-packages/{packageId:int}")]
public async Task<ActionResult<SubmittedPackageDetailDto>> GetSubmittedPackage(int jobId, int packageId, CancellationToken ct)
{
var userId = await CurrentUserIdAsync();
if (userId is null) return Unauthorized();
var result = await _packages.GetAsync(userId, jobId, packageId, ct);
return result is null ? NotFound() : Ok(result);
}
[HttpGet("submitted-packages/{packageId:int}/attachments/{attachmentId:int}")]
public async Task<IActionResult> DownloadSubmittedAttachment(int jobId, int packageId, int attachmentId, CancellationToken ct)
{
var userId = await CurrentUserIdAsync();
if (userId is null) return Unauthorized();
var file = await _packages.GetAttachmentAsync(userId, jobId, packageId, attachmentId, ct);
return file is null ? NotFound() : PhysicalFile(file.Path, file.ContentType, file.FileName);
}
private async Task<string?> CurrentUserIdAsync() => (await _users.GetUserAsync(User))?.Id;
private static CvRenderPerson Person(ApplicationUser user)
{
var name = string.Join(" ", new[] { user.FirstName?.Trim(), user.LastName?.Trim() }.Where(x => !string.IsNullOrWhiteSpace(x)));
if (string.IsNullOrWhiteSpace(name)) name = user.DisplayName?.Trim();
if (string.IsNullOrWhiteSpace(name)) name = user.UserName?.Trim();
if (string.IsNullOrWhiteSpace(name)) name = user.Email?.Trim();
return new CvRenderPerson(string.IsNullOrWhiteSpace(name) ? "Your Name" : name, AvatarStorage.Resolve(user.AvatarImageDataUrl));
}
}