Skip to content

Commit daafd6f

Browse files
committed
feat: corrigindo cancelattion token, testes de unidade e alguns outros ajustes
1 parent 58faec6 commit daafd6f

File tree

18 files changed

+179
-119
lines changed

18 files changed

+179
-119
lines changed

src/DEPLOY.Cachorro.Api/Controllers/v1/CachorrosController.cs

+23-9
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ public async Task<IActionResult> GetByIdAsync(
4949
Guid id,
5050
CancellationToken cancellationToken = default)
5151
{
52-
var items = await _cachorroAppService.GetByIdAsync(id, cancellationToken);
52+
var items = await _cachorroAppService.GetByIdAsync(
53+
id,
54+
cancellationToken);
55+
5356
if (items == null)
5457
{
5558
return NotFound();
@@ -68,16 +71,22 @@ public async Task<IActionResult> GetByIdAsync(
6871
Tags = new[] { "Cachorros" },
6972
Description = "Operação para cadastrar cachorro")]
7073
public async Task<IActionResult> CreateAsync(
71-
[FromBody] CachorroCreateDto cachorroCreateDto,
74+
[FromBody] CachorroCreateDto cachorroCreateDto,
7275
CancellationToken cancellationToken = default)
7376
{
74-
var item = await _cachorroAppService.InsertAsync(cachorroCreateDto, cancellationToken);
77+
var item = await _cachorroAppService.InsertAsync(
78+
cachorroCreateDto,
79+
cancellationToken);
7580

7681
if (item.Erros.Any())
77-
return BadRequest(item.Erros);
82+
return UnprocessableEntity(item.Erros);
7883

7984
return CreatedAtAction("GetById",
80-
new { id = item.Id, version = new ApiVersion(1, 0).ToString() },
85+
new { id = item.Id,
86+
version = new ApiVersion(
87+
1,
88+
0)
89+
.ToString() },
8190
item);
8291
}
8392

@@ -88,15 +97,18 @@ public async Task<IActionResult> CreateAsync(
8897
Description = "Operação para atualizar de cachorro")]
8998
public async Task<IActionResult> UpdateAsync(
9099
Guid id,
91-
CachorroDto cachorroDto,
100+
[FromBody] CachorroDto cachorroDto,
92101
CancellationToken cancellationToken = default)
93102
{
94103
if (id != cachorroDto.Id)
95104
{
96-
return BadRequest();
105+
return UnprocessableEntity();
97106
}
98107

99-
return await _cachorroAppService.UpdateAsync(id, cachorroDto, cancellationToken)
108+
return await _cachorroAppService.UpdateAsync(
109+
id,
110+
cachorroDto,
111+
cancellationToken)
100112
? NoContent()
101113
: NotFound();
102114
}
@@ -114,7 +126,9 @@ public async Task<IActionResult> DeleteAsync(
114126
Guid id,
115127
CancellationToken cancellationToken = default)
116128
{
117-
var item = await _cachorroAppService.DeleteAsync(id, cancellationToken);
129+
var item = await _cachorroAppService.DeleteAsync(
130+
id,
131+
cancellationToken);
118132

119133
if (!item)
120134
{

src/DEPLOY.Cachorro.Api/Controllers/v1/TutoresController.cs

+21-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public TutoresController(ITutorAppServices tutorAppServices)
3131
public async Task<IActionResult> ListAllAsync(
3232
CancellationToken cancellationToken = default)
3333
{
34-
var items = await _tutorAppServices.GetAllAsync();
34+
var items = await _tutorAppServices.GetAllAsync(cancellationToken);
3535

3636
return Ok(items);
3737
}
@@ -49,7 +49,7 @@ public async Task<IActionResult> GetByIdAsync(
4949
long id,
5050
CancellationToken cancellationToken = default)
5151
{
52-
var items = await _tutorAppServices.GetByIdAsync(id);
52+
var items = await _tutorAppServices.GetByIdAsync(id, cancellationToken);
5353

5454
if (items == null)
5555
{
@@ -72,13 +72,20 @@ public async Task<IActionResult> CreateAsync(
7272
[FromBody] TutorDto tutorDto,
7373
CancellationToken cancellationToken = default)
7474
{
75-
var item = await _tutorAppServices.InsertAsync(tutorDto);
75+
var item = await _tutorAppServices.InsertAsync(
76+
tutorDto,
77+
cancellationToken);
7678

7779
if (item.Erros.Any())
78-
return BadRequest(item.Erros);
80+
return UnprocessableEntity(item.Erros);
7981

8082
return CreatedAtAction("GetById",
81-
new { id = item.Id, version = new ApiVersion(1, 0).ToString() },
83+
new {
84+
id = item.Id,
85+
version = new ApiVersion(
86+
1,
87+
0)
88+
.ToString() },
8289
item);
8390
}
8491

@@ -89,14 +96,19 @@ public async Task<IActionResult> CreateAsync(
8996
Description = "Operação para atualizar tutor")]
9097
public async Task<IActionResult> UpdateAsync(
9198
long id,
92-
TutorDto tutorDto,
99+
[FromBody] TutorDto tutorDto,
93100
CancellationToken cancellationToken = default)
94101
{
95102
if (id != tutorDto.Id)
96103
{
97-
return BadRequest();
104+
return UnprocessableEntity();
98105
}
99-
var retorned = await _tutorAppServices.UpdateAsync(id, tutorDto);
106+
107+
var retorned = await _tutorAppServices.UpdateAsync(
108+
id,
109+
tutorDto,
110+
cancellationToken);
111+
100112
return retorned ? NoContent()
101113
: NotFound();
102114
}
@@ -114,7 +126,7 @@ public async Task<IActionResult> DeleteAsync(
114126
long id,
115127
CancellationToken cancellationToken = default)
116128
{
117-
var item = await _tutorAppServices.DeleteAsync(id);
129+
var item = await _tutorAppServices.DeleteAsync(id, cancellationToken);
118130

119131
if (!item)
120132
{

src/DEPLOY.Cachorro.Api/DEPLOY.Cachorro.Api.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
1212
<ApplicationInsightsResourceId>/subscriptions/f4574f09-7fab-45bd-8eb5-508b8c6aa04a/resourceGroups/rg-canaldeploy-dev/providers/microsoft.insights/components/appi-cachorroapi-dev</ApplicationInsightsResourceId>
13-
<Version>2.1.1</Version>
13+
<Version>2.1.2</Version>
1414
</PropertyGroup>
1515

1616
<ItemGroup>

src/DEPLOY.Cachorro.Api/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void Main(string[] args)
2626
opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
2727
opt.JsonSerializerOptions.WriteIndented = true;
2828
opt.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
29-
29+
opt.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
3030
});
3131

3232
builder.Services.AddRouting(opt =>

src/DEPLOY.Cachorro.Application/AppServices/AdocaoAppService.cs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using DEPLOY.Cachorro.Application.Interfaces.Services;
22
using DEPLOY.Cachorro.Domain.Aggregates.Adotar.Interfaces.Service;
3+
using System.Diagnostics.CodeAnalysis;
34

45
namespace DEPLOY.Cachorro.Application.AppServices
56
{
7+
[ExcludeFromCodeCoverage]
68
public class AdocaoAppService : IAdocaoAppService
79
{
810
private readonly IAdocaoService _adocaoService;
@@ -13,12 +15,21 @@ public AdocaoAppService(IAdocaoService adocaoService)
1315
}
1416

1517
//https://github.com/altmann/FluentResults
16-
public async Task AdotarAsync(Guid cachorroId, long tutorId, CancellationToken cancellationToken = default)
18+
public async Task AdotarAsync(
19+
Guid cachorroId,
20+
long tutorId,
21+
CancellationToken cancellationToken = default)
1722
{
18-
await _adocaoService.AdotarAsync(cachorroId, tutorId, cancellationToken);
23+
await _adocaoService.AdotarAsync(
24+
cachorroId,
25+
tutorId,
26+
cancellationToken);
1927
}
2028

21-
public Task DevolverAdocaoAsync(Guid cachorroId, long TutorId, CancellationToken cancellationToken = default)
29+
public Task DevolverAdocaoAsync(
30+
Guid cachorroId,
31+
long TutorId,
32+
CancellationToken cancellationToken = default)
2233
{
2334
throw new NotImplementedException();
2435
}

src/DEPLOY.Cachorro.Application/AppServices/CachorroAppServices.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using DEPLOY.Cachorro.Application.Dtos;
22
using DEPLOY.Cachorro.Application.Interfaces.Services;
33
using DEPLOY.Cachorro.Domain.Aggregates.Cachorro.Interfaces.Services;
4+
using System.Diagnostics.CodeAnalysis;
45

56
namespace DEPLOY.Cachorro.Application.AppServices
67
{
8+
[ExcludeFromCodeCoverage]
79
public class CachorroAppServices : ICachorroAppServices
810
{
911
private readonly ICachorroService _cachorroService;

src/DEPLOY.Cachorro.Application/DEPLOY.Cachorro.Application.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
11+
</ItemGroup>
12+
913
<ItemGroup>
1014
<ProjectReference Include="..\DEPLOY.Cachorro.Domain\DEPLOY.Cachorro.Domain.csproj" />
1115
</ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Swashbuckle.AspNetCore.Annotations;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Text.Json.Serialization;
4+
5+
namespace DEPLOY.Cachorro.Application.Dtos
6+
{
7+
[ExcludeFromCodeCoverage]
8+
public record BaseDto
9+
{
10+
public BaseDto(IEnumerable<string> erros)
11+
{
12+
Erros = erros;
13+
}
14+
15+
[JsonIgnore]
16+
public IEnumerable<string> Erros { get; set; }
17+
}
18+
}

src/DEPLOY.Cachorro.Application/Dtos/CachorroDto.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using DEPLOY.Cachorro.Domain.Aggregates.Cachorro.ValueObject;
2-
using DEPLOY.Cachorro.Domain.Aggregates.Tutor.Entities;
2+
using Swashbuckle.AspNetCore.Annotations;
33
using System.Diagnostics.CodeAnalysis;
44

55
namespace DEPLOY.Cachorro.Application.Dtos
@@ -15,7 +15,8 @@ public record CachorroDto(
1515
PELAGEM Pelagem,
1616
float Peso,
1717
TutorDto? Tutor,
18-
IEnumerable<string> Erros)
18+
[SwaggerSchema(ReadOnly = true)]
19+
IEnumerable<string> Erros = null) : BaseDto(Erros)
1920
{
2021
public static implicit operator Domain.Aggregates.Cachorro.Entities.Cachorro(CachorroDto dto) =>
2122
new Domain.Aggregates.Cachorro.Entities.Cachorro(

src/DEPLOY.Cachorro.Application/Dtos/TutorDto.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
using DEPLOY.Cachorro.Domain.Aggregates.Tutor.Entities;
2+
using Swashbuckle.AspNetCore.Annotations;
3+
using System.Diagnostics.CodeAnalysis;
24

35
namespace DEPLOY.Cachorro.Application.Dtos
46
{
7+
[ExcludeFromCodeCoverage]
58
public record TutorDto(
69
long Id,
710
string Nome,
811
DateTime Cadastro,
912
DateTime? Atualizacao,
1013
long CPF,
11-
IEnumerable<String> Erros = null)
14+
[SwaggerSchema(ReadOnly = true)]
15+
IEnumerable<string> Erros = null) : BaseDto(Erros)
1216
{
1317
public static implicit operator Tutor(TutorDto dto) =>
14-
new Domain.Aggregates.Tutor.Entities.Tutor (
18+
new Tutor(
1519
dto.Id,
1620
dto.Nome,
1721
dto.Cadastro,

src/DEPLOY.Cachorro.Domain/Aggregates/Adotar/Services/AdocaoService.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ public async Task AdotarAsync(
2828
CancellationToken cancellationToken = default)
2929
{
3030
var existedCachorro = await _cachorroRepository.GetByIdAsync(cachorroId, cancellationToken);
31+
// capturar erro de cachorro não encontrado
3132

3233
var existedTutor = await _tutorRepository.GetByIdAsync(tutorId, cancellationToken);
34+
// capturar erro de tutor não encontrado
35+
36+
//retornar validacao
3337

3438
existedCachorro.Adotar(existedTutor);
3539

3640
await _cachorroRepository.UpdateAsync(existedCachorro);
3741

38-
await _uow.CommitAndSaveChangesAsync();
42+
await _uow.CommitAndSaveChangesAsync(cancellationToken);
3943
}
4044

4145
public Task DevolverAdocaoAsync(

src/DEPLOY.Cachorro.Domain/Aggregates/Cachorro/Validations/CachorroValidator.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using FluentValidation;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace DEPLOY.Cachorro.Domain.Aggregates.Cachorro.Validations
45
{
6+
[ExcludeFromCodeCoverage]
57
public class CachorroValidator : AbstractValidator<Entities.Cachorro>
68
{
79
public CachorroValidator()
@@ -32,9 +34,9 @@ public CachorroValidator()
3234
.GreaterThan(0)
3335
.WithMessage("Peso deve ser maior que 0");
3436

35-
RuleFor(x => x.Nascimento)
36-
.GreaterThan(System.DateTime.Now)
37-
.WithMessage("Data de nascimento deve ser menor que a data atual");
37+
RuleFor(x => x.Nascimento.Date)
38+
.LessThanOrEqualTo(DateTime.Now.Date)
39+
.WithMessage("Data de nascimento deve ser menor ou igual a data atual");
3840
});
3941

4042
RuleSet("Update", () =>

src/DEPLOY.Cachorro.Domain/Aggregates/Tutor/Validations/TutorValidator.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using FluentValidation;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace DEPLOY.Cachorro.Domain.Aggregates.Tutor.Validations
45
{
6+
[ExcludeFromCodeCoverage]
57
public class TutorValidator : AbstractValidator<Entities.Tutor>
68
{
79
public TutorValidator()

src/DEPLOY.Cachorro.Domain/Shared/GenericService.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ protected GenericService(
2525
_genericRepository = genericRepository;
2626
}
2727

28-
public virtual async Task<TEntity> CreateAsync(TEntity entity,
28+
public virtual async Task<TEntity> CreateAsync(
29+
TEntity entity,
2930
CancellationToken cancellationToken = default)
3031
{
3132
var validated = await _validator.ValidateAsync(entity,
@@ -39,7 +40,7 @@ public virtual async Task<TEntity> CreateAsync(TEntity entity,
3940

4041
await _genericRepository.InsertAsync(entity);
4142

42-
await _uow.CommitAndSaveChangesAsync();
43+
await _uow.CommitAndSaveChangesAsync(cancellationToken);
4344

4445
return entity;
4546
}
@@ -53,11 +54,11 @@ public virtual async Task<bool> DeleteAsync(
5354
if (!item)
5455
return false;
5556

56-
return await _uow.CommitAndSaveChangesAsync();
57+
return await _uow.CommitAndSaveChangesAsync(cancellationToken);
5758
}
5859

59-
public virtual async Task<bool> UpdateAsync
60-
(Tid id,
60+
public virtual async Task<bool> UpdateAsync(
61+
Tid id,
6162
TEntity entity,
6263
CancellationToken cancellationToken = default)
6364
{
@@ -77,7 +78,7 @@ public virtual async Task<bool> UpdateAsync
7778

7879
await _genericRepository.UpdateAsync(entity);
7980

80-
return await _uow.CommitAndSaveChangesAsync();
81+
return await _uow.CommitAndSaveChangesAsync(cancellationToken);
8182
}
8283

8384
public virtual async Task<IEnumerable<TEntity>> GetAllAsync(
@@ -88,7 +89,8 @@ public virtual async Task<IEnumerable<TEntity>> GetAllAsync(
8889
return item.Any() ? item : Enumerable.Empty<TEntity>();
8990
}
9091

91-
public virtual async Task<TEntity?> GetByIdAsync(Tid id,
92+
public virtual async Task<TEntity?> GetByIdAsync(
93+
Tid id,
9294
CancellationToken cancellationToken = default)
9395
{
9496
return await _genericRepository.GetByIdAsync(id);

0 commit comments

Comments
 (0)