63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using MinAttest.Contracts.Attests;
|
|
using MinAttest.Application.Abstractions;
|
|
using MinAttest.Domain.Entities;
|
|
|
|
namespace MinAttest.Application.Features.Attests.Commands;
|
|
|
|
public record CompleteUploadCommand(Guid UploadId, CompleteUploadRequest Request) : IRequest<Guid?>;
|
|
|
|
public class CompleteUploadCommandHandler(IAppDbContext db) : IRequestHandler<CompleteUploadCommand, Guid?>
|
|
{
|
|
public async Task<Guid?> Handle(CompleteUploadCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var r = request.Request;
|
|
var personExists = await db.Persons.AnyAsync(p => p.Id == r.PersonId, cancellationToken);
|
|
if (!personExists) return null;
|
|
|
|
if (r.EmployerId is Guid eid)
|
|
{
|
|
var exists = await db.Employers.AnyAsync(e => e.Id == eid, cancellationToken);
|
|
if (!exists) return null;
|
|
}
|
|
|
|
byte[]? content = null;
|
|
long? length = null;
|
|
if (!string.IsNullOrWhiteSpace(r.ContentBase64))
|
|
{
|
|
try
|
|
{
|
|
content = Convert.FromBase64String(r.ContentBase64);
|
|
length = content.LongLength;
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
// invalid base64 -> treat as bad request
|
|
return null;
|
|
}
|
|
}
|
|
|
|
var a = new Attest
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
PersonId = r.PersonId,
|
|
EmployerId = r.EmployerId,
|
|
Title = r.Title,
|
|
From = r.From,
|
|
To = r.To,
|
|
Summary = r.Summary,
|
|
BlobPath = r.BlobPath,
|
|
BlobHash = r.BlobHash,
|
|
Content = content,
|
|
ContentType = r.ContentType,
|
|
ContentLength = length,
|
|
IssuedAt = DateTimeOffset.UtcNow
|
|
};
|
|
|
|
db.Attests.Add(a);
|
|
await db.SaveChangesAsync(cancellationToken);
|
|
return a.Id;
|
|
}
|
|
}
|