Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/ItemProgramadoRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 3.53 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ItemProgramadoRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/ItemProgramadoRepository.cs
-
curl -L -o ItemProgramadoRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ItemProgramadoRepository.cs
3.53 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Controle; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class ItemProgramadoRepository : IItemProgramadoRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public ItemProgramadoRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| private const string Columns = "id, item_controle_id, descricao, quantidade, data_programada, peso, subatividade_padrao_id, tipo_atividade"; | |
| public async Task<IReadOnlyList<ItemProgramado>> GetByItemControleAsync(Guid itemControleId, CancellationToken ct = default) | |
| { | |
| const string sql = $""" | |
| SELECT {Columns} | |
| FROM itens_programados | |
| WHERE item_controle_id = @ItemControleId | |
| ORDER BY descricao | |
| """; | |
| var rows = await Conn.QueryAsync<ItemProgramado>( | |
| new CommandDefinition(sql, new { ItemControleId = itemControleId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<ItemProgramado?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Columns} FROM itens_programados WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<ItemProgramado>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> ItemControleExistsAsync(Guid itemControleId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM itens_controle WHERE id = @Id"; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Id = itemControleId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task AddAsync(ItemProgramado i, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO itens_programados (id, item_controle_id, descricao, quantidade, data_programada, peso, subatividade_padrao_id, tipo_atividade) | |
| VALUES (@Id, @ItemControleId, @Descricao, @Quantidade, @DataProgramada, @Peso, @SubAtividadePadraoId, @TipoAtividade) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(i), Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(ItemProgramado i, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| UPDATE itens_programados | |
| SET descricao = @Descricao, quantidade = @Quantidade, data_programada = @DataProgramada, | |
| peso = @Peso, subatividade_padrao_id = @SubAtividadePadraoId, tipo_atividade = @TipoAtividade | |
| WHERE id = @Id | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(i), Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM itens_programados WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private static object ToParameters(ItemProgramado i) => new | |
| { | |
| i.Id, | |
| i.ItemControleId, | |
| i.Descricao, | |
| i.Quantidade, | |
| i.DataProgramada, | |
| i.Peso, | |
| i.SubAtividadePadraoId, | |
| TipoAtividade = (int)i.TipoAtividade | |
| }; | |
| } | |