Skip to content

Commit

Permalink
replace NotFoundException
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Oct 4, 2024
1 parent e958b66 commit 76583cb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/Templates/Commands/AddEdit/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public class AddEdit{itemname}Command: ICacheInvalidatorRequest<Result<int>>
{
if (request.Id > 0)
{
var item = await _context.{nameofPlural}.FindAsync(new object[] { request.Id }, cancellationToken) ?? throw new NotFoundException($"{itemname} with id: [{request.Id}] not found.");
var item = await _context.{nameofPlural}.FindAsync(request.Id, cancellationToken);
if (item == null)
{
return await Result<int>.FailureAsync($"{itemname} with id: [{request.Id}] not found.");
}
item = _mapper.Map(request, item);
// raise a update domain event
item.AddDomainEvent(new {itemname}UpdatedEvent(item));
Expand Down
6 changes: 5 additions & 1 deletion src/Templates/Commands/Update/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ public class Update{itemname}Command: ICacheInvalidatorRequest<Result<int>>
public async Task<Result<int>> Handle(Update{itemname}Command request, CancellationToken cancellationToken)
{

var item =await _context.{nameofPlural}.FindAsync( new object[] { request.Id }, cancellationToken)?? throw new NotFoundException($"{itemname} with id: [{request.Id}] not found.");
var item = await _context.{nameofPlural}.FindAsync(request.Id, cancellationToken);
if (item == null)
{
return await Result<int>.FailureAsync($"{itemname} with id: [{request.Id}] not found.");
}
item = _mapper.Map(request, item);
// raise a update domain event
item.AddDomainEvent(new {itemname}UpdatedEvent(item));
Expand Down
8 changes: 4 additions & 4 deletions src/Templates/Queries/GetById/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ using {selectns}.{nameofPlural}.Specifications;

namespace {namespace};

public class Get{itemname}ByIdQuery : ICacheableRequest<{itemname}Dto>
public class Get{itemname}ByIdQuery : ICacheableRequest<{itemname}Dto?>
{
public required int Id { get; set; }
public string CacheKey => {itemname}CacheKey.GetByIdCacheKey($"{Id}");
public MemoryCacheEntryOptions? Options => {itemname}CacheKey.MemoryCacheEntryOptions;
}

public class Get{itemname}ByIdQueryHandler :
IRequestHandler<Get{itemname}ByIdQuery, {itemname}Dto>
IRequestHandler<Get{itemname}ByIdQuery, {itemname}Dto?>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
Expand All @@ -32,11 +32,11 @@ public class Get{itemname}ByIdQueryHandler :
_localizer = localizer;
}

public async Task<{itemname}Dto> Handle(Get{itemname}ByIdQuery request, CancellationToken cancellationToken)
public async Task<{itemname}Dto?> Handle(Get{itemname}ByIdQuery request, CancellationToken cancellationToken)
{
var data = await _context.{nameofPlural}.ApplySpecification(new {itemname}ByIdSpecification(request.Id))
.ProjectTo<{itemname}Dto>(_mapper.ConfigurationProvider)
.FirstAsync(cancellationToken) ?? throw new NotFoundException($"{itemname} with id: [{request.Id}] not found.");
.FirstAsync(cancellationToken);
return data;
}
}

0 comments on commit 76583cb

Please sign in to comment.