Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Bb]in/
[Oo]bj/
[Ll]og/

# Visual Studio Code
.vscode/
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# Visual Studio cache/options directory
.vs/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

# NUnit
*.VisualState.xml
TestResult.xml

# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/

# StyleCop
StyleCopReport.xml

# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc

# NuGet Packages
*.nupkg
**/packages/*
!**/packages/build/
*.nuget.props
*.nuget.targets

# Others
*.cache
*.publishsettings
node_modules/
*.dll
*.exe
72 changes: 72 additions & 0 deletions Hamurabi.Api/Controllers/BooksController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Microsoft.AspNetCore.Mvc;
using Hamurabi.Core.Interfaces;
using Hamurabi.Core.Models;

namespace Hamurabi.Api.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class BooksController : ControllerBase
{
private readonly IBookService _bookService;

public BooksController(IBookService bookService)
{
_bookService = bookService;
}

// GET: api/books
[HttpGet]
public ActionResult<List<Book>> GetAllBooks()
{
var books = _bookService.GetAllBooks();
return Ok(books);
}

// GET: api/books/search?term=Jules
[HttpGet("search")]
public ActionResult<List<Book>> SearchBooks([FromQuery] string term)
{
if (string.IsNullOrWhiteSpace(term))
{
return BadRequest("O termo de busca não pode ser vazio.");
}

var books = _bookService.SearchBooks(term);
return Ok(books);
}

// GET: api/books/ordered?ascending=true
[HttpGet("ordered")]
public ActionResult<List<Book>> GetBooksOrdered([FromQuery] bool ascending = true)
{
var books = _bookService.GetAllBooks();
var orderedBooks = _bookService.OrderByPrice(books, ascending);
return Ok(orderedBooks);
}

// GET: api/books/5/shipping
[HttpGet("{id}/shipping")]
public ActionResult<object> GetShipping(int id)
{
var books = _bookService.GetAllBooks();
var book = books.FirstOrDefault(b => b.Id == id);

if (book == null)
{
return NotFound($"Livro com ID {id} não encontrado.");
}

var result = new
{
bookId = book.Id,
bookName = book.Name,
price = book.Price,
shipping = book.CalculateShipping(),
totalPrice = book.GetTotalPrice()
};

return Ok(result);
}
}
}
18 changes: 18 additions & 0 deletions Hamurabi.Api/Hamurabi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Hamurabi.Core\Hamurabi.Core.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions Hamurabi.Api/Hamurabi.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@Hamurabi_HostAddress = http://localhost:5161

GET {{Hamurabi_HostAddress}}/weatherforecast/
Accept: application/json

###
35 changes: 35 additions & 0 deletions Hamurabi.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Hamurabi.Core.Interfaces;
using Hamurabi.Core.Repositories;
using Hamurabi.Core.Services;

var builder = WebApplication.CreateBuilder(args);

// Adicionar suporte a Controllers
builder.Services.AddControllers();

// Configurar Swagger/OpenAPI
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Configurar caminho do arquivo books.json
var booksFilePath = Path.Combine(builder.Environment.ContentRootPath, "..", "books.json");

// Registrar dependências (Injeção de Dependência)
builder.Services.AddSingleton<IBookRepository>(new BookRepository(booksFilePath));
builder.Services.AddScoped<IBookService, BookService>();

var app = builder.Build();

// Configurar o pipeline HTTP - SEMPRE habilitar Swagger (não só em Development)
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Catálogo de Livros API v1");
c.RoutePrefix = string.Empty;
});

app.UseAuthorization();

app.MapControllers();

app.Run();
23 changes: 23 additions & 0 deletions Hamurabi.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5161",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7147;http://localhost:5161",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions Hamurabi.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions Hamurabi.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
9 changes: 9 additions & 0 deletions Hamurabi.Core/Hamurabi.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
11 changes: 11 additions & 0 deletions Hamurabi.Core/Interfaces/IBookRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Hamurabi.Core.Models;

namespace Hamurabi.Core.Interfaces
{
///Contrato para operações de leitura do catálogo de livros
public interface IBookRepository
{
// Retorna todos os livros do catálogo
List<Book> GetAllBooks();
}
}
29 changes: 29 additions & 0 deletions Hamurabi.Core/Interfaces/IBookService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Hamurabi.Core.Models;

namespace Hamurabi.Core.Interfaces
{

// Contrato para operações de busca e manipulação de livros

public interface IBookService
{

// Busca livros por qualquer especificação (autor, nome, etc)

// <param name="searchTerm">Termo de busca</param>
// <returns>Lista de livros encontrados</returns>
List<Book> SearchBooks(string searchTerm);


// Ordena livros por preço

// <param name="books">Lista de livros</param>
// <param name="ascending">true = ascendente, false = descendente</param>
// <returns>Lista ordenada</returns>
List<Book> OrderByPrice(List<Book> books, bool ascending = true);


// Retorna todos os livros
List<Book> GetAllBooks();
}
}
23 changes: 23 additions & 0 deletions Hamurabi.Core/Models/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Hamurabi.Core.Models
{
// Representa um livro do catálogo
public class Book
{
public int Id { get; set; }
public string? Name { get; set; }
public decimal Price { get; set; }
public BookSpecifications? Specifications { get; set; }

// Calcula o valor do frete (20% do preço do livro)
public decimal CalculateShipping()
{
return Price * 0.20m;
}

// Retorna o preço total (preço + frete)
public decimal GetTotalPrice()
{
return Price + CalculateShipping();
}
}
}
23 changes: 23 additions & 0 deletions Hamurabi.Core/Models/BookSpecifications.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json.Serialization;

namespace Hamurabi.Core.Models
{
// Especificações detalhadas de um livro
public class BookSpecifications
{
[JsonPropertyName("Originally published")]
public string? OriginallyPublished { get; set; }

[JsonPropertyName("Author")]
public string? Author { get; set; }

[JsonPropertyName("Page count")]
public int PageCount { get; set; }

[JsonPropertyName("Illustrator")]
public object? Illustrator { get; set; }

[JsonPropertyName("Genres")]
public object? Genres { get; set; }
}
}
Loading