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
456 changes: 456 additions & 0 deletions BookManager/.gitignore

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions BookManager/BookManager.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql" Version="7.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.5" />
</ItemGroup>

</Project>
77 changes: 77 additions & 0 deletions BookManager/Controllers/AuthContoller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using BookManager.Model;
using BookManager.Repository.Interfaces;
using BookManager.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace BookManager.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{

private readonly IUserRepository repository;

public AuthController(IUserRepository pRepository)
{
repository = pRepository;
}

[AllowAnonymous]
[HttpPost("login")]
public async Task<IActionResult> login(Credential param)
{
if (param == null ||
string.IsNullOrWhiteSpace(param.email) ||
string.IsNullOrWhiteSpace(param.password))
{
//Logger.LogInformation("Dados Inválidos");
return BadRequest("Dados Inválidos");
}


var user = await repository.login(param);



if (user != null && !string.IsNullOrWhiteSpace(user.name))
{
Token token = new TokenService().GenerateToken(user);
return Ok(token);
}
else
{

return NotFound("User not found.");
}

}


[AllowAnonymous]
[HttpPost("signup")]
public async Task<IActionResult> signup(User user)
{

if (user == null ||
string.IsNullOrWhiteSpace(user.name) ||
string.IsNullOrWhiteSpace(user.email) ||
string.IsNullOrWhiteSpace(user.password) ||
string.IsNullOrWhiteSpace(user.role))
return BadRequest("Dados Inválidos");
if (await repository.checkUserExists(user))
return BadRequest("User exists.");

repository.Add(user);

return await repository.SaveChangesAsync()
? Ok("User adicionado com sucesso")
: BadRequest("Erro ao salvar o Usuario");

}


}
}
269 changes: 269 additions & 0 deletions BookManager/Controllers/BooksController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
using BookManager.Model;
using BookManager.Repository.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;

namespace BookManager.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class BooksController : ControllerBase
{

private readonly IBookRepository repository;
private readonly ICostomLogRepository costomLogRepository;

public BooksController(IBookRepository pRepository, ICostomLogRepository pCostomLogRepository)
{
repository = pRepository;
costomLogRepository = pCostomLogRepository;
}
[AllowAnonymous]
[HttpGet]
public async Task<IActionResult> Get()
{

try
{
var books = await repository.GetBooksAsync();
if (books != null && books.Any())
{
return Ok(books);
}
else
{

return NotFound("Books not exiists");
}
}
catch (Exception ex)
{
logger("Get", ex.ToString());
return BadRequest(ex.Message);
}
}


[AllowAnonymous]
[HttpGet("GetFilter/skip/{skip:int}/take/{take:int}/name/{name}")]
public async Task<IActionResult> GetFilter(
[FromRoute] int skip = 0,
[FromRoute] int take = 50,
[FromRoute] string name = "")
{
try
{
var books = await repository.GetFilter(skip, take, name);

if (books != null && books.Any())
{
int pCount = repository.getCountBook(name);
return Ok(new
{
skip = skip,
take = take,
count = pCount,
data = books
});
}
else
{
return NotFound("Books not exists");
}
}
catch (Exception ex)
{
logger("GetFilter", ex.ToString());
return BadRequest(ex.Message);
}
}


[AllowAnonymous]
[HttpGet("id/{id:int}")]
public async Task<IActionResult> GetById(int id)
{
try
{
if (id <= 0)
{
return NotFound("Id Not Found");
}


var book = await repository.GetBooksByIdAsync(id);
return book != null
? Ok(book)
: NotFound("Book not found");
}
catch (Exception ex)
{
logger("GetById", ex.ToString());
return BadRequest(ex.Message);
}
}


[Authorize]
[HttpPut]
public async Task<IActionResult> put(Book book)
{

try
{
if (book == null) return BadRequest("Dados Inválidos");

var bookDB = await repository.GetBooksByIdAsync(book.id);
if (string.IsNullOrWhiteSpace(book.decription))
{
bookDB.decription = book.decription;
}
if (string.IsNullOrWhiteSpace(book.author))
{
bookDB.author = book.author;
}
if (string.IsNullOrWhiteSpace(book.name))
{
bookDB.name = book.name;
}
if (string.IsNullOrWhiteSpace(book.isnb))
{
bookDB.isnb = book.isnb;
}
repository.Update(bookDB);

return await repository.SaveChangesAsync()
? Ok("Book Alterado com sucesso")
: BadRequest("Erro ao salvar book");
}
catch (Exception ex)
{
logger("Put", ex.ToString());
return BadRequest(ex.Message);
}

}

[Authorize]
[HttpDelete("id/{id:int}")]
public async Task<IActionResult> delete(int id)
{
try
{
var book = await repository.GetBooksByIdAsync(id);
repository.Delete(book);

return await repository.SaveChangesAsync()
? Ok("Book excluido com sucesso.")
: BadRequest("Erro ao excluir o book.");
}
catch (Exception ex)
{
logger("delete", ex.ToString());
return BadRequest(ex.Message);
}
}

[Authorize]
[HttpPost]
public async Task<IActionResult> Post(Book book)
{
try
{
if (book == null ||
string.IsNullOrWhiteSpace(book.name) ||
string.IsNullOrWhiteSpace(book.decription) ||
string.IsNullOrWhiteSpace(book.isnb) ||
string.IsNullOrWhiteSpace(book.author) ||
book.idCategory <= 0 ||
book.idPublisher <= 0 ||
book.year <= 0
)
{
return BadRequest("Dados Inválidos");
}

var checkExists = await repository.bookCheckExists(book);
if (checkExists)
{
return BadRequest("Book exists.");
}
repository.Add(book);

return await repository.SaveChangesAsync()
? Ok("Book adicionado com sucesso")
: BadRequest("Erro ao salvar o book");
}
catch (Exception ex)
{
logger("Post", ex.ToString());
return BadRequest(ex.Message);
}


}

[Authorize]
[HttpGet("load/qtd/{qtd:int}")]
public async Task<IActionResult> load(int qtd = 100)
{
try
{
bool controle = false;
if (qtd > 1000)
return BadRequest("Maximo 1000");

int max = await repository.getMaxIdBook();
for (int i = max + 1; i < (qtd + max + 1); i++)
{
var book = new Book
{

name = $"Nome Book {i}",
decription = $"Description book {i}",
author = "author",
idCategory = 1,
isnb = $"9999{i}",
year = 2004,
idPublisher = 1,
exemplary = i,
createAt = DateTime.Now

};
repository.Add(book);

}
controle = await repository.SaveChangesAsync();
return controle
? Ok("Load com sucesso")
: BadRequest("Erro");
}
catch (Exception ex)
{
logger("Get", ex.ToString());
return BadRequest(ex.Message);
}

}

private async void logger(string pOperation, string pTrace)
{
try
{
var log = new CustomLog()
{
operation = String.Concat("bookController", pOperation).ToLower(),
trace = pTrace,
createAt = DateTime.Now
};
costomLogRepository.Add(log);
await costomLogRepository.SaveChangesAsync();
}
catch
{
//não sobe, grava no event viwer
}
}
}
}
Loading