From 7e734919fbdf5a67326173e58863c38b27f79b36 Mon Sep 17 00:00:00 2001 From: 703558 <703558@pucminas.br> Date: Wed, 30 Jul 2025 16:25:48 -0300 Subject: [PATCH] listagem --- Data/SqliteClientRepository.cs | 47 +++++++++++++++++++++++++++++++--- local.db | 0 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 local.db diff --git a/Data/SqliteClientRepository.cs b/Data/SqliteClientRepository.cs index 2aab76a..3184bbd 100644 --- a/Data/SqliteClientRepository.cs +++ b/Data/SqliteClientRepository.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Data; using System.Threading.Tasks; using Microsoft.Data.Sqlite; using Microsoft.Extensions.Configuration; @@ -20,13 +21,53 @@ namespace Workshop8.Data public async Task> GetAllClientsAsync() { - // TODO: implementar SELECT - return new List(); + var itens = new List(); + + using SqliteConnection conn = await GetConnection(); + + var cmd = conn.CreateCommand(); + + cmd.CommandText = "SELECT Id, Nome, Email, CriadoEm FROM Clientes"; + + await using var reader = await cmd.ExecuteReaderAsync(); + + while (await reader.ReadAsync()) + { + var p = new Cliente + { + Id = reader.GetInt32(0), + Nome = reader.GetString(1), + Email = reader.GetString(2), + CriadoEm = reader.GetDateTime(3) + }; + itens.Add(p); + } + return itens; + } + + private static async Task GetConnection() + { + var conn = new SqliteConnection("Data Source=database.sqlite;"); + await conn.OpenAsync(); + return conn; } public async Task AddClientAsync(Cliente c) { - // TODO: implementar INSERT + var conn = await GetConnection(); + + var cmd = conn.CreateCommand(); + + cmd.CommandText = @"INSERT INTO Clientes (nome, email, criadoEm) + VALUES (@nome, @email, @criadoEm) "; + + cmd.CommandType = CommandType.Text; + cmd.Parameters.Add(new SqliteParameter("@nome",c.Nome)); + cmd.Parameters.Add(new SqliteParameter("@email",c.Email)); + cmd.Parameters.Add(new SqliteParameter("@criadoEm",c.CriadoEm)); + + await using cmd.ExecuteNonQueryAsync(); + } public async Task UpdateClientAsync(Cliente c) diff --git a/local.db b/local.db new file mode 100644 index 0000000..e69de29