50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using MediatR;
|
|
using MinAttest.Application.Abstractions;
|
|
using MinAttest.Contracts.Attests;
|
|
using MinAttest.Domain.Entities;
|
|
|
|
namespace MinAttest.Application.Features.Attests.Commands;
|
|
|
|
public record PersonUploadAttestCommand(Guid PersonId, PersonAttestUploadRequest Request) : IRequest<Guid?>;
|
|
|
|
public class PersonUploadAttestCommandHandler(IAppDbContext db) : IRequestHandler<PersonUploadAttestCommand, Guid?>
|
|
{
|
|
public async Task<Guid?> Handle(PersonUploadAttestCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var r = request.Request;
|
|
|
|
var person = await db.Persons.FindAsync([request.PersonId], cancellationToken);
|
|
if (person is null) return null;
|
|
|
|
byte[]? content = null;
|
|
long? length = null;
|
|
if (!string.IsNullOrWhiteSpace(r.ContentBase64))
|
|
{
|
|
try { content = Convert.FromBase64String(r.ContentBase64); length = content.LongLength; }
|
|
catch (FormatException) { return null; }
|
|
}
|
|
|
|
var a = new Attest
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
PersonId = request.PersonId,
|
|
EmployerId = null,
|
|
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;
|
|
}
|
|
}
|
|
|