77 lines
2.6 KiB
C#
77 lines
2.6 KiB
C#
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AutoMapper;
|
|
using EnvelopeGenerator.Application.Common.Dto;
|
|
using EnvelopeGenerator.Application.Common.Extensions;
|
|
|
|
namespace EnvelopeGenerator.Application.DocStatus.Commands;
|
|
|
|
/// <summary>
|
|
/// Represents a command to save the status of a document, either by creating a new status or updating an existing one based on the provided envelope and receiver identifiers.
|
|
/// It returns the identifier of the saved document status.
|
|
/// </summary>
|
|
public record SaveDocStatusCommand : ModifyDocStatusCommandBase, IRequest<DocumentStatusDto?>;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class SaveDocStatusCommandHandler : IRequestHandler<SaveDocStatusCommand, DocumentStatusDto?>
|
|
{
|
|
private readonly IMapper _mapper;
|
|
|
|
private readonly IRepository<DocumentStatus> _repo;
|
|
|
|
private readonly IRepository<Envelope> _envRepo;
|
|
|
|
private readonly IRepository<Receiver> _rcvRepo;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="mapper"></param>
|
|
/// <param name="repo"></param>
|
|
/// <param name="rcvRepo"></param>
|
|
/// <param name="envRepo"></param>
|
|
public SaveDocStatusCommandHandler(IMapper mapper, IRepository<DocumentStatus> repo, IRepository<Receiver> rcvRepo, IRepository<Envelope> envRepo)
|
|
{
|
|
_mapper = mapper;
|
|
_repo = repo;
|
|
_rcvRepo = rcvRepo;
|
|
_envRepo = envRepo;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<DocumentStatusDto?> Handle(SaveDocStatusCommand request, CancellationToken cancel)
|
|
{
|
|
// ceck if exists
|
|
bool isExists = await _repo.ReadOnly().Where(request).AnyAsync(cancel);
|
|
|
|
var env = await _envRepo.ReadOnly().Where(request.Envelope).FirstAsync(cancel);
|
|
var rcv = await _rcvRepo.ReadOnly().Where(request.Receiver).FirstAsync(cancel);
|
|
|
|
request.Envelope.Id = env.Id;
|
|
request.Receiver.Id = rcv.Id;
|
|
|
|
if (isExists)
|
|
{
|
|
var uReq = request.To<UpdateDocStatusCommand>();
|
|
await _repo.UpdateAsync(uReq, q => q.Where(request), cancel);
|
|
}
|
|
else
|
|
{
|
|
var cReq = request.To<CreateDocStatusCommand>();
|
|
await _repo.CreateAsync(cReq, cancel);
|
|
}
|
|
|
|
var docStatus = await _repo.ReadOnly().Where(request).SingleOrDefaultAsync(cancel);
|
|
|
|
return _mapper.Map<DocumentStatusDto>(docStatus);
|
|
}
|
|
} |