From 68ccae9eb3fed38ecb8399ad919a83338d6f07e3 Mon Sep 17 00:00:00 2001 From: usuario Date: Wed, 30 Jul 2025 18:25:27 +0000 Subject: [PATCH] add: edicao de clientes --- Data/SqliteClientRepository.cs | 51 ++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/Data/SqliteClientRepository.cs b/Data/SqliteClientRepository.cs index 6cd12b3..37dfd03 100644 --- a/Data/SqliteClientRepository.cs +++ b/Data/SqliteClientRepository.cs @@ -36,31 +36,70 @@ namespace Workshop8.Data { clientes.Add(new Cliente { - Id = reader.GetInt32(0), - Nome = reader.GetString(1), - Email = reader.GetString(2), + Id = reader.GetInt32(0), + Nome = reader.GetString(1), + Email = reader.GetString(2), CriadoEm = reader.GetDateTime(3) }); } } } + connection.Close(); + connection.Dispose(); } return clientes; } public async Task AddClientAsync(Cliente c) { - // TODO: implementar INSERT + await using (var connection = new SqliteConnection(_connectionString)) + { + await connection.OpenAsync(); + await using (var cmd = connection.CreateCommand()) + { + cmd.CommandText = @" + INSERT INTO Clientes (Nome, Email, CriadoEm) + VALUES (@nome, @email, @criado);"; + cmd.Parameters.AddWithValue("@nome", c.Nome); + cmd.Parameters.AddWithValue("@email", c.Email); + cmd.Parameters.AddWithValue("@criado", c.CriadoEm); + await cmd.ExecuteNonQueryAsync(); + } + } } public async Task UpdateClientAsync(Cliente c) { - // TODO: implementar UPDATE + await using (var connection = new SqliteConnection(_connectionString)) + { + await connection.OpenAsync(); + await using (var cmd = connection.CreateCommand()) + { + cmd.CommandText = @" + UPDATE Clientes + SET Nome = @nome, + Email = @email + WHERE Id = @id;"; + cmd.Parameters.AddWithValue("@nome", c.Nome); + cmd.Parameters.AddWithValue("@email", c.Email); + cmd.Parameters.AddWithValue("@id", c.Id); + await cmd.ExecuteNonQueryAsync(); + } + } } public async Task DeleteClientAsync(int id) { - // TODO: implementar DELETE + await using (var connection = new SqliteConnection(_connectionString)) + { + await connection.OpenAsync(); + await using (var cmd = connection.CreateCommand()) + { + cmd.CommandText = @"DELETE FROM Clientes WHERE Id = @id;"; + cmd.Parameters.AddWithValue("@id", id); + await cmd.ExecuteNonQueryAsync(); + } + } } } }