1 Commits

Author SHA1 Message Date
88cfc3fc9a Update Data/SqliteClientRepository.cs
Implementação do CRUD completo de clientes
2025-07-30 18:03:12 +00:00

View File

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
using Workshop8.Models;
using System;
namespace Workshop8.Data
{
@@ -20,44 +21,76 @@ namespace Workshop8.Data
public async Task<IEnumerable<Cliente>> GetAllClientsAsync()
{
// SELECT
var clientes = new List<Cliente>();
var clients = new List<Cliente>();
await using var conn = new SqliteConnection(_connectionString);
await conn.OpenAsync();
using var connection = new SqliteConnection(_connectionString);
await connection.OpenAsync();
var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT id, nome, email, CriadoEm FROM Clientes;";
var command = connection.CreateCommand();
command.CommandText = "SELECT Id, Nome, Email, CriadoEm FROM Clientes";
await using var reader = await cmd.ExecuteReaderAsync();
using var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
var c = new Cliente
clients.Add(new Cliente
{
Id = reader.GetInt32(0),
Nome = reader.GetString(1),
Email = reader.GetString(2),
CriadoEm = reader.GetDateTime(3)
};
clientes.Add(c);
});
}
return clientes;
return new List<Cliente>();
return clients;
}
public async Task AddClientAsync(Cliente c)
{
// TODO: implementar INSERT
using var connection = new SqliteConnection(_connectionString);
await connection.OpenAsync();
var command = connection.CreateCommand();
command.CommandText = @"
INSERT INTO Clientes (Nome, Email, CriadoEm)
VALUES ($nome, $email, $criadoEm);
";
command.Parameters.AddWithValue("$nome", c.Nome);
command.Parameters.AddWithValue("$email", c.Email);
command.Parameters.AddWithValue("$criadoEm", c.CriadoEm);
await command.ExecuteNonQueryAsync();
}
public async Task UpdateClientAsync(Cliente c)
{
// TODO: implementar UPDATE
using var connection = new SqliteConnection(_connectionString);
await connection.OpenAsync();
var command = connection.CreateCommand();
command.CommandText = @"
UPDATE Clientes
SET Nome = $nome, Email = $email
WHERE Id = $id;
";
command.Parameters.AddWithValue("$nome", c.Nome);
command.Parameters.AddWithValue("$email", c.Email);
command.Parameters.AddWithValue("$id", c.Id);
await command.ExecuteNonQueryAsync();
}
public async Task DeleteClientAsync(int id)
{
// TODO: implementar DELETE
using var connection = new SqliteConnection(_connectionString);
await connection.OpenAsync();
var command = connection.CreateCommand();
command.CommandText = "DELETE FROM Clientes WHERE Id = $id;";
command.Parameters.AddWithValue("$id", id);
await command.ExecuteNonQueryAsync();
}
}
}