File size: 3,286 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
using System.Data;
using Dapper;
using SCM.Application.Common;
using SCM.Domain.Qualidade;

namespace SCM.Infrastructure.Persistence.Repositories;

public sealed class SubAtividadePadraoRepository : ISubAtividadePadraoRepository
{
    private readonly IUnitOfWork _uow;

    public SubAtividadePadraoRepository(IUnitOfWork uow) => _uow = uow;

    private IDbConnection Conn => _uow.Connection;
    private IDbTransaction? Tx => _uow.CurrentTransaction;

    private const string Columns = "id, obra_id, codigo, descricao";

    public async Task<IReadOnlyList<SubAtividadePadrao>> GetByObraAsync(Guid obraId, CancellationToken ct = default)
    {
        const string sql = $"SELECT {Columns} FROM subativs_padrao WHERE obra_id = @ObraId ORDER BY codigo";
        var rows = await Conn.QueryAsync<SubAtividadePadrao>(
            new CommandDefinition(sql, new { ObraId = obraId }, Tx, cancellationToken: ct));
        return rows.ToList();
    }

    public async Task<SubAtividadePadrao?> GetByIdAsync(Guid id, CancellationToken ct = default)
    {
        const string sql = $"SELECT {Columns} FROM subativs_padrao WHERE id = @Id";
        return await Conn.QueryFirstOrDefaultAsync<SubAtividadePadrao>(
            new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct));
    }

    public async Task<bool> CodigoExistsAsync(string codigo, Guid obraId, Guid? excludeId = null, CancellationToken ct = default)
    {
        const string sql = """
            SELECT COUNT(*) FROM subativs_padrao
            WHERE lower(codigo) = lower(@Codigo) AND obra_id = @ObraId AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId)
            """;
        var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition(
            sql, new { Codigo = codigo, ObraId = obraId, ExcludeId = excludeId }, Tx, cancellationToken: ct));
        return count > 0;
    }

    public async Task<bool> ObraExistsAsync(Guid obraId, CancellationToken ct = default)
    {
        const string sql = "SELECT COUNT(*) FROM obras WHERE id = @Id";
        var count = await Conn.ExecuteScalarAsync<long>(
            new CommandDefinition(sql, new { Id = obraId }, Tx, cancellationToken: ct));
        return count > 0;
    }

    public async Task AddAsync(SubAtividadePadrao s, CancellationToken ct = default)
    {
        const string sql = "INSERT INTO subativs_padrao (id, obra_id, codigo, descricao) VALUES (@Id, @ObraId, @Codigo, @Descricao)";
        await Conn.ExecuteAsync(new CommandDefinition(
            sql, new { s.Id, s.ObraId, s.Codigo, s.Descricao }, Tx, cancellationToken: ct));
    }

    public async Task UpdateAsync(SubAtividadePadrao s, CancellationToken ct = default)
    {
        const string sql = "UPDATE subativs_padrao SET codigo = @Codigo, descricao = @Descricao WHERE id = @Id";
        await Conn.ExecuteAsync(new CommandDefinition(
            sql, new { s.Id, s.Codigo, s.Descricao }, Tx, cancellationToken: ct));
    }

    public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default)
    {
        const string sql = "DELETE FROM subativs_padrao WHERE id = @Id";
        var affected = await Conn.ExecuteAsync(
            new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct));
        return affected > 0;
    }
}