diff --git a/.vscode/launch.json b/.vscode/launch.json
index c5a205c..0bfaa89 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -18,10 +18,10 @@
"cwd": "${workspaceFolder}/Biblioteca",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
- "serverReadyAction": {
- "action": "openExternally",
- "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
- },
+ // "serverReadyAction": {
+ // "action": "openExternally",
+ // "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
+ // },
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
diff --git a/Biblioteca/Biblioteca.csproj b/Biblioteca/Biblioteca.csproj
index 4bc7159..41f87ac 100644
--- a/Biblioteca/Biblioteca.csproj
+++ b/Biblioteca/Biblioteca.csproj
@@ -8,6 +8,7 @@
+
diff --git a/Biblioteca/Biblioteca.http b/Biblioteca/Biblioteca.http
index 3422d7b..04685e6 100644
--- a/Biblioteca/Biblioteca.http
+++ b/Biblioteca/Biblioteca.http
@@ -12,9 +12,9 @@ Accept: application/json
Content-Type: application/json
{
- "isbn": "9780321741769",
- "titulo": "The C# Programming Language",
- "autor": "Anders Hejlsberg"
+ "isbn": "abc2",
+ "titulo": "teste",
+ "autor": "teste"
}
### Edita um livro
@@ -31,7 +31,7 @@ Content-Type: application/json
### Obtém um livro individual
-GET {{url}}/livros/9780321741769
+GET {{url}}/livros/abc1
Accept: application/json
### Remove um livro
diff --git a/Biblioteca/Models/Livro.cs b/Biblioteca/Models/Livro.cs
new file mode 100644
index 0000000..f2e60fc
--- /dev/null
+++ b/Biblioteca/Models/Livro.cs
@@ -0,0 +1,15 @@
+namespace Biblioteca.Models;
+
+public class Livro
+{
+ public string? Isbn { get; set; }
+ public string? Titulo { get; set; }
+ public string? Autor { get; set; }
+ public string? Genero { get; set; }
+ public string? Descricao { get; set; }
+ public string? Foto { get; set; }
+ public string? Keywords { get; set; }
+ public bool Ativo { get; set; }
+ public DateTime CriadoEm { get; set; }
+ public DateTime AtualizadoEm { get; set; }
+}
\ No newline at end of file
diff --git a/Biblioteca/Program.cs b/Biblioteca/Program.cs
index a566a94..00115e0 100644
--- a/Biblioteca/Program.cs
+++ b/Biblioteca/Program.cs
@@ -1,3 +1,5 @@
+using Biblioteca.Models;
+using Biblioteca.Repositories;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
@@ -12,60 +14,47 @@ app.MapGet("/", () =>
});
// Obtém uma lista com os livros registrados.
-app.MapGet("/livros", () =>
+app.MapGet("/livros", async () =>
{
- return new Livro[]
- {
- new()
- {
- Isbn = "9780262510875",
- Titulo = "Structure and Interpretation of Computer Programs",
- Autor = "Gerald Jay Sussman"
- },
- new()
- {
- Isbn = "9780131103627",
- Titulo = "C Programming Language: ANSI C Version",
- Autor = "Dennis Ritchie, Brian Kerningham"
- },
- new()
- {
- Isbn = "9780134190440",
- Titulo = "The Go Programming Language",
- Autor = "Brian Kerningham"
- }
- };
+ var repo = new LivroRepository();
+ var resultado = await repo.Obter(pagina: 1);
+
+ return resultado;
});
// Cria um novo livro.
-app.MapPost("/livros", (Livro livro) =>
+app.MapPost("/livros", async (Livro livro) =>
{
- return livro;
+ var repo = new LivroRepository();
+ var resultado = await repo.Criar(livro);
+
+ return resultado;
});
// Edita um livro.
-app.MapPut("/livros/{isbn}", (string isbn, Livro livro) =>
+app.MapPut("/livros/{isbn}", async (string isbn, Livro livro) =>
{
- return new { editando = isbn, dados = livro };
+ var repo = new LivroRepository();
+ var resultado = await repo.Editar(isbn, livro);
+
+ return resultado;
});
// Obtém os dados de um livro individual.
-app.MapGet("/livros/{isbn}", (string isbn) =>
+app.MapGet("/livros/{isbn}", async (string isbn) =>
{
- return new Livro() { Isbn = isbn };
+ var repo = new LivroRepository();
+ var resultado = await repo.Obter(isbn);
+
+ return resultado;
});
// Remove um livro.
-app.MapDelete("/livros/{isbn}", (string isbn) =>
+app.MapDelete("/livros/{isbn}", async (string isbn) =>
{
- return Results.NoContent();
+ var repo = new LivroRepository();
+
+ await repo.Desativar(isbn);
});
-app.Run();
-
-public class Livro
-{
- public string? Isbn { get; set; }
- public string? Titulo { get; set; }
- public string? Autor { get; set; }
-};
\ No newline at end of file
+app.Run();
\ No newline at end of file
diff --git a/Biblioteca/Repositories/LivroRepository.cs b/Biblioteca/Repositories/LivroRepository.cs
new file mode 100644
index 0000000..a3b76f6
--- /dev/null
+++ b/Biblioteca/Repositories/LivroRepository.cs
@@ -0,0 +1,191 @@
+using Biblioteca.Models;
+using MySqlConnector;
+
+namespace Biblioteca.Repositories;
+
+public class LivroRepository
+{
+ private const string ConnString = "Server=gbrl.dev;Port=5306;User ID=sistemasdistribuidos.aluno;Password=eW03avS7M8kOUL1A9bZWW2RTIfzEI1Di;Database=sistemasdistribuidos";
+
+ public async Task> Obter(int pagina)
+ {
+ using var conn = new MySqlConnection(ConnString);
+ using var cmd = conn.CreateCommand();
+
+ await conn.OpenAsync();
+
+ var take = 30;
+ var offset = take * Math.Max(pagina-1, 0);
+ var lista = new List(capacity: take);
+
+ cmd.CommandText = "SELECT Isbn, Titulo, Autor, Genero, Descricao, Foto, Keywords, Ativo, CriadoEm, AtualizadoEm FROM Livro ORDER BY CriadoEm LIMIT @offset,@take";
+ cmd.Parameters.AddWithValue("offset", offset);
+ cmd.Parameters.AddWithValue("take", take);
+
+ using var reader = await cmd.ExecuteReaderAsync();
+
+ while (await reader.ReadAsync())
+ {
+ lista.Add(new()
+ {
+ Isbn = reader.GetString(0),
+ Titulo = reader.GetString(1),
+ Autor = reader.GetString(2),
+ Genero = reader.GetString(3),
+ Descricao = reader.GetString(4),
+ Foto = reader.GetString(5),
+ Keywords = reader.GetString(6),
+ Ativo = reader.GetBoolean(7),
+ CriadoEm = reader.GetDateTime(8),
+ AtualizadoEm = reader.GetDateTime(9),
+ });
+ }
+
+ return lista;
+ }
+
+ ///
+ /// Obtém um livro pelo seu ISBN.
+ ///
+ ///
+ ///
+ public async Task Obter(string isbn)
+ {
+ using var conn = new MySqlConnection(ConnString);
+ using var cmd = conn.CreateCommand();
+
+ await conn.OpenAsync();
+
+ cmd.CommandText = "SELECT Isbn, Titulo, Autor, Genero, Descricao, Foto, Keywords, Ativo, CriadoEm, AtualizadoEm FROM Livro WHERE Isbn=@isbn";
+ cmd.Parameters.AddWithValue("isbn", isbn);
+
+ using var reader = await cmd.ExecuteReaderAsync();
+
+ var existe = await reader.ReadAsync();
+
+ if (!existe)
+ {
+ throw new Exception($"Livro com ISBN {isbn} não encontrado");
+ }
+
+ return new()
+ {
+ Isbn = reader.GetString(0),
+ Titulo = reader.GetString(1),
+ Autor = reader.GetString(2),
+ Genero = reader.GetString(3),
+ Descricao = reader.GetString(4),
+ Foto = reader.GetString(5),
+ Keywords = reader.GetString(6),
+ Ativo = reader.GetBoolean(7),
+ CriadoEm = reader.GetDateTime(8),
+ AtualizadoEm = reader.GetDateTime(9),
+ };
+ }
+
+ public async Task Criar(Livro dados)
+ {
+ using var conn = new MySqlConnection(ConnString);
+ using var cmd = conn.CreateCommand();
+
+ await conn.OpenAsync();
+
+ var livro = new Livro
+ {
+ Isbn = dados.Isbn?.Trim() ?? "",
+ Titulo = dados.Titulo?.Trim() ?? "",
+ Autor = dados.Autor?.Trim() ?? "",
+ Genero = dados.Genero?.Trim() ?? "",
+ Descricao = dados.Descricao?.Trim() ?? "",
+ Foto = dados.Foto?.Trim() ?? "",
+ Keywords = dados.Keywords?.Trim() ?? "",
+ Ativo = true,
+ CriadoEm = DateTime.Now,
+ AtualizadoEm = default,
+ };
+
+ if (livro.Isbn == "")
+ {
+ throw new Exception("O ISBN do livro é obrigatório.");
+ }
+
+ if (livro.Titulo == "")
+ {
+ throw new Exception("O título do livro é obrigatório.");
+ }
+
+ cmd.CommandText =
+ @"
+ INSERT INTO Livro
+ (Isbn, Titulo, Autor, Genero, Descricao, Foto, Keywords, Ativo, CriadoEm, AtualizadoEm)
+ VALUES
+ (@isbn, @titulo, @autor, @genero, @descricao, @foto, @keywords, @ativo, @criadoem, @atualizadoem)
+ ";
+
+ cmd.Parameters.AddWithValue("isbn", livro.Isbn);
+ cmd.Parameters.AddWithValue("titulo", livro.Titulo);
+ cmd.Parameters.AddWithValue("autor", livro.Autor);
+ cmd.Parameters.AddWithValue("genero", livro.Genero);
+ cmd.Parameters.AddWithValue("descricao", livro.Descricao);
+ cmd.Parameters.AddWithValue("foto", livro.Foto);
+ cmd.Parameters.AddWithValue("keywords", livro.Keywords);
+ cmd.Parameters.AddWithValue("ativo", livro.Ativo);
+ cmd.Parameters.AddWithValue("criadoem", livro.CriadoEm);
+ cmd.Parameters.AddWithValue("atualizadoem", livro.AtualizadoEm);
+
+ await cmd.ExecuteNonQueryAsync();
+
+ return livro;
+ }
+
+ public async Task Editar(string isbn, Livro dados)
+ {
+ using var conn = new MySqlConnection(ConnString);
+ using var cmd = conn.CreateCommand();
+
+ await conn.OpenAsync();
+
+ var livro = await Obter(isbn);
+
+ livro.Isbn = dados.Isbn?.Trim() ?? "";
+ livro.Titulo = dados.Titulo?.Trim() ?? "";
+ livro.Autor = dados.Autor?.Trim() ?? "";
+ livro.Genero = dados.Genero?.Trim() ?? "";
+ livro.Descricao = dados.Descricao?.Trim() ?? "";
+ livro.Foto = dados.Foto?.Trim() ?? "";
+ livro.Keywords = dados.Keywords?.Trim() ?? "";
+ livro.AtualizadoEm = DateTime.Now;
+
+ cmd.CommandText =
+ @"
+ UPDATE Livro SET
+ Isbn=@isbn, Titulo=@titulo, Autor=@autor, Genero=@genero, Descricao=@descricao, Foto=@foto, Keywords=@keywords, Status=@status, AtualizadoEm=@atualizadoem
+ ";
+
+ cmd.Parameters.AddWithValue("isbn", livro.Isbn);
+ cmd.Parameters.AddWithValue("titulo", livro.Titulo);
+ cmd.Parameters.AddWithValue("autor", livro.Autor);
+ cmd.Parameters.AddWithValue("genero", livro.Genero);
+ cmd.Parameters.AddWithValue("descricao", livro.Descricao);
+ cmd.Parameters.AddWithValue("foto", livro.Foto);
+ cmd.Parameters.AddWithValue("keywords", livro.Keywords);
+ cmd.Parameters.AddWithValue("atualizadoem", livro.AtualizadoEm);
+
+ await cmd.ExecuteNonQueryAsync();
+
+ return livro;
+ }
+
+ public async Task Desativar(string isbn)
+ {
+ using var conn = new MySqlConnection(ConnString);
+ using var cmd = conn.CreateCommand();
+
+ await conn.OpenAsync();
+
+ cmd.CommandText = "UPDATE Livro SET Ativo=0 WHERE Isbn=@isbn";
+ cmd.Parameters.AddWithValue("isbn", isbn);
+
+ await cmd.ExecuteNonQueryAsync();
+ }
+}
\ No newline at end of file
diff --git a/scripts/schema.sql b/scripts/schema.sql
new file mode 100644
index 0000000..a099839
--- /dev/null
+++ b/scripts/schema.sql
@@ -0,0 +1,15 @@
+CREATE TABLE Livro (
+ Isbn VARCHAR(255) PRIMARY KEY NOT NULL,
+ Titulo VARCHAR(512) NOT NULL,
+ Autor TEXT NOT NULL,
+ Genero TEXT NOT NULL,
+ Descricao TEXT NOT NULL,
+ Foto TEXT NOT NULL,
+ Keywords TEXT NOT NULL,
+ Ativo BOOLEAN NOT NULL DEFAULT 0,
+ CriadoEm DATETIME NOT NULL,
+ AtualizadoEm DATETIME NOT NULL,
+
+ FULLTEXT (Titulo, Autor, Genero, Descricao, Keywords),
+ INDEX (CriadoEm)
+);
\ No newline at end of file