@@ -11,21 +11,21 @@ public class GetAccountByIdQueryHandler(IApplicationDbContext context) : IReques
1111 {
1212 public async Task < Result < AccountDto > > Handle ( GetAccountByIdQuery request , CancellationToken cancellationToken )
1313 {
14- if ( request . Id == Guid . Empty )
15- return Result < AccountDto > . Failure ( "Unable to process request, Account ID is not provided." , 400 ) ;
14+ var accountId = request . Id ;
15+ var userId = request . UserId ;
1616
17- var accountEntity = await context . Accounts
17+ if ( accountId == Guid . Empty || userId == Guid . Empty )
18+ return Result < AccountDto > . Failure ( "Unable to process request, missing IDs." , 400 ) ;
19+
20+ var account = await context . Accounts
1821 . Include ( a => a . Transactions )
1922 . AsNoTracking ( )
20- . FirstOrDefaultAsync ( a => a . Id == request . Id , cancellationToken ) ;
23+ . FirstOrDefaultAsync ( a => a . Id == accountId && a . UserId == userId , cancellationToken ) ;
2124
22- if ( accountEntity is null )
25+ if ( account is null )
2326 return Result < AccountDto > . Failure ( "Account not found." , 404 ) ;
2427
25- if ( accountEntity . UserId != request . UserId )
26- return Result < AccountDto > . Failure ( "Unauthorized access to account." , 401 ) ;
27-
28- var transactionDtos = accountEntity . Transactions ? . Select ( t => new TransactionDto
28+ var transactionDtos = account . Transactions ? . Select ( t => new TransactionDto
2929 (
3030 Id : t . Id ,
3131 AccountId : t . AccountId ,
@@ -38,11 +38,11 @@ public async Task<Result<AccountDto>> Handle(GetAccountByIdQuery request, Cancel
3838
3939 var accountDto = new AccountDto
4040 (
41- Id : accountEntity . Id ,
42- Name : accountEntity . Name ,
43- Balance : accountEntity . Balance ,
44- Currency : accountEntity . Currency ,
45- CreatedAt : accountEntity . CreatedAt ,
41+ Id : account . Id ,
42+ Name : account . Name ,
43+ Balance : account . Balance ,
44+ Currency : account . Currency ,
45+ CreatedAt : account . CreatedAt ,
4646 Transactions : transactionDtos
4747 ) ;
4848
0 commit comments