scm-api / src /SCM.Infrastructure /Persistence /Repositories /FuncionarioRepository.cs
rafaeldavila's picture
Deploy SCM API (.NET 8) - Docker Space
de8b914 verified
Raw History Blame Contribute Delete
4.32 kB
using System.Data;
using Dapper;
using SCM.Application.Common;
using SCM.Domain.Planejamento;
namespace SCM.Infrastructure.Persistence.Repositories;
public sealed class FuncionarioRepository : IFuncionarioRepository
{
private readonly IUnitOfWork _uow;
public FuncionarioRepository(IUnitOfWork uow) => _uow = uow;
private IDbConnection Conn => _uow.Connection;
private IDbTransaction? Tx => _uow.CurrentTransaction;
public async Task<IReadOnlyList<FuncionarioComContagem>> GetAllAsync(CancellationToken ct = default)
{
const string sql = """
SELECT f.id, f.matricula, f.nome, f.funcao, f.tipo, f.ativo, COUNT(em.id) AS qtd
FROM funcionarios f
LEFT JOIN equipe_membros em ON em.funcionario_id = f.id
GROUP BY f.id, f.matricula, f.nome, f.funcao, f.tipo, f.ativo
ORDER BY f.nome
""";
var rows = await Conn.QueryAsync<FuncionarioRow>(
new CommandDefinition(sql, transaction: Tx, cancellationToken: ct));
return rows.Select(r => new FuncionarioComContagem(r.ToDomain(), r.Qtd)).ToList();
}
public async Task<Funcionario?> GetByIdAsync(Guid id, CancellationToken ct = default)
{
const string sql = "SELECT id, matricula, nome, funcao, tipo, ativo FROM funcionarios WHERE id = @Id";
return await Conn.QueryFirstOrDefaultAsync<Funcionario>(
new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct));
}
public async Task<bool> MatriculaExistsAsync(string matricula, Guid? excludeId = null, CancellationToken ct = default)
{
const string sql = """
SELECT COUNT(*) FROM funcionarios
WHERE lower(matricula) = lower(@Matricula) AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId)
""";
var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition(
sql, new { Matricula = matricula, ExcludeId = excludeId }, Tx, cancellationToken: ct));
return count > 0;
}
public async Task<int> ContarEquipesAsync(Guid id, CancellationToken ct = default)
{
const string sql = "SELECT COUNT(*) FROM equipe_membros WHERE funcionario_id = @Id";
return await Conn.ExecuteScalarAsync<int>(
new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct));
}
public async Task AddAsync(Funcionario funcionario, CancellationToken ct = default)
{
const string sql = """
INSERT INTO funcionarios (id, matricula, nome, funcao, tipo, ativo)
VALUES (@Id, @Matricula, @Nome, @Funcao, @Tipo, @Ativo)
""";
await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(funcionario), Tx, cancellationToken: ct));
}
public async Task UpdateAsync(Funcionario funcionario, CancellationToken ct = default)
{
const string sql = """
UPDATE funcionarios
SET nome = @Nome, funcao = @Funcao, tipo = @Tipo, ativo = @Ativo
WHERE id = @Id
""";
await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(funcionario), Tx, cancellationToken: ct));
}
public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default)
{
const string sql = "DELETE FROM funcionarios WHERE id = @Id";
var affected = await Conn.ExecuteAsync(
new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct));
return affected > 0;
}
private static object ToParameters(Funcionario f) => new
{
f.Id,
f.Matricula,
f.Nome,
f.Funcao,
Tipo = (int)f.Tipo,
f.Ativo
};
private sealed class FuncionarioRow
{
public Guid Id { get; set; }
public string Matricula { get; set; } = string.Empty;
public string Nome { get; set; } = string.Empty;
public string? Funcao { get; set; }
public int Tipo { get; set; }
public bool Ativo { get; set; }
public int Qtd { get; set; }
public Funcionario ToDomain() => new()
{
Id = Id,
Matricula = Matricula,
Nome = Nome,
Funcao = Funcao,
Tipo = (FuncionarioTipo)Tipo,
Ativo = Ativo
};
}
}