diff --git a/Biblioteca/Biblioteca.http b/Biblioteca/Biblioteca.http index 1e14aae..3422d7b 100644 --- a/Biblioteca/Biblioteca.http +++ b/Biblioteca/Biblioteca.http @@ -1,6 +1,40 @@ -@Biblioteca_HostAddress = http://localhost:5000 +@url = http://localhost:5000 -GET {{Biblioteca_HostAddress}}/weatherforecast/ +### Obtém uma lista de livros + +GET {{url}}/livros Accept: application/json -### +### Cria um novo livro + +POST {{url}}/livros +Accept: application/json +Content-Type: application/json + +{ + "isbn": "9780321741769", + "titulo": "The C# Programming Language", + "autor": "Anders Hejlsberg" +} + +### Edita um livro + +PUT {{url}}/livros/9780321741769 +Accept: application/json +Content-Type: application/json + +{ + "isbn": "9780321741769", + "titulo": "The C# Programming Language", + "autor": "Anders Hejlsberg, Mads Torgensen" +} + +### Obtém um livro individual + +GET {{url}}/livros/9780321741769 +Accept: application/json + +### Remove um livro + +DELETE {{url}}/livros/9780321741769 +Accept: application/json \ No newline at end of file diff --git a/Biblioteca/Program.cs b/Biblioteca/Program.cs index 3917ef1..a566a94 100644 --- a/Biblioteca/Program.cs +++ b/Biblioteca/Program.cs @@ -1,41 +1,71 @@ -var builder = WebApplication.CreateBuilder(args); +using Microsoft.AspNetCore.Mvc; -// Add services to the container. -// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi -builder.Services.AddOpenApi(); +var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) +app.MapGet("/", () => { - app.MapOpenApi(); -} + string[] saudacoes = ["Olá!", "こんにちは", "Привет", "Ողջույն"]; -app.UseHttpsRedirection(); + return saudacoes[Random.Shared.Next(saudacoes.Length)]; +}); -var summaries = new[] +// Obtém uma lista com os livros registrados. +app.MapGet("/livros", () => { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" -}; + 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" + } + }; +}); -app.MapGet("/weatherforecast", () => +// Cria um novo livro. +app.MapPost("/livros", (Livro livro) => { - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; -}) -.WithName("GetWeatherForecast"); + return livro; +}); + +// Edita um livro. +app.MapPut("/livros/{isbn}", (string isbn, Livro livro) => +{ + return new { editando = isbn, dados = livro }; +}); + +// Obtém os dados de um livro individual. +app.MapGet("/livros/{isbn}", (string isbn) => +{ + return new Livro() { Isbn = isbn }; +}); + +// Remove um livro. +app.MapDelete("/livros/{isbn}", (string isbn) => +{ + return Results.NoContent(); +}); app.Run(); -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) +public class Livro { - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} + public string? Isbn { get; set; } + public string? Titulo { get; set; } + public string? Autor { get; set; } +}; \ No newline at end of file