File size: 2,922 Bytes
de8b914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using SCM.Application.Common;
using SCM.Application.Controle.Dtos;
using SCM.Domain.Controle;
using SCM.Shared.Exceptions;

namespace SCM.Application.Controle;

public sealed class ItemProgramadoService : IItemProgramadoService
{
    private readonly IItemProgramadoRepository _repository;
    private readonly IUnitOfWork _unitOfWork;

    public ItemProgramadoService(IItemProgramadoRepository repository, IUnitOfWork unitOfWork)
    {
        _repository = repository;
        _unitOfWork = unitOfWork;
    }

    public async Task<IReadOnlyList<ItemProgramadoResponse>> GetByItemControleAsync(Guid itemControleId, CancellationToken ct = default)
    {
        var itens = await _repository.GetByItemControleAsync(itemControleId, ct);
        return itens.Select(x => x.ToResponse()).ToList();
    }

    public async Task<ItemProgramadoResponse> GetByIdAsync(Guid id, CancellationToken ct = default)
    {
        var item = await _repository.GetByIdAsync(id, ct)
            ?? throw new NotFoundException($"Item programado {id} não encontrado.");
        return item.ToResponse();
    }

    public async Task<ItemProgramadoResponse> CreateAsync(CreateItemProgramadoRequest request, CancellationToken ct = default)
    {
        if (!await _repository.ItemControleExistsAsync(request.ItemControleId, ct))
            throw new NotFoundException($"Item de controle {request.ItemControleId} não encontrado.");

        var item = ItemProgramado.Create(request.ItemControleId, request.Descricao, request.Quantidade, request.DataProgramada, request.Peso, request.SubAtividadePadraoId, request.TipoAtividade);

        await ExecuteInTransactionAsync(() => _repository.AddAsync(item, ct));
        return item.ToResponse();
    }

    public async Task<ItemProgramadoResponse> UpdateAsync(Guid id, UpdateItemProgramadoRequest request, CancellationToken ct = default)
    {
        var item = await _repository.GetByIdAsync(id, ct)
            ?? throw new NotFoundException($"Item programado {id} não encontrado.");

        item.Atualizar(request.Descricao, request.Quantidade, request.DataProgramada, request.Peso, request.SubAtividadePadraoId, request.TipoAtividade);

        await ExecuteInTransactionAsync(() => _repository.UpdateAsync(item, ct));
        return item.ToResponse();
    }

    public async Task DeleteAsync(Guid id, CancellationToken ct = default)
    {
        var item = await _repository.GetByIdAsync(id, ct)
            ?? throw new NotFoundException($"Item programado {id} não encontrado.");

        await ExecuteInTransactionAsync(() => _repository.DeleteAsync(item.Id, ct));
    }

    private async Task ExecuteInTransactionAsync(Func<Task> action)
    {
        _unitOfWork.BeginTransaction();
        try
        {
            await action();
            _unitOfWork.Commit();
        }
        catch
        {
            _unitOfWork.Rollback();
            throw;
        }
    }
}