public async Task <IActionResult> GetAccount([FromRoute] Guid accountId) { if (!HttpContext.User.IsInRole("Admin")) { if (!HttpContext.HasAccountId(accountId.ToString())) { throw new ForbiddenException(); } } var queryAccountCommand = new QueryAccountCommand { Offset = 0, Limit = 1, AccountId = new AccountId(accountId) }; PaginatedCollection <AccountResponse> paginatedCollection = await _executionContext.ExecuteAsync(queryAccountCommand, CancellationToken.None); AccountResponse accountResponse = paginatedCollection.Data.First(); AccountHttpResponse result = new AccountHttpResponse { AccountId = accountResponse.AccountId.Value, Username = accountResponse.Username.Value, FirstName = accountResponse.Name.FirstName, LastName = accountResponse.Name.LastName }; return(StatusCode((int)HttpStatusCode.OK, result)); }
public async Task <IActionResult> GetAccounts([FromQuery] GetAccountHttpRequest?getAccountHttpRequest) { var queryAccountCommand = new QueryAccountCommand { Offset = getAccountHttpRequest?.Offset ?? 0, Limit = getAccountHttpRequest?.Limit ?? 1, }; PaginatedCollection <AccountResponse> paginatedCollection = await _executionContext.ExecuteAsync(queryAccountCommand, CancellationToken.None); PaginatedCollection <AccountHttpResponse> result = new PaginatedCollection <AccountHttpResponse>(paginatedCollection.TotalCount, paginatedCollection.Data.Select(a => new AccountHttpResponse { AccountId = a.AccountId.Value, Username = a.Username.Value, FirstName = a.Name.FirstName, LastName = a.Name.LastName })); return(StatusCode((int)HttpStatusCode.OK, result)); }
public async Task <PaginatedCollection <AccountResponse> > Handle(QueryAccountCommand request, CancellationToken cancellationToken) { var accountRepository = _accountDbContext.AccountRepository; PaginatedCollection <AccountResponse> result; if (request.AccountId != null) { Account account = await accountRepository.GetByAccountIdAsync(request.AccountId, cancellationToken); result = new PaginatedCollection <AccountResponse>(1, new[] { new AccountResponse(account.Id, account.Username, account.Name) }); } else { PaginatedCollection <Account> paginatedCollection = await accountRepository.GetAsync(request.Offset, request.Limit, cancellationToken); result = new PaginatedCollection <AccountResponse>(paginatedCollection.TotalCount, paginatedCollection.Data.Select(a => new AccountResponse(a.Id, a.Username, a.Name)).ToList()); } return(result); }