ws8/arthur-faria/Lab2 #1

Open
admin wants to merge 3 commits from ws8/arthur-faria/Lab2 into main
2 changed files with 70 additions and 6 deletions

View File

@@ -20,23 +20,86 @@ namespace Workshop8.Data
public async Task<IEnumerable<Cliente>> GetAllClientsAsync()
{
// TODO: implementar SELECT
return new List<Cliente>();
var clientes = new List<Cliente>();
await using (var connection = new SqliteConnection(_connectionString))
{
await connection.OpenAsync();
await using (var cmd = connection.CreateCommand())
{
cmd.CommandText = @"SELECT Id, Nome, Email, CriadoEm
FROM Clientes
ORDER BY Id;";
await using (var reader = await cmd.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
clientes.Add(new Cliente
{
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();
}
}
}
}
}

View File

@@ -3,8 +3,9 @@
@{
ViewData["Title"] = "Clientes";
}
<div class="d-flex justify-content-between align-items-center my-3">
<h2>Clientes</h2>
<h3>Clientes</h3>
<button class="btn btn-primary" type="button" id="createClientBtn">Adicionar Cliente</button>
</div>
<table class="table table-striped">