2 Commits

Author SHA1 Message Date
703558
be468646f5 feat: list e insert 2025-07-30 16:40:47 -03:00
703558
7e734919fb listagem 2025-07-30 16:25:48 -03:00
4 changed files with 43 additions and 66 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
@@ -20,86 +21,63 @@ namespace Workshop8.Data
public async Task<IEnumerable<Cliente>> GetAllClientsAsync()
{
var clientes = new List<Cliente>();
var itens = new List<Cliente>();
await using (var connection = new SqliteConnection(_connectionString))
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())
{
await connection.OpenAsync();
await using (var cmd = connection.CreateCommand())
var p = new Cliente
{
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();
Id = reader.GetInt32(0),
Nome = reader.GetString(1),
Email = reader.GetString(2),
CriadoEm = reader.GetDateTime(3)
};
itens.Add(p);
}
return clientes;
return itens;
}
private static async Task<SqliteConnection> GetConnection()
{
var conn = new SqliteConnection("Data Source=database.sqlite;");
await conn.OpenAsync();
return conn;
}
public async Task AddClientAsync(Cliente c)
{
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();
}
}
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 cmd.ExecuteNonQueryAsync();
}
public async Task UpdateClientAsync(Cliente c)
{
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();
}
}
// TODO: implementar UPDATE
}
public async Task DeleteClientAsync(int id)
{
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();
}
}
// TODO: implementar DELETE
}
}
}

View File

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

Binary file not shown.

0
local.db Normal file
View File