|
| 1 | +package com.blockchain.api.application.controller; |
| 2 | + |
| 3 | +import com.blockchain.api.application.exception.InvalidAddressException; |
| 4 | +import com.blockchain.api.application.mapper.BalanceDtoMapper; |
| 5 | +import com.blockchain.api.domain.service.balance.BalanceService; |
| 6 | +import com.blockchain.api.model.ApiError; |
| 7 | +import com.blockchain.api.model.BalanceDto; |
| 8 | +import io.swagger.v3.oas.annotations.Operation; |
| 9 | +import io.swagger.v3.oas.annotations.media.Content; |
| 10 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 11 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 12 | +import io.swagger.v3.oas.annotations.responses.ApiResponses; |
| 13 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 14 | +import lombok.RequiredArgsConstructor; |
| 15 | +import lombok.extern.slf4j.Slf4j; |
| 16 | +import org.springframework.web.bind.annotation.GetMapping; |
| 17 | +import org.springframework.web.bind.annotation.PathVariable; |
| 18 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 19 | +import org.springframework.web.bind.annotation.RestController; |
| 20 | +import org.web3j.crypto.WalletUtils; |
| 21 | + |
| 22 | +@Slf4j |
| 23 | +@RestController |
| 24 | +@RequiredArgsConstructor |
| 25 | +@RequestMapping("/api/v1/balances") |
| 26 | +@Tag( |
| 27 | + name = "Ethereum Balance", |
| 28 | + description = "Operations related to fetching Ethereum account balances") |
| 29 | +public class EthereumBalanceController { |
| 30 | + |
| 31 | + private final BalanceDtoMapper mapper; |
| 32 | + private final BalanceService balanceService; |
| 33 | + |
| 34 | + @Operation( |
| 35 | + summary = "Get Ethereum Balance", |
| 36 | + description = "Fetch the balance of a Ethereum account by its address") |
| 37 | + @ApiResponses({ |
| 38 | + @ApiResponse( |
| 39 | + responseCode = "200", |
| 40 | + description = "Successfully retrieved balance", |
| 41 | + content = |
| 42 | + @Content( |
| 43 | + mediaType = "application/json", |
| 44 | + schema = @Schema(implementation = BalanceDto.class))), |
| 45 | + @ApiResponse( |
| 46 | + responseCode = "400", |
| 47 | + description = "Invalid address format", |
| 48 | + content = |
| 49 | + @Content( |
| 50 | + mediaType = "application/json", |
| 51 | + schema = @Schema(implementation = ApiError.class))), |
| 52 | + @ApiResponse( |
| 53 | + responseCode = "500", |
| 54 | + description = "Internal server error", |
| 55 | + content = |
| 56 | + @Content( |
| 57 | + mediaType = "application/json", |
| 58 | + schema = @Schema(implementation = ApiError.class))) |
| 59 | + }) |
| 60 | + @GetMapping("/{address}") |
| 61 | + public BalanceDto getBalance(@PathVariable("address") String address) { |
| 62 | + log.info("Fetching balance for address: {}", address); |
| 63 | + if (!WalletUtils.isValidAddress(address)) { |
| 64 | + throw InvalidAddressException.withAddress(address); |
| 65 | + } |
| 66 | + return mapper.mapToDto(address, balanceService.getEthereumBalance(address).toString()); |
| 67 | + } |
| 68 | +} |
0 commit comments