Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/FolhaTarefaRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 8.62 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/FolhaTarefaRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/FolhaTarefaRepository.cs
-
curl -L -o FolhaTarefaRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/FolhaTarefaRepository.cs
8.62 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Planejamento; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class FolhaTarefaRepository : IFolhaTarefaRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public FolhaTarefaRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| private const string Columns = """ | |
| id, contrato_id, equipe_id, supervisor_id, codigo, descricao, observacao, | |
| data_inicial, periodo, data_final_prox_periodo, numero_horas, | |
| semana_contrato_num, semana_anual, tipo, status, fechada, | |
| revisao, data_fechamento, codigo_equipe_congelado, data_criacao | |
| """; | |
| public async Task<IReadOnlyList<FolhaTarefa>> GetByContratoAsync(Guid contratoId, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Columns} FROM folhas_tarefa WHERE contrato_id = @ContratoId ORDER BY codigo"; | |
| var rows = await Conn.QueryAsync<FolhaTarefa>( | |
| new CommandDefinition(sql, new { ContratoId = contratoId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<FolhaTarefa?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Columns} FROM folhas_tarefa WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<FolhaTarefa>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> CodigoExistsAsync(string codigo, Guid contratoId, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM folhas_tarefa | |
| WHERE lower(codigo) = lower(@Codigo) AND contrato_id = @ContratoId | |
| AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { Codigo = codigo, ContratoId = contratoId, ExcludeId = excludeId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<(bool Existe, DateOnly? DataInicio)> GetContratoDataInicioAsync(Guid contratoId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT data_inicio FROM contratos WHERE id = @Id"; | |
| var rows = (await Conn.QueryAsync<DateOnly?>( | |
| new CommandDefinition(sql, new { Id = contratoId }, Tx, cancellationToken: ct))).ToList(); | |
| return rows.Count == 0 ? (false, null) : (true, rows[0]); | |
| } | |
| public async Task<int?> ContarFuncionariosDaEquipeAsync(Guid equipeId, CancellationToken ct = default) | |
| { | |
| const string sqlEquipe = "SELECT COUNT(*) FROM equipes WHERE id = @Id"; | |
| var existe = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sqlEquipe, new { Id = equipeId }, Tx, cancellationToken: ct)); | |
| if (existe == 0) return null; | |
| const string sql = "SELECT COUNT(*) FROM equipe_membros WHERE equipe_id = @Id"; | |
| return await Conn.ExecuteScalarAsync<int>( | |
| new CommandDefinition(sql, new { Id = equipeId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> SupervisorExistsAsync(Guid funcionarioId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM funcionarios WHERE id = @Id"; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Id = funcionarioId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<bool?> EquipeEstaAtivaAsync(Guid equipeId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT ativa FROM equipes WHERE id = @Id"; | |
| var rows = (await Conn.QueryAsync<bool>( | |
| new CommandDefinition(sql, new { Id = equipeId }, Tx, cancellationToken: ct))).ToList(); | |
| return rows.Count == 0 ? null : rows[0]; | |
| } | |
| public async Task<string?> GetCodigoEquipeAsync(Guid equipeId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT codigo FROM equipes WHERE id = @Id"; | |
| return await Conn.ExecuteScalarAsync<string?>( | |
| new CommandDefinition(sql, new { Id = equipeId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<IReadOnlyList<(string Nome, string Matricula, string? Funcao, int Tipo, bool Ativo)>> GetSnapshotFuncionariosAsync(Guid equipeId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT f.nome, f.matricula, f.funcao, f.tipo, f.ativo | |
| FROM equipe_membros m | |
| JOIN funcionarios f ON f.id = m.funcionario_id | |
| WHERE m.equipe_id = @Id | |
| ORDER BY f.nome | |
| """; | |
| var rows = await Conn.QueryAsync<(string Nome, string Matricula, string? Funcao, int Tipo, bool Ativo)>( | |
| new CommandDefinition(sql, new { Id = equipeId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task CongelarFuncionariosAsync(Guid folhaTarefaId, IReadOnlyList<(string Nome, string Matricula, string? Funcao, int Tipo, bool Ativo)> snapshot, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO folha_tarefa_funcionarios_congelados (id, folha_tarefa_id, nome, matricula, funcao, tipo, ativo) | |
| VALUES (@Id, @FtId, @Nome, @Matricula, @Funcao, @Tipo, @Ativo) | |
| """; | |
| foreach (var s in snapshot) | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new | |
| { | |
| Id = Guid.NewGuid(), FtId = folhaTarefaId, s.Nome, s.Matricula, s.Funcao, s.Tipo, s.Ativo | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task RemoverCongeladosAsync(Guid folhaTarefaId, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM folha_tarefa_funcionarios_congelados WHERE folha_tarefa_id = @Id"; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, new { Id = folhaTarefaId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task AddAsync(FolhaTarefa f, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO folhas_tarefa | |
| (id, contrato_id, equipe_id, supervisor_id, codigo, descricao, observacao, | |
| data_inicial, periodo, data_final_prox_periodo, numero_horas, | |
| semana_contrato_num, semana_anual, tipo, status, fechada, | |
| revisao, data_fechamento, codigo_equipe_congelado, data_criacao) | |
| VALUES | |
| (@Id, @ContratoId, @EquipeId, @SupervisorId, @Codigo, @Descricao, @Observacao, | |
| @DataInicial, @Periodo, @DataFinalProxPeriodo, @NumeroHoras, | |
| @SemanaContratoNum, @SemanaAnual, @Tipo, @Status, @Fechada, | |
| @Revisao, @DataFechamento, @CodigoEquipeCongelado, @DataCriacao) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(f), Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(FolhaTarefa f, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| UPDATE folhas_tarefa | |
| SET codigo = @Codigo, descricao = @Descricao, observacao = @Observacao, | |
| data_inicial = @DataInicial, periodo = @Periodo, | |
| data_final_prox_periodo = @DataFinalProxPeriodo, numero_horas = @NumeroHoras, | |
| semana_contrato_num = @SemanaContratoNum, semana_anual = @SemanaAnual, | |
| tipo = @Tipo, status = @Status, fechada = @Fechada, | |
| revisao = @Revisao, data_fechamento = @DataFechamento, | |
| codigo_equipe_congelado = @CodigoEquipeCongelado | |
| WHERE id = @Id | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(f), Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM folhas_tarefa WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private static object ToParameters(FolhaTarefa f) => new | |
| { | |
| f.Id, f.ContratoId, f.EquipeId, f.SupervisorId, f.Codigo, f.Descricao, f.Observacao, | |
| f.DataInicial, f.Periodo, f.DataFinalProxPeriodo, f.NumeroHoras, | |
| f.SemanaContratoNum, f.SemanaAnual, f.Tipo, Status = (int)f.Status, f.Fechada, | |
| f.Revisao, f.DataFechamento, f.CodigoEquipeCongelado, f.DataCriacao | |
| }; | |
| } | |