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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1,021 changes: 1,021 additions & 0 deletions CatalogoLivros/.vs/CatalogoLivros/config/applicationhost.config

Large diffs are not rendered by default.

Binary file added CatalogoLivros/.vs/CatalogoLivros/v17/.futdcache.v2
Binary file not shown.
Binary file added CatalogoLivros/.vs/CatalogoLivros/v17/.suo
Binary file not shown.
53 changes: 53 additions & 0 deletions CatalogoLivros/.vs/CatalogoLivros/v17/DocumentLayout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"Version": 1,
"WorkspaceRootPath": "C:\\Users\\Anderson\\Desktop\\anderson\\Dev\\projetos\\CatalogoLivros\\CatalogoLivros\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{54C5792F-1497-49C0-8283-9ACCC74F8BCF}|CatalogoLivros\\CatalogoLivros.csproj|c:\\users\\anderson\\desktop\\anderson\\dev\\projetos\\catalogolivros\\catalogolivros\\catalogolivros\\controllers\\generoscontroller.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{54C5792F-1497-49C0-8283-9ACCC74F8BCF}|CatalogoLivros\\CatalogoLivros.csproj|solutionrelative:catalogolivros\\controllers\\generoscontroller.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{54C5792F-1497-49C0-8283-9ACCC74F8BCF}|CatalogoLivros\\CatalogoLivros.csproj|c:\\users\\anderson\\desktop\\anderson\\dev\\projetos\\catalogolivros\\catalogolivros\\catalogolivros\\controllers\\livroscontroller.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{54C5792F-1497-49C0-8283-9ACCC74F8BCF}|CatalogoLivros\\CatalogoLivros.csproj|solutionrelative:catalogolivros\\controllers\\livroscontroller.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
}
],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 255,
"SelectedChildIndex": 0,
"Children": [
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "GenerosController.cs",
"DocumentMoniker": "C:\\Users\\Anderson\\Desktop\\anderson\\Dev\\projetos\\CatalogoLivros\\CatalogoLivros\\CatalogoLivros\\Controllers\\GenerosController.cs",
"RelativeDocumentMoniker": "CatalogoLivros\\Controllers\\GenerosController.cs",
"ToolTip": "C:\\Users\\Anderson\\Desktop\\anderson\\Dev\\projetos\\CatalogoLivros\\CatalogoLivros\\CatalogoLivros\\Controllers\\GenerosController.cs",
"RelativeToolTip": "CatalogoLivros\\Controllers\\GenerosController.cs",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-07-13T13:31:58.094Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 1,
"Title": "LivrosController.cs",
"DocumentMoniker": "C:\\Users\\Anderson\\Desktop\\anderson\\Dev\\projetos\\CatalogoLivros\\CatalogoLivros\\CatalogoLivros\\Controllers\\LivrosController.cs",
"RelativeDocumentMoniker": "CatalogoLivros\\Controllers\\LivrosController.cs",
"ToolTip": "C:\\Users\\Anderson\\Desktop\\anderson\\Dev\\projetos\\CatalogoLivros\\CatalogoLivros\\CatalogoLivros\\Controllers\\LivrosController.cs",
"RelativeToolTip": "CatalogoLivros\\Controllers\\LivrosController.cs",
"ViewState": "AQIAAAAAAAAAAAAAAAAAAAsAAAAuAAAA",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2024-07-11T22:20:56.342Z"
}
]
}
]
}
]
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added CatalogoLivros/Anderson Curriculum.pdf
Binary file not shown.
25 changes: 25 additions & 0 deletions CatalogoLivros/CatalogoLivros.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.35013.160
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CatalogoLivros", "CatalogoLivros\CatalogoLivros.csproj", "{54C5792F-1497-49C0-8283-9ACCC74F8BCF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{54C5792F-1497-49C0-8283-9ACCC74F8BCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{54C5792F-1497-49C0-8283-9ACCC74F8BCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{54C5792F-1497-49C0-8283-9ACCC74F8BCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{54C5792F-1497-49C0-8283-9ACCC74F8BCF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9AB104C6-7010-4F6D-9E0B-A21B6301BDED}
EndGlobalSection
EndGlobal
29 changes: 29 additions & 0 deletions CatalogoLivros/CatalogoLivros/CatalogoLivros.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.7" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions CatalogoLivros/CatalogoLivros/CatalogoLivros.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Common/Api</Controller_SelectedScaffolderCategoryPath>
</PropertyGroup>
</Project>
6 changes: 6 additions & 0 deletions CatalogoLivros/CatalogoLivros/CatalogoLivros.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@CatalogoLivros_HostAddress = http://localhost:5282

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

###
15 changes: 15 additions & 0 deletions CatalogoLivros/CatalogoLivros/Context/CatalogoLivrosContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using CatalogoLivros.Models;
using Microsoft.EntityFrameworkCore;

namespace CatalogoLivros.Context;

public class CatalogoLivrosContext : DbContext
{
public CatalogoLivrosContext(DbContextOptions<CatalogoLivrosContext> options)
: base(options)
{
}

public DbSet<Genero> Generos { get; set; }
public DbSet<Livro> Livros { get; set; }
}
68 changes: 68 additions & 0 deletions CatalogoLivros/CatalogoLivros/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using CatalogoLivros.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace CatalogoLivros.Controllers
{
[Route("[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
private readonly IConfiguration _configuration;

public AuthController(IConfiguration configuration)
{
_configuration = configuration;
}

[HttpPost("login")]
public IActionResult Login([FromBody] UserLogin userLogin)
{
if (userLogin == null || string.IsNullOrEmpty(userLogin.Username) || string.IsNullOrEmpty(userLogin.Password))
{
return BadRequest("Usuário e senha são necessários.");
}

if (userLogin.Username == "admin" && userLogin.Password == "admin")
{
var token = GenerateJwtToken("Admin");
return Ok(new { Token = token });
}
else if (userLogin.Username == "public" && userLogin.Password == "public")
{
var token = GenerateJwtToken("Public");
return Ok(new { Token = token });
}

return Unauthorized("Usuário ou senha inválidos.");
}

private string GenerateJwtToken(string role)
{
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, role),
new Claim("Role", role)
};

var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(claims),
Expires = DateTime.UtcNow.AddHours(1),
Issuer = _configuration["Jwt:Issuer"],
Audience = _configuration["Jwt:Audience"],
SigningCredentials = creds
};

var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
}
}
169 changes: 169 additions & 0 deletions CatalogoLivros/CatalogoLivros/Controllers/GenerosController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using CatalogoLivros.DTOs;
using CatalogoLivros.Exceptions;
using CatalogoLivros.Models;
using CatalogoLivros.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace CatalogoLivros.Controllers;

[Route("[controller]")]
[ApiController]
public class GenerosController : ControllerBase
{
private readonly IGeneroRepository _repository;
private readonly ILogger<GenerosController> _logger;

public GenerosController(IGeneroRepository repository, ILogger<GenerosController> logger)
{
_repository = repository;
_logger = logger;
}

[HttpGet("livros")]
[Authorize(Policy = "PublicAccess")]
public ActionResult<IEnumerable<GeneroDTO>> GetGenerosLivros()
{
_logger.LogInformation("Iniciando a solicitação GET para obter todos os gêneros com seus livros.");
var generosLivros = _repository.GetGenerosLivros();
_logger.LogInformation("Solicitação GET para obter todos os gêneros com seus livros concluída com sucesso.");
return Ok(generosLivros);
}

[HttpGet]
[Authorize(Policy = "PublicAccess")]
public ActionResult<IEnumerable<GeneroDTO>> Get()
{
_logger.LogInformation("Iniciando a solicitação GET para obter todos os gêneros.");
var generos = _repository.GetGeneros();
if (generos == null)
{
return NotFound("Não existem categorias.");
}

var generosDto = new List<GeneroDTO>();
foreach(var genero in generos)
{
var generoDto = new GeneroDTO()
{
Id = genero.Id,
Nome = genero.Nome,
Livros = genero.Livros
};
generosDto.Add(generoDto);
}

_logger.LogInformation("Solicitação GET para obter todos os gêneros concluída com sucesso.");
return Ok(generosDto);
}

[HttpGet("{id:int}", Name = "ObterGenero")]
[Authorize(Policy = "PublicAccess")]
public ActionResult<GeneroDTO> Get(int id)
{
_logger.LogInformation("Iniciando a solicitação GET para obter o gênero com id={Id}.", id);
var genero = _repository.GetGeneroPorId(id);
if (genero is null)
{
_logger.LogWarning("Gênero com id={Id} não encontrado.", id);
return NotFound($"Gênero com id={id} não encontrado.");
}
var generoDto = new GeneroDTO()
{
Id = genero.Id,
Nome = genero.Nome,
Livros = genero.Livros
};

_logger.LogInformation("Gênero com id={Id} encontrado com sucesso.", id);
return Ok(generoDto);
}

[HttpPost]
[Authorize(Policy = "AdminOnly")]
public ActionResult<GeneroDTO> Post(GeneroDTO generoDto)
{
if (generoDto is null)
{
_logger.LogWarning("Tentativa de adicionar um gênero nulo.");
return BadRequest("O gênero não pode ser nulo.");
}
_logger.LogInformation("Iniciando a solicitação POST para adicionar o gênero '{Nome}'.", generoDto.Nome);

bool cadastroExiste = _repository.JaExisteCadastroGenero(generoDto.Nome);

if(cadastroExiste)
{
_logger.LogWarning("O gênero '{Nome}' já está cadastrado no banco de dados.", generoDto.Nome);
throw new JaExisteCadastroException("Gênero informado já cadastrado!");
}

var genero = new Genero()
{
Id = generoDto.Id,
Nome = generoDto.Nome,
Livros = generoDto.Livros
};

_repository.Create(genero);
_logger.LogInformation("Novo gênero '{Nome}' adicionado com sucesso.", genero.Nome);

return Ok(CreatedAtRoute("ObterGenero", new { id = genero.Id }, genero)); //rever
}

[HttpPut("{id:int}")]
[Authorize(Policy = "AdminOnly")]
public ActionResult<GeneroDTO> Put(int id, GeneroDTO generoDto)
{
if (id != generoDto.Id)
{
_logger.LogWarning("Tentativa de atualizar o gênero com id={Id} com dados inválidos.", id);
return BadRequest("Dados inválidos");
}
_logger.LogInformation("Iniciando a solicitação PUT para atualizar o gênero com id={Id}.", id);

var generoJaExistente = _repository.JaExisteCadastroGenero(generoDto.Nome);

if (generoJaExistente)
{
_logger.LogWarning("O gênero '{Nome}' já está cadastrado no banco de dados.", generoDto.Nome);
throw new JaExisteCadastroException("Gênero informado já cadastrado!");
}

var genero = new Genero()
{
Id = generoDto.Id,
Nome = generoDto.Nome,
Livros = generoDto.Livros
};

var generoAtualizado = _repository.Update(genero);
_logger.LogInformation("Gênero com id={Id} atualizado com sucesso.", id);

var generoAtualizadoDto = new GeneroDTO()
{
Id = generoAtualizado.Id,
Nome = generoAtualizado.Nome,
Livros = generoAtualizado.Livros
};
return Ok(generoAtualizadoDto);
}

[HttpDelete("{id:int}")]
[Authorize(Policy = "AdminOnly")]
public ActionResult<GeneroDTO> Delete(int id)
{
_logger.LogInformation("Iniciando a solicitação DELETE para remover o gênero com id={Id}.", id);

var genero = _repository.Delete(id);
_logger.LogInformation("Gênero com id={Id} removido com sucesso.", id);

var generoExcluido = new GeneroDTO()
{
Id = genero.Id,
Nome = genero.Nome,
Livros = genero.Livros
};
return Ok(generoExcluido);
}
}
Loading