Skip to content

Commit 6ba2b68

Browse files
committed
cleanup
1 parent c47af51 commit 6ba2b68

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

JixMinApi/Features/Todo/Queries/GetTodoByIdQuery.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using FluentValidation;
22
using FluentValidation.Results;
3-
using JixMinApi.Features.Todo.Commands;
43
using MediatR;
54
using Microsoft.EntityFrameworkCore;
65

76
namespace JixMinApi.Features.Todo.Queries;
87

9-
public record GetTodoByIdQuery(Guid Id) :IRequest<Result<TodoDto>>;
8+
public record GetTodoByIdQuery(Guid Id) : IRequest<Result<TodoDto>>;
109

1110
public sealed class GetTodoByIdQueryValidator
1211
: AbstractValidator<GetTodoByIdQuery>

JixMinApi/Features/Todo/Result.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33

44
public class Result<T>
55
{
6-
public bool IsSuccess => !Errors.Any();
6+
public bool IsSuccess => !Errors.Any() && !IsNotFound;
77
public T? Value { get; init; }
88
public IReadOnlyList<KeyValuePair<string, string>> Errors { get; init; } = [];
9+
public bool IsNotFound { get; set; }
910

1011
public Result(T value)
1112
{
13+
if (value is null)
14+
{
15+
IsNotFound = true;
16+
}
17+
1218
Value = value;
1319
}
1420

@@ -22,10 +28,9 @@ public Result(string field, string validationErrorMessage)
2228
Errors = [new(field, validationErrorMessage)];
2329
}
2430

25-
// TODO: using NotFound still Result.IsSuccess to true, will lead to bugs
26-
public static Result<T> NotFound<T>()
31+
public static Result<T> NotFound()
2732
{
28-
T val = default(T);
33+
T val = default;
2934
return new Result<T>(val);
3035
}
3136
}

JixMinApi/Features/Todo/TodoEndpoints.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,10 @@ public static async Task<Results<ValidationProblem, NotFound, Ok<TodoDto>>> GetT
6767

6868
if (!result.IsSuccess)
6969
{
70-
return TypedResults.ValidationProblem(result.Errors.ToErrorDictionary());
71-
}
72-
73-
if (result.Value is null)
74-
{
75-
return TypedResults.NotFound();
70+
if (result.IsNotFound)
71+
return TypedResults.NotFound();
72+
else
73+
return TypedResults.ValidationProblem(result.Errors.ToErrorDictionary());
7674
}
7775

7876
return TypedResults.Ok(result.Value);
@@ -103,6 +101,6 @@ public static async Task<Results<Created<TodoDto>, ValidationProblem>> CreateTod
103101
}
104102

105103
var todo = result.Value;
106-
return TypedResults.Created($"{Constants.TodoApiRootPath}/{todo.Id}", todo);
104+
return TypedResults.Created($"{Constants.TodoApiRootPath}/{todo!.Id}", todo);
107105
}
108106
}

JixMinApi/Program.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using FluentValidation;
22
using JixMinApi.Features.Todo;
33
using JixMinApi.Shared;
4-
using Microsoft.AspNetCore.Identity;
54
using Microsoft.OpenApi.Models;
65
using System.Reflection;
76

JixMinApiTests/Features/Todo/TodoEndpointsTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using MediatR;
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.AspNetCore.Http.HttpResults;
5-
using Microsoft.AspNetCore.Mvc;
65
using Moq;
76
using Xunit;
87
using Assert = Xunit.Assert;
@@ -46,7 +45,7 @@ public async Task GetTodoByIdAsync_Returns_NotFound_When_Todo_Not_Found()
4645
var mediatorMock = new Mock<IMediator>();
4746
var nonExistentId = Guid.NewGuid(); // Assuming this id doesn't exist
4847
mediatorMock.Setup(m => m.Send(It.IsAny<GetTodoByIdQuery>(), CancellationToken.None))
49-
.ReturnsAsync(Result<TodoDto>.NotFound<TodoDto>());
48+
.ReturnsAsync(Result<TodoDto>.NotFound());
5049

5150
// Act
5251
var response = await TodoEndpoints.GetTodoByIdAsync(nonExistentId, mediatorMock.Object);

0 commit comments

Comments
 (0)