Skip to content

Commit

Permalink
Feature/ef and refactory (#22)
Browse files Browse the repository at this point in the history
* feat: ajute nos testes

* feat: ajuste nos testes

* feat: ef + swagger

* feat: Exclude class from Code Coverage

* feat: Exclude class from Code Coverage
  • Loading branch information
felipementel authored Nov 14, 2023
1 parent 783b21b commit 88e58b2
Show file tree
Hide file tree
Showing 34 changed files with 995 additions and 195 deletions.
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md.
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/DEPLOY.Cachorro.Api/bin/Debug/net7.0/DEPLOY.Cachorro.Api.dll",
"args": [],
"cwd": "${workspaceFolder}/src/DEPLOY.Cachorro.Api",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/src/DEPLOY.Cachorro.Api.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/src/DEPLOY.Cachorro.Api.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/src/DEPLOY.Cachorro.Api.sln"
],
"problemMatcher": "$msCompile"
}
]
}
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ reportgenerator -reports:C:/Proj/DEPLOY/DEPLOY.Cachorro/src/DEPLOY.Cachorro.Api.

---

# EntityFramework Commands

```
dotnet tool install --global dotnet-ef
```

```
dotnet ef migrations add InitDatabaseAPI -s DEPLOY.Cachorro.Api -p DEPLOY.Cachorro.Repository -c DEPLOY.Cachorro.Repository.CachorroDbContext --output-dir Migrations/API -v
```

```
dotnet ef database update InitDatabaseAPI --startup-project DEPLOY.Cachorro.Api --project DEPLOY.Cachorro.Api -context DEPLOY.Cachorro.Repository.CachorroDbContext --verbose
```

Connection String

```
Data Source=127.0.0.1,1433;Initial Catalog=Cachorro;User Id=sa;Password=Abcd1234%;Integrated Security=False;MultipleActiveResultSets=True;TrustServerCertificate=true;
```

<br/>
<br/>
<br/>
Expand Down
48 changes: 48 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# run from src folder
# docker-compose -f ../docker/docker-compose.yml up -d --build
# docker-compose -f ../docker/docker-compose.yml down --remove-orphans

version: "3.8"
services:
# cachorro.api:
# hostname: cachorro-api
# container_name: cachorro-api
# networks:
# - cachorro-net
# restart: always
# depends_on:
# - azuresqledge
# build:
# context: ../src
# dockerfile: ../src/DEPLOY.Cachorro.Api/Dockerfile
# ports:
# - 9042:80
# - 9043:443
# deploy:
# resources:
# limits:
# cpus: "0.25"
# memory: 0.5GB
# reservations:
# cpus: "0.25"
# memory: 500M

azuresqledge:
image: mcr.microsoft.com/azure-sql-edge:2.0.0
hostname: azuresqledge
container_name: azuresqledge
restart: always
cap_add:
- SYS_PTRACE
networks:
- cachorro-net
environment:
ACCEPT_EULA: 1
MSSQL_SA_PASSWORD: Abcd1234%
MSSQL_PID: Developer
ports:
- 1433:1433

networks:
cachorro-net:
driver: bridge
6 changes: 6 additions & 0 deletions src/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sonarlint.connectedMode.project": {
"connectionId": "",
"projectKey": ""
}
}
10 changes: 5 additions & 5 deletions src/DEPLOY.Cachorro.Api.Tests/CachorrosControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ public async Task ObterPorIdAsync_ReturnsNotFound_WhenCachorroNotFound()
var controller = new CachorrosController(context);

// Act
var result = await controller.ObterPorIdAsync(cachorro.Id + 1);
var result = await controller.ObterPorIdAsync(Guid.NewGuid());

// Assert
result.Should().BeOfType<NotFoundResult>();
context.Cachorros.Should().NotContain(c => c.Id == cachorro.Id + 1);
context.Cachorros.Should().NotContain(c => c.Id == Guid.NewGuid());
}
}

Expand Down Expand Up @@ -217,7 +217,7 @@ public async Task PutCachorroAsync_ReturnsBadRequest_WhenCachorroIsInvalid()

// Act
var result = await controller.PutCachorroAsync(
2,
Guid.NewGuid(),
new Domain.Cachorro()
{
Nome = "Sirius v2"
Expand All @@ -233,7 +233,7 @@ public async Task PutCachorroAsync_ReturnsBadRequest_WhenCachorroIsInvalid()
public async Task ExcluirCachorroAsync_ReturnsNotFound_WhenCachorroIdDontExists()
{
// Arrange
var cachorroIdInexistente = -1;
var cachorroIdInexistente = Guid.NewGuid();

var options = new DbContextOptionsBuilder<CachorroDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
Expand Down Expand Up @@ -263,7 +263,7 @@ public async Task ExcluirCachorroAsync_ReturnsNotFound_WhenCachorroIdDontExists(
public async Task ExcluirCachorroAsync_ReturnsNoContent_WhenCachorroIsDeleted()
{
// Arrange
var cachorroId = 1;
var cachorroId = Guid.NewGuid();
var cachorro = new Domain.Cachorro { Id = cachorroId, Nome = "Sirius" };

var options = new DbContextOptionsBuilder<CachorroDbContext>()
Expand Down
10 changes: 6 additions & 4 deletions src/DEPLOY.Cachorro.Api.Tests/DEPLOY.Cachorro.Api.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="FluentAssertions" Version="6.4.0" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">

<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion src/DEPLOY.Cachorro.Api.Tests/TutoresControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public async Task ExcluirTutorAsync_ReturnsNoContent_WhenTutorIsDeleted()

// Assert
result.Should().BeOfType<NoContentResult>();
context.Cachorros.Should().NotContain(c => c.Id == cachorroId);
context.Cachorros.Should().NotContain(c => c.Id == Guid.NewGuid());
}
}
}
Expand Down
34 changes: 28 additions & 6 deletions src/DEPLOY.Cachorro.Api/Controllers/v1/CachorrosController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using DEPLOY.Cachorro.Repository;
using Asp.Versioning;
using DEPLOY.Cachorro.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

using Swashbuckle.AspNetCore.Annotations;

namespace DEPLOY.Cachorro.Api.Controllers.v1
{
[ApiController]
Expand All @@ -14,12 +16,16 @@ public class CachorrosController : ControllerBase
public CachorrosController(CachorroDbContext context)
{
_context = context;
}
}

[HttpGet]
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable<DEPLOY.Cachorro.Domain.Cachorro>), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(
Summary = "List Cachorro",
Description = "Operação para listar de cachorro",
Tags = new[] { "Cachorro", "Get" })]
public async Task<IActionResult> ListarAsync()
{
var items = await _context.Cachorros.ToListAsync();
Expand All @@ -32,7 +38,11 @@ public async Task<IActionResult> ListarAsync()
[ProducesResponseType(typeof(DEPLOY.Cachorro.Domain.Cachorro), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> ObterPorIdAsync(int id)
[SwaggerOperation(
Summary = "Obter Cachorro",
Description = "Operação para obter de cachorro por id",
Tags = new[] { "Cachorro", "Get" })]
public async Task<IActionResult> ObterPorIdAsync(Guid id)
{
var items = await _context.Cachorros.FindAsync(id);

Expand All @@ -49,6 +59,10 @@ public async Task<IActionResult> ObterPorIdAsync(int id)
[Produces("application/json")]
[ProducesResponseType(typeof(DEPLOY.Cachorro.Domain.Cachorro), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(
Summary = "Cadastar Cachorro",
Description = "Operação para cadastrar de cachorro",
Tags = new[] { "Cachorro", "Post" })]
public async Task<IActionResult> CadastrarCachorroAsync(
[FromBody] DEPLOY.Cachorro.Domain.Cachorro cachorro)
{
Expand All @@ -61,8 +75,12 @@ public async Task<IActionResult> CadastrarCachorroAsync(
}

[HttpPut("{id}")]
[SwaggerOperation(
Summary = "Atualizar Cachorro",
Description = "Operação para atualizar de cachorro",
Tags = new[] { "Cachorro", "Put" })]
public async Task<IActionResult> PutCachorroAsync(
long id,
Guid id,
Domain.Cachorro cachorro)
{
if (id != cachorro.Id)
Expand All @@ -82,7 +100,11 @@ public async Task<IActionResult> PutCachorroAsync(
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> ExcluirCachorroAsync(int id)
[SwaggerOperation(
Summary = "Excluir Cachorro",
Description = "Operação para excluir de cachorro",
Tags = new[] { "Cachorro", "Delete" })]
public async Task<IActionResult> ExcluirCachorroAsync(Guid id)
{
var item = await _context.Cachorros.FindAsync(id);

Expand Down
32 changes: 27 additions & 5 deletions src/DEPLOY.Cachorro.Api/Controllers/v1/TutoresController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using DEPLOY.Cachorro.Repository;
using Asp.Versioning;
using DEPLOY.Cachorro.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

using Swashbuckle.AspNetCore.Annotations;

namespace DEPLOY.Cachorro.Api.Controllers.v1
{
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
public class TutoresController : ControllerBase
{
private readonly CachorroDbContext _cachorroDbContext;
Expand All @@ -20,6 +22,10 @@ public TutoresController(CachorroDbContext cachorroDbContext)
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable<Domain.Tutor>), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(
Summary = "Listar Tutor",
Description = "Operação para listar do tutor",
Tags = new[] { "Tutor", "Get" })]
public async Task<IActionResult> ListarAsync()
{
var items = await _cachorroDbContext.Tutores.ToListAsync();
Expand All @@ -32,7 +38,11 @@ public async Task<IActionResult> ListarAsync()
[ProducesResponseType(typeof(Domain.Tutor), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> ObterPorIdAsync(int id)
[SwaggerOperation(
Summary = "Obter Tutor",
Description = "Operação para obter tutor por id",
Tags = new[] { "Tutor", "Get" })]
public async Task<IActionResult> ObterPorIdAsync(long id)
{
var items = await _cachorroDbContext.Tutores.FindAsync(id);

Expand All @@ -49,6 +59,10 @@ public async Task<IActionResult> ObterPorIdAsync(int id)
[Produces("application/json")]
[ProducesResponseType(typeof(Domain.Tutor), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(
Summary = "Cadastrar Tutor",
Description = "Operação para cadastrar do tutor",
Tags = new[] { "Tutor", "Post" })]
public async Task<IActionResult> CadastrarTutorAsync(
[FromBody] Domain.Tutor tutor)
{
Expand All @@ -61,6 +75,10 @@ public async Task<IActionResult> CadastrarTutorAsync(
}

[HttpPut("{id}")]
[SwaggerOperation(
Summary = "Atualizar Tutor",
Description = "Operação para atualizar do tutor",
Tags = new[] { "Tutor", "Put" })]
public async Task<IActionResult> PutTutorAsync(
long id,
Domain.Tutor tutor)
Expand All @@ -82,7 +100,11 @@ public async Task<IActionResult> PutTutorAsync(
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> ExcluirTutorAsync(int id)
[SwaggerOperation(
Summary = "Excluir Tutor",
Description = "Operação para excluir do tutor por id",
Tags = new[] { "Tutor", "Delete" })]
public async Task<IActionResult> ExcluirTutorAsync(long id)
{
var item = await _cachorroDbContext.Tutores.FindAsync(id);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics.CodeAnalysis;

namespace DEPLOY.Cachorro.Api.Controllers.v2
Expand Down
Loading

0 comments on commit 88e58b2

Please sign in to comment.