-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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 sealed class GetTodoByIdQueryValidator | ||
: AbstractValidator<GetTodoByIdQuery> | ||
{ | ||
public GetTodoByIdQueryValidator() | ||
{ | ||
RuleFor(query => query.Id) | ||
.NotNull() | ||
.NotEmpty(); | ||
} | ||
} | ||
|
||
public class GetTodoByIdQueryHandler : IRequestHandler<GetTodoByIdQuery, Result<TodoDto>> | ||
{ | ||
private readonly TodoDb _db; | ||
private readonly ILogger<GetTodoByIdQueryHandler> _logger; | ||
|
||
public GetTodoByIdQueryHandler(TodoDb db, ILogger<GetTodoByIdQueryHandler> logger) => (_db, _logger) = (db, logger); | ||
|
||
public async Task<Result<TodoDto>> Handle(GetTodoByIdQuery request, CancellationToken cancellationToken) | ||
{ | ||
// validate | ||
var validator = new GetTodoByIdQueryValidator(); | ||
ValidationResult validationResult = validator.Validate(request); | ||
|
||
if (!validationResult.IsValid) | ||
{ | ||
var errors = validationResult.Errors.Select(e => new KeyValuePair<string, string>(e.PropertyName, e.ErrorMessage)); | ||
return new Result<TodoDto>(errors); | ||
} | ||
|
||
_logger.LogDebug($"Start query"); | ||
Todo? todo = await _db.Todos | ||
.AsNoTracking() | ||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken: cancellationToken); | ||
_logger.LogDebug($"End query"); | ||
|
||
return new Result<TodoDto>(todo?.ToDto()); | ||
Check warning on line 47 in JixMinApi/Features/Todo/Queries/GetTodoByIdQuery.cs
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters