scm-api / src /SCM.Infrastructure /Persistence /Repositories /GrupoTrabalhoRepository.cs
rafaeldavila's picture
Deploy SCM API (.NET 8) - Docker Space
de8b914 verified
Raw History Blame Contribute Delete
6.05 kB
using System.Data;
using Dapper;
using SCM.Application.Common;
using SCM.Domain.AtividadesCiclicas;
namespace SCM.Infrastructure.Persistence.Repositories;
public sealed class GrupoTrabalhoRepository : IGrupoTrabalhoRepository
{
private readonly IUnitOfWork _uow;
public GrupoTrabalhoRepository(IUnitOfWork uow) => _uow = uow;
private IDbConnection Conn => _uow.Connection;
private IDbTransaction? Tx => _uow.CurrentTransaction;
public async Task<IReadOnlyList<GrupoTrabalhoComContagem>> GetAllAsync(CancellationToken ct = default)
{
const string sql = """
SELECT g.id, g.codigo, g.nome, g.ativo, COUNT(m.id) AS qtd
FROM grupos_trabalho g
LEFT JOIN grupo_trabalho_membros m ON m.grupo_trabalho_id = g.id
GROUP BY g.id, g.codigo, g.nome, g.ativo
ORDER BY g.nome
""";
var rows = await Conn.QueryAsync<GrupoRow>(new CommandDefinition(sql, transaction: Tx, cancellationToken: ct));
return rows.Select(r => new GrupoTrabalhoComContagem(r.ToDomain(), r.Qtd)).ToList();
}
public async Task<GrupoTrabalho?> GetByIdAsync(Guid id, CancellationToken ct = default)
=> await Conn.QueryFirstOrDefaultAsync<GrupoTrabalho>(new CommandDefinition(
"SELECT id, codigo, nome, ativo FROM grupos_trabalho WHERE id = @Id", new { Id = id }, Tx, cancellationToken: ct));
public async Task<bool> CodigoExistsAsync(string codigo, Guid? excludeId = null, CancellationToken ct = default)
{
const string sql = "SELECT COUNT(*) FROM grupos_trabalho WHERE codigo = @Codigo AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId)";
return await Conn.ExecuteScalarAsync<long>(new CommandDefinition(sql, new { Codigo = codigo, ExcludeId = excludeId }, Tx, cancellationToken: ct)) > 0;
}
public async Task<int> ContarAtividadesAsync(Guid grupoId, CancellationToken ct = default)
=> await Conn.ExecuteScalarAsync<int>(new CommandDefinition(
"SELECT COUNT(*) FROM atividades_ciclicas WHERE grupo_trabalho_id = @Id", new { Id = grupoId }, Tx, cancellationToken: ct));
public async Task AddAsync(GrupoTrabalho grupo, CancellationToken ct = default)
=> await Conn.ExecuteAsync(new CommandDefinition(
"INSERT INTO grupos_trabalho (id, codigo, nome, ativo) VALUES (@Id, @Codigo, @Nome, @Ativo)",
new { grupo.Id, grupo.Codigo, grupo.Nome, grupo.Ativo }, Tx, cancellationToken: ct));
public async Task UpdateAsync(GrupoTrabalho grupo, CancellationToken ct = default)
=> await Conn.ExecuteAsync(new CommandDefinition(
"UPDATE grupos_trabalho SET codigo = @Codigo, nome = @Nome, ativo = @Ativo WHERE id = @Id",
new { grupo.Id, grupo.Codigo, grupo.Nome, grupo.Ativo }, Tx, cancellationToken: ct));
public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default)
=> await Conn.ExecuteAsync(new CommandDefinition(
"DELETE FROM grupos_trabalho WHERE id = @Id", new { Id = id }, Tx, cancellationToken: ct)) > 0;
public async Task<IReadOnlyList<MembroGrupo>> GetMembrosAsync(Guid grupoId, CancellationToken ct = default)
{
const string sql = """
SELECT m.id, m.user_id, u.username, u.email
FROM grupo_trabalho_membros m
JOIN users u ON u.id = m.user_id
WHERE m.grupo_trabalho_id = @GrupoId
ORDER BY u.username
""";
var rows = await Conn.QueryAsync<MembroRow>(new CommandDefinition(sql, new { GrupoId = grupoId }, Tx, cancellationToken: ct));
return rows.Select(r => new MembroGrupo(r.Id, r.UserId, r.Username, r.Email)).ToList();
}
public async Task<bool> UserExistsAsync(Guid userId, CancellationToken ct = default)
=> await Conn.ExecuteScalarAsync<long>(new CommandDefinition(
"SELECT COUNT(*) FROM users WHERE id = @Id", new { Id = userId }, Tx, cancellationToken: ct)) > 0;
public async Task<bool> MembroExistsAsync(Guid grupoId, Guid userId, CancellationToken ct = default)
=> await Conn.ExecuteScalarAsync<long>(new CommandDefinition(
"SELECT COUNT(*) FROM grupo_trabalho_membros WHERE grupo_trabalho_id = @GrupoId AND user_id = @UserId",
new { GrupoId = grupoId, UserId = userId }, Tx, cancellationToken: ct)) > 0;
public async Task AddMembroAsync(GrupoTrabalhoMembro membro, CancellationToken ct = default)
=> await Conn.ExecuteAsync(new CommandDefinition(
"INSERT INTO grupo_trabalho_membros (id, grupo_trabalho_id, user_id) VALUES (@Id, @GrupoTrabalhoId, @UserId)",
new { membro.Id, membro.GrupoTrabalhoId, membro.UserId }, Tx, cancellationToken: ct));
public async Task<bool> RemoveMembroAsync(Guid grupoId, Guid userId, CancellationToken ct = default)
=> await Conn.ExecuteAsync(new CommandDefinition(
"DELETE FROM grupo_trabalho_membros WHERE grupo_trabalho_id = @GrupoId AND user_id = @UserId",
new { GrupoId = grupoId, UserId = userId }, Tx, cancellationToken: ct)) > 0;
public async Task<IReadOnlyList<Guid>> GetGrupoIdsByUserAsync(Guid userId, CancellationToken ct = default)
=> (await Conn.QueryAsync<Guid>(new CommandDefinition(
"SELECT grupo_trabalho_id FROM grupo_trabalho_membros WHERE user_id = @UserId", new { UserId = userId }, Tx, cancellationToken: ct))).ToList();
private sealed class GrupoRow
{
public Guid Id { get; set; }
public string Codigo { get; set; } = string.Empty;
public string Nome { get; set; } = string.Empty;
public bool Ativo { get; set; }
public int Qtd { get; set; }
public GrupoTrabalho ToDomain() => new() { Id = Id, Codigo = Codigo, Nome = Nome, Ativo = Ativo };
}
private sealed class MembroRow
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public string Username { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
}