Skip to content

Commit 1f7b908

Browse files
authored
fix: change predicate to use Dto instead entity (#45)
* fix: change predicate to use Dto instead entity * improvement: extracct implementation to method * fix: correções de comentarios
1 parent 16b7e80 commit 1f7b908

File tree

6 files changed

+76
-10
lines changed

6 files changed

+76
-10
lines changed

.vscode/launch.json

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"args": [],
1515
"cwd": "${workspaceFolder}/src/DEPLOY.Cachorro.Api",
1616
"stopAtEntry": false,
17+
"launchBrowser": {
18+
"enabled": true
19+
},
1720
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
1821
"serverReadyAction": {
1922
"action": "openExternally",

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task<IActionResult> ListAllCachorrosAdotadosAsync(
4949
CancellationToken cancellationToken = default)
5050
{
5151
var items = await _cachorroAppService.GetByKeyAsync(
52-
c => c.Tutor != null,
52+
c => c.Adotado,
5353
cancellationToken);
5454

5555
return items?.Count() > 0 ? Ok(items) : NoContent();
@@ -67,7 +67,7 @@ public async Task<IActionResult> ListAllCachorrosParaAdocaoAsync(
6767
CancellationToken cancellationToken = default)
6868
{
6969
var items = await _cachorroAppService.GetByKeyAsync(
70-
c => c.Tutor == null,
70+
c => !c.Adotado,
7171
cancellationToken);
7272

7373
return items?.Count > 0 ? Ok(items) : NoContent();

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

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DEPLOY.Cachorro.Application.Dtos;
22
using DEPLOY.Cachorro.Application.Interfaces.Services;
3+
using DEPLOY.Cachorro.Application.Shared;
34
using DEPLOY.Cachorro.Domain.Aggregates.Cachorro.Interfaces.Services;
45
using System.Diagnostics.CodeAnalysis;
56
using System.Linq.Expressions;
@@ -44,10 +45,12 @@ public async Task<bool> DeleteAsync(
4445

4546

4647
public async Task<List<CachorroDto>> GetByKeyAsync(
47-
Expression<Func<Domain.Aggregates.Cachorro.Entities.Cachorro, bool>> predicate,
48+
Expression<Func<CachorroDto, bool>> predicate,
4849
CancellationToken cancellationToken = default)
49-
{
50-
var item = await _cachorroService.GetByKeyAsync(predicate, cancellationToken);
50+
{
51+
Expression<Func<Domain.Aggregates.Cachorro.Entities.Cachorro, bool>> domainPredicate = ConvertExpression(predicate);
52+
53+
var item = await _cachorroService.GetByKeyAsync(domainPredicate, cancellationToken);
5154

5255
return item.Select(x => (CachorroDto)x!).ToList();
5356
}
@@ -71,5 +74,18 @@ public async Task<IEnumerable<string>> UpdateAsync(
7174
cachorroDto,
7275
cancellationToken);
7376
}
77+
78+
private static Expression<Func<Domain.Aggregates.Cachorro.Entities.Cachorro, bool>> ConvertExpression(Expression<Func<CachorroDto, bool>> predicate)
79+
{
80+
ParameterExpression parameter = Expression.Parameter(typeof(Domain.Aggregates.Cachorro.Entities.Cachorro), "cachorro");
81+
82+
ExpressionConverter body = new(parameter);
83+
84+
Expression<Func<Domain.Aggregates.Cachorro.Entities.Cachorro, bool>> domainPredicate = Expression.Lambda<Func<Domain.Aggregates.Cachorro.Entities.Cachorro, bool>>(
85+
body.Visit(predicate.Body),
86+
parameter
87+
);
88+
return domainPredicate;
89+
}
7490
}
7591
}

src/DEPLOY.Cachorro.Application/Interfaces/Services/ICachorroAppServices.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public Task<bool> DeleteAsync(
2626
CancellationToken cancellationToken = default);
2727

2828
Task<List<CachorroDto>> GetByKeyAsync(
29-
Expression<Func<Domain.Aggregates.Cachorro.Entities.Cachorro, bool>> predicate,
29+
Expression<Func<CachorroDto, bool>> predicate,
3030
CancellationToken cancellationToken = default);
3131

3232
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Linq.Expressions;
2+
using DEPLOY.Cachorro.Application.Dtos;
3+
4+
namespace DEPLOY.Cachorro.Application.Shared
5+
{
6+
public class ExpressionConverter : ExpressionVisitor
7+
{
8+
private readonly ParameterExpression _parameter;
9+
10+
public ExpressionConverter(ParameterExpression parameter)
11+
{
12+
_parameter = parameter;
13+
}
14+
15+
protected override Expression VisitParameter(ParameterExpression node)
16+
{
17+
return _parameter;
18+
}
19+
20+
protected override Expression VisitMember(MemberExpression node)
21+
{
22+
if(node.Member.DeclaringType == typeof(CachorroDto))
23+
{
24+
return Expression.Property(Visit(node.Expression), node.Member.Name);
25+
}
26+
27+
return base.VisitMember(node);
28+
}
29+
30+
protected override Expression VisitBinary(BinaryExpression node)
31+
{
32+
if(node.NodeType == ExpressionType.Equal || node.NodeType == ExpressionType.NotEqual)
33+
{
34+
if(node.Left.NodeType == ExpressionType.Constant && ((ConstantExpression)node.Left).Value == null)
35+
{
36+
return Expression.MakeBinary(node.NodeType, Visit(node.Right), node.Left);
37+
}
38+
else if (node.Right.NodeType == ExpressionType.Constant && ((ConstantExpression)node.Right).Value == null)
39+
{
40+
return Expression.MakeBinary(node.NodeType, Visit(node.Left), node.Right);
41+
}
42+
}
43+
44+
return base.VisitBinary(node);
45+
}
46+
}
47+
}

src/Tests/DEPLOY.Cachorro.Api.Tests/CachorrosControllerTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public async Task GivenListAllCachorrosAdotadosAsync_WhenExistsCachorrosAdotados
332332
var cachorrosAdotados = _cachorroDtoFixture.CreateManyCachorroDtoWithTutorDto(40);
333333

334334
_cachorroAppServiceMock.Setup(repo => repo.GetByKeyAsync(
335-
It.IsAny<Expression<Func<Domain.Aggregates.Cachorro.Entities.Cachorro, bool>>>(),
335+
It.IsAny<Expression<Func<CachorroDto, bool>>>(),
336336
It.IsAny<CancellationToken>()))
337337
.ReturnsAsync(cachorrosAdotados);
338338

@@ -357,7 +357,7 @@ public async Task GivenListAllCachorrosAdotadosAsync_WhenDontExistsCachorrosAdot
357357
{
358358
// Arrange
359359
_cachorroAppServiceMock.Setup(repo => repo.GetByKeyAsync(
360-
It.IsAny<Expression<Func<Domain.Aggregates.Cachorro.Entities.Cachorro, bool>>>(),
360+
It.IsAny<Expression<Func<CachorroDto, bool>>>(),
361361
It.IsAny<CancellationToken>()))
362362
.ReturnsAsync(new List<CachorroDto>());
363363

@@ -382,7 +382,7 @@ public async Task GivenListAllCachorrosParaAdocaoAsync_WhenCachorrosExistis_Than
382382
var cachorrosParaAdocao = _cachorroDtoFixture.CreateManyCachorroDtoWithoutTutorDto(40);
383383

384384
_cachorroAppServiceMock.Setup(repo => repo.GetByKeyAsync(
385-
It.IsAny<Expression<Func<Domain.Aggregates.Cachorro.Entities.Cachorro, bool>>>(),
385+
It.IsAny<Expression<Func<CachorroDto, bool>>>(),
386386
It.IsAny<CancellationToken>()))
387387
.ReturnsAsync(cachorrosParaAdocao);
388388

@@ -404,7 +404,7 @@ public async Task GivenListAllCachorrosParaAdocaoAsync_WhenCachorrosDontExistis_
404404
{
405405
// Arrange
406406
_cachorroAppServiceMock.Setup(repo => repo.GetByKeyAsync(
407-
It.IsAny<Expression<Func<Domain.Aggregates.Cachorro.Entities.Cachorro, bool>>>(),
407+
It.IsAny<Expression<Func<CachorroDto, bool>>>(),
408408
It.IsAny<CancellationToken>()))
409409
.ReturnsAsync(new List<CachorroDto>());
410410

0 commit comments

Comments
 (0)