Skip to content

Commit

Permalink
changes related to the api interface
Browse files Browse the repository at this point in the history
  • Loading branch information
GoliasVictor committed Dec 17, 2024
1 parent ec3a3db commit 4e11a60
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 42 deletions.
8 changes: 4 additions & 4 deletions src/LivrEtec.GIB/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace LivrEtec.GIB.Controllers;

[Route("api/sessao")]
[Route("auth")]
[ApiController]
public sealed class AuthController: ControllerBase
{
Expand All @@ -22,10 +22,10 @@ public AuthController(ILogger<AuthController> logger, IAutenticacaoService auten
this.authKeyProvider = authKeyProvider;
this.repUsuarios = repUsuarios;
}
public record LoginRequest(int IdUsuario, string HashSenha);
public record RequestLogin(int IdUsuario, string HashSenha);
[AllowAnonymous]
[HttpPost("login")]
public async Task<ActionResult<string>> Login(LoginRequest request)
public async Task<ActionResult<string>> Login(RequestLogin request)
{
if (await autenticacaoService.EhAutentico(request.IdUsuario, request.HashSenha))
return TokenService.GerarToken(request.IdUsuario, authKeyProvider.authKey);
Expand All @@ -43,7 +43,7 @@ public async Task<ActionResult<bool>> EhAutorizado(int id_permissao)
}

[HttpGet("usuario")]
public async Task<LEM::Usuario> CarregarUsuario()
public async Task<DTO::Usuario> CarregarUsuario()
{
await identidadeService.CarregarUsuario();
return identidadeService.Usuario!;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
namespace LivrEtec.GIB.Controllers;

[Route("api/emprestimos")]
[Route("emprestimos")]
[ApiController]
public sealed class EmprestimoController
public sealed class EmprestimosController
{
private readonly ILogger<EmprestimoController> logger;
private readonly ILogger<EmprestimosController> logger;
private readonly IEmprestimoService emprestimoService;
private readonly IIdentidadeService identidadeService;
public EmprestimoController(ILogger<EmprestimoController> logger, IEmprestimoService emprestimoService, IIdentidadeService identidadeService)
public EmprestimosController(ILogger<EmprestimosController> logger, IEmprestimoService emprestimoService, IIdentidadeService identidadeService)
{
this.logger = logger;
this.emprestimoService = emprestimoService;
this.identidadeService = identidadeService;
}
public record AbrirRequest(int IdPessoa, int IdLivro);
public record RequestAbrirEmprestimo(int IdPessoa, int IdLivro);
[HttpPost()]
public async Task<int> Abrir(AbrirRequest request)
public async Task<int> Abrir(RequestAbrirEmprestimo request)
{
return await emprestimoService.Abrir(request.IdPessoa, request.IdLivro);
}
public record BuscarRequest(
public record RequestBuscar(
int? IdLivro,
int? IdPessoa,
bool? Fechado,
bool? Atrasado
);
[HttpGet("buscar")]
public async Task<IEnumerable<DTO::Emprestimo>> Buscar([FromQuery] BuscarRequest request)
[HttpGet()]
public async Task<IEnumerable<DTO::Emprestimo>> Buscar([FromQuery] RequestBuscar request)
{
return (await emprestimoService.Buscar(new LEM::ParamBuscaEmprestimo(
IdLivro: request.IdLivro,
Expand All @@ -35,34 +35,34 @@ public record BuscarRequest(
Atrasado: request.Atrasado
))).Select(e => (DTO::Emprestimo)e);
}
public record DevolverRequest(
public record RequestDevolverEmprestimo(
int IdEmprestimo,
bool? AtrasoJustificado,
string? ExplicacaoAtraso
);
[HttpPatch("devolver")]
public async Task Devolver(DevolverRequest request)
public async Task Devolver(RequestDevolverEmprestimo request)
{
await emprestimoService.Devolver(
request.IdEmprestimo,
request.AtrasoJustificado,
request.ExplicacaoAtraso
);
}
public record ProrrogarRequest(
public record RequestProrrogarEmprestimo(
int IdEmprestimo,
DateTime NovaData
);
[HttpPatch("prorrogar")]
public async Task Prorrogar(ProrrogarRequest request)
public async Task Prorrogar(RequestProrrogarEmprestimo request)
{
await emprestimoService.Prorrogar(request.IdEmprestimo, request.NovaData);
}
public record PerdaRequest(
public record RequestPerdaEmprestimo(
int id
);
[HttpPatch("perda")]
public async Task RegistrarPerda(PerdaRequest request)
public async Task RegistrarPerda(RequestPerdaEmprestimo request)
{
await emprestimoService.RegistrarPerda(request.id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/LivrEtec.GIB/Controllers/LivrosController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

namespace LivrEtec.GIB.Controllers;

[Route("api/livros")]
[Route("livros")]
[ApiController]
public sealed class LivrosController(ILogger<LivrosController> logger, ILivrosService livrosService)
{
Expand Down
2 changes: 1 addition & 1 deletion src/LivrEtec.GIB/Controllers/TagsController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LivrEtec.GIB.Services;

[Route("api/tags")]
[Route("tags")]
[ApiController]
public sealed class TagsController(ILogger<TagsController> logger, ITagsService tagsService)
{
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions src/LivrEtec.Testes/Client/EmprestimoServiceAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task<int> Abrir(int idPessoa, int idlivro)
{

var response = await client.PostAsJsonAsync(
"/api/emprestimos",
"emprestimos",
new AbrirRequest(idPessoa, idlivro)
);
response.EnsureSuccessStatusCode();
Expand All @@ -28,28 +28,28 @@ public async Task Devolver(int idEmprestimo, bool? AtrasoJustificado = null, str
{

var request = new DevolverRequest(idEmprestimo, AtrasoJustificado, ExplicacaoAtraso);
await client.PatchAsJsonAsync("api/emprestimos/devolver", request);
await client.PatchAsJsonAsync("emprestimos/devolver", request);

}
public async Task Prorrogar(int idEmprestimo, DateTime novaData)
{


var request = new ProrrogarRequest(idEmprestimo, novaData);
await client.PatchAsJsonAsync("api/emprestimos/prorrogar", request);
await client.PatchAsJsonAsync("emprestimos/prorrogar", request);

}

public async Task RegistrarPerda(int idEmprestimo)
{
await client.PatchAsJsonAsync(
"api/emprestimos/perda",
"emprestimos/perda",
new PerdaRequest(idEmprestimo)
);

}
public async Task Excluir(int idEmprestimo)
{
await client.DeleteAsync($"api/emprestimos/{idEmprestimo}");
await client.DeleteAsync($"emprestimos/{idEmprestimo}");
}
}
12 changes: 5 additions & 7 deletions src/LivrEtec.Testes/Client/LivrosServiceAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public async Task Editar(LEM::Livro livro)

livro.Tags ??= new();

_ = await client.PutAsJsonAsync("api/livros", (DTO::Livro)livro);
_ = await client.PutAsJsonAsync("livros", (DTO::Livro)livro);

}

public async Task<LEM::Livro?> Obter(int id)
{

return await client.GetFromJsonAsync<DTO::Livro>($"api/livros/{id}");
return await client.GetFromJsonAsync<DTO::Livro>($"livros/{id}");

}

Expand All @@ -48,13 +48,13 @@ public async Task Registrar(LEM::Livro livro)
{
throw new InvalidDataException();
}
_ = await client.PostAsJsonAsync("api/livros", (DTO::Livro)livro);
_ = await client.PostAsJsonAsync("livros", (DTO::Livro)livro);
}


public async Task Remover(int id)
{
_ = await client.DeleteAsync($"api/livros/{id}");
_ = await client.DeleteAsync($"livros/{id}");
}

public async Task<IEnumerable<LEM::Livro>> Buscar(string nome, string nomeAutor, IEnumerable<int>? idTags)
Expand All @@ -63,15 +63,13 @@ public async Task Remover(int id)
nomeAutor ??= "";
idTags ??= new List<int>();

var uri = "/api/livros";
var uri = "/livros";
uri = QueryHelpers.AddQueryString(uri, "NomeLivro", nome);
uri = QueryHelpers.AddQueryString(uri, "NomeAutor", nomeAutor);
foreach (var t in idTags)
uri = QueryHelpers.AddQueryString(uri, "IdTags", t.ToString());
Console.WriteLine(uri);
var response = await client.GetAsync(uri);
response.EnsureSuccessStatusCode();
Console.WriteLine(response.Content.ReadAsStringAsync());
return (await response.Content.ReadFromJsonAsync<IEnumerable<DTO::Livro>>())
.Select(l => (LEM::Livro)l!);
}
Expand Down
13 changes: 5 additions & 8 deletions src/LivrEtec.Testes/Client/TagsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,32 @@ public TagsServiceAPI(HttpClient client, ILogger<TagsServiceAPI> logger)
public async Task<int> Registrar(Tag tag)
{
Validador.ErroSeInvalido(tag);
var response = await client.PostAsJsonAsync("/api/tags", tag);
var response = await client.PostAsJsonAsync("tags", tag);
response.EnsureSuccessStatusCode();
return Int32.Parse(await response.Content.ReadAsStringAsync());



}

public async Task Editar(Tag tag)
{
_ = tag ?? throw new ArgumentNullException(nameof(tag));
_ = await client.PutAsJsonAsync("api/tags", (DTO::Tag)tag);
_ = await client.PutAsJsonAsync("tags", (DTO::Tag)tag);
}

public async Task<Tag?> Obter(int id)
{
return await client.GetFromJsonAsync<DTO::Tag?>($"api/tags/{id}");
return await client.GetFromJsonAsync<DTO::Tag?>($"tags/{id}");
}


public async Task Remover(int id)
{
_ = await client.DeleteAsync($"api/tags/{id}");
_ = await client.DeleteAsync($"tags/{id}");
}

public async Task<IEnumerable<Tag>> Buscar(string nome)
{
nome ??= "";
var uri = QueryHelpers.AddQueryString("api/tags", new Dictionary<string, string?> {
var uri = QueryHelpers.AddQueryString("tags", new Dictionary<string, string?> {
{nameof(nome), nome}
});
return (await client.GetFromJsonAsync<List<DTO::Tag>>(uri))
Expand Down
2 changes: 1 addition & 1 deletion src/LivrEtec.Testes/Configuracao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static Configuracao()

if (AppSettingsJsonPath is null)
{
Console.WriteLine("Arquvio de configuração appsettings.json não definido, sera usado ./appsettings.json por padrão");
Console.WriteLine("Caminho para arquvio de configuração appsettings.json não definido, sera usado ./appsettings.json por padrão");
AppSettingsJsonPath = "./appsettings.json";
}
IConfigurationRoot config = new ConfigurationBuilder()
Expand Down

0 comments on commit 4e11a60

Please sign in to comment.