Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/FolhaTarefaItemRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 3.37 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/FolhaTarefaItemRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/FolhaTarefaItemRepository.cs
-
curl -L -o FolhaTarefaItemRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/FolhaTarefaItemRepository.cs
3.37 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Planejamento; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class FolhaTarefaItemRepository : IFolhaTarefaItemRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public FolhaTarefaItemRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| private const string Columns = """ | |
| id, folha_tarefa_id, item_programado_id, qt_programada, qt_programada_prox, | |
| prioridade, observacao, data_prev_ini, data_prev_fim, data_prev_ini_prox, data_prev_fim_prox | |
| """; | |
| public async Task<IReadOnlyList<FolhaTarefaItem>> GetByFolhaTarefaAsync(Guid folhaTarefaId, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Columns} FROM folha_tarefa_itens WHERE folha_tarefa_id = @FtId ORDER BY prioridade, id"; | |
| var rows = await Conn.QueryAsync<FolhaTarefaItem>( | |
| new CommandDefinition(sql, new { FtId = folhaTarefaId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<decimal?> GetQuantidadePrevistaAsync(Guid itemProgramadoId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT quantidade FROM itens_programados WHERE id = @Id"; | |
| var rows = (await Conn.QueryAsync<decimal>( | |
| new CommandDefinition(sql, new { Id = itemProgramadoId }, Tx, cancellationToken: ct))).ToList(); | |
| return rows.Count == 0 ? null : rows[0]; | |
| } | |
| public async Task<decimal> GetTotalProgramadoOutrasFtsAsync(Guid itemProgramadoId, Guid folhaTarefaId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COALESCE(SUM(qt_programada + qt_programada_prox), 0) | |
| FROM folha_tarefa_itens | |
| WHERE item_programado_id = @ItemId AND folha_tarefa_id <> @FtId | |
| """; | |
| return await Conn.ExecuteScalarAsync<decimal>(new CommandDefinition( | |
| sql, new { ItemId = itemProgramadoId, FtId = folhaTarefaId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task ReplaceAsync(Guid folhaTarefaId, IReadOnlyList<FolhaTarefaItem> itens, CancellationToken ct = default) | |
| { | |
| const string sqlDel = "DELETE FROM folha_tarefa_itens WHERE folha_tarefa_id = @FtId"; | |
| await Conn.ExecuteAsync(new CommandDefinition(sqlDel, new { FtId = folhaTarefaId }, Tx, cancellationToken: ct)); | |
| const string sqlIns = """ | |
| INSERT INTO folha_tarefa_itens | |
| (id, folha_tarefa_id, item_programado_id, qt_programada, qt_programada_prox, | |
| prioridade, observacao, data_prev_ini, data_prev_fim, data_prev_ini_prox, data_prev_fim_prox) | |
| VALUES | |
| (@Id, @FolhaTarefaId, @ItemProgramadoId, @QtProgramada, @QtProgramadaProx, | |
| @Prioridade, @Observacao, @DataPrevIni, @DataPrevFim, @DataPrevIniProx, @DataPrevFimProx) | |
| """; | |
| foreach (var i in itens) | |
| await Conn.ExecuteAsync(new CommandDefinition(sqlIns, new | |
| { | |
| i.Id, i.FolhaTarefaId, i.ItemProgramadoId, i.QtProgramada, i.QtProgramadaProx, | |
| i.Prioridade, i.Observacao, i.DataPrevIni, i.DataPrevFim, i.DataPrevIniProx, i.DataPrevFimProx | |
| }, Tx, cancellationToken: ct)); | |
| } | |
| } | |