Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jicking committed Apr 19, 2024
1 parent c47af51 commit 6ba2b68
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
3 changes: 1 addition & 2 deletions JixMinApi/Features/Todo/Queries/GetTodoByIdQuery.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using FluentValidation;
using FluentValidation.Results;
using JixMinApi.Features.Todo.Commands;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace JixMinApi.Features.Todo.Queries;

public record GetTodoByIdQuery(Guid Id) :IRequest<Result<TodoDto>>;
public record GetTodoByIdQuery(Guid Id) : IRequest<Result<TodoDto>>;

public sealed class GetTodoByIdQueryValidator
: AbstractValidator<GetTodoByIdQuery>
Expand Down
13 changes: 9 additions & 4 deletions JixMinApi/Features/Todo/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@

public class Result<T>
{
public bool IsSuccess => !Errors.Any();
public bool IsSuccess => !Errors.Any() && !IsNotFound;
public T? Value { get; init; }
public IReadOnlyList<KeyValuePair<string, string>> Errors { get; init; } = [];
public bool IsNotFound { get; set; }

public Result(T value)
{
if (value is null)
{
IsNotFound = true;
}

Value = value;
}

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

// TODO: using NotFound still Result.IsSuccess to true, will lead to bugs
public static Result<T> NotFound<T>()
public static Result<T> NotFound()
{
T val = default(T);
T val = default;

Check warning on line 33 in JixMinApi/Features/Todo/Result.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 33 in JixMinApi/Features/Todo/Result.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
return new Result<T>(val);

Check warning on line 34 in JixMinApi/Features/Todo/Result.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'Result<T>.Result(T value)'.

Check warning on line 34 in JixMinApi/Features/Todo/Result.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'Result<T>.Result(T value)'.
}
}
Expand Down
12 changes: 5 additions & 7 deletions JixMinApi/Features/Todo/TodoEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ public static async Task<Results<ValidationProblem, NotFound, Ok<TodoDto>>> GetT

if (!result.IsSuccess)
{
return TypedResults.ValidationProblem(result.Errors.ToErrorDictionary());
}

if (result.Value is null)
{
return TypedResults.NotFound();
if (result.IsNotFound)
return TypedResults.NotFound();
else
return TypedResults.ValidationProblem(result.Errors.ToErrorDictionary());
}

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

var todo = result.Value;
return TypedResults.Created($"{Constants.TodoApiRootPath}/{todo.Id}", todo);
return TypedResults.Created($"{Constants.TodoApiRootPath}/{todo!.Id}", todo);
}
}
1 change: 0 additions & 1 deletion JixMinApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using FluentValidation;
using JixMinApi.Features.Todo;
using JixMinApi.Shared;
using Microsoft.AspNetCore.Identity;
using Microsoft.OpenApi.Models;
using System.Reflection;

Expand Down
3 changes: 1 addition & 2 deletions JixMinApiTests/Features/Todo/TodoEndpointsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Xunit;
using Assert = Xunit.Assert;
Expand Down Expand Up @@ -46,7 +45,7 @@ public async Task GetTodoByIdAsync_Returns_NotFound_When_Todo_Not_Found()
var mediatorMock = new Mock<IMediator>();
var nonExistentId = Guid.NewGuid(); // Assuming this id doesn't exist
mediatorMock.Setup(m => m.Send(It.IsAny<GetTodoByIdQuery>(), CancellationToken.None))
.ReturnsAsync(Result<TodoDto>.NotFound<TodoDto>());
.ReturnsAsync(Result<TodoDto>.NotFound());

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

0 comments on commit 6ba2b68

Please sign in to comment.