| 1 |
package com.paymentlink.controller.api; |
| 2 |
|
| 3 |
import com.paymentlink.model.entity.ExchangeRate; |
| 4 |
import com.paymentlink.service.CurrencyService; |
| 5 |
import org.springframework.http.HttpStatus; |
| 6 |
import org.springframework.http.ResponseEntity; |
| 7 |
import org.springframework.web.bind.annotation.*; |
| 8 |
|
| 9 |
import java.util.HashMap; |
| 10 |
import java.util.List; |
| 11 |
import java.util.Map; |
| 12 |
import java.util.Set; |
| 13 |
|
| 14 |
@RestController |
| 15 |
@RequestMapping("/api/currency") |
| 16 |
public class CurrencyApiController { |
| 17 |
|
| 18 |
private final CurrencyService currencyService; |
| 19 |
|
| 20 |
public CurrencyApiController(CurrencyService currencyService) { |
| 21 |
this.currencyService = currencyService; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
* GET /api/currency/rates - Get all current exchange rates |
| 26 |
*/ |
| 27 |
@GetMapping("/rates") |
| 28 |
public ResponseEntity<Map<String, Object>> getExchangeRates() { |
| 29 |
try { |
| 30 |
List<ExchangeRate> rates = currencyService.getAllRates(); |
| 31 |
|
| 32 |
Map<String, Object> response = new HashMap<>(); |
| 33 |
response.put("success", true); |
| 34 |
response.put("rates", rates); |
| 35 |
response.put("total", rates.size()); |
| 36 |
|
| 37 |
return ResponseEntity.ok(response); |
| 38 |
} catch (Exception e) { |
| 39 |
Map<String, Object> error = new HashMap<>(); |
| 40 |
error.put("success", false); |
| 41 |
error.put("error", e.getMessage()); |
| 42 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
* GET /api/currency/convert - Convert an amount between currencies |
| 48 |
*/ |
| 49 |
@GetMapping("/convert") |
| 50 |
public ResponseEntity<Map<String, Object>> convertCurrency( |
| 51 |
@RequestParam Long amount, |
| 52 |
@RequestParam String from, |
| 53 |
@RequestParam String to) { |
| 54 |
|
| 55 |
try { |
| 56 |
Long converted = currencyService.convertPrice(amount, from, to); |
| 57 |
Double rate = currencyService.getRate(from, to); |
| 58 |
|
| 59 |
Map<String, Object> response = new HashMap<>(); |
| 60 |
response.put("success", true); |
| 61 |
response.put("fromAmount", amount); |
| 62 |
response.put("fromCurrency", from); |
| 63 |
response.put("toAmount", converted); |
| 64 |
response.put("toCurrency", to); |
| 65 |
response.put("rate", rate); |
| 66 |
|
| 67 |
return ResponseEntity.ok(response); |
| 68 |
} catch (Exception e) { |
| 69 |
Map<String, Object> error = new HashMap<>(); |
| 70 |
error.put("success", false); |
| 71 |
error.put("error", e.getMessage()); |
| 72 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
* GET /api/currency/supported - Get list of supported currencies |
| 78 |
*/ |
| 79 |
@GetMapping("/supported") |
| 80 |
public ResponseEntity<Map<String, Object>> getSupportedCurrencies() { |
| 81 |
try { |
| 82 |
Set<String> currencies = currencyService.getSupportedCurrencies(); |
| 83 |
|
| 84 |
Map<String, Object> response = new HashMap<>(); |
| 85 |
response.put("success", true); |
| 86 |
response.put("currencies", currencies); |
| 87 |
response.put("total", currencies.size()); |
| 88 |
|
| 89 |
return ResponseEntity.ok(response); |
| 90 |
} catch (Exception e) { |
| 91 |
Map<String, Object> error = new HashMap<>(); |
| 92 |
error.put("success", false); |
| 93 |
error.put("error", e.getMessage()); |
| 94 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
* POST /api/admin/currency/refresh - Manually trigger exchange rate refresh |
| 100 |
*/ |
| 101 |
@PostMapping("/admin/refresh") |
| 102 |
public ResponseEntity<Map<String, Object>> refreshRates() { |
| 103 |
try { |
| 104 |
currencyService.manualRefresh(); |
| 105 |
|
| 106 |
Map<String, Object> response = new HashMap<>(); |
| 107 |
response.put("success", true); |
| 108 |
response.put("message", "Exchange rates refreshed successfully"); |
| 109 |
|
| 110 |
return ResponseEntity.ok(response); |
| 111 |
} catch (IllegalStateException e) { |
| 112 |
Map<String, Object> error = new HashMap<>(); |
| 113 |
error.put("success", false); |
| 114 |
error.put("error", e.getMessage()); |
| 115 |
return ResponseEntity.status(HttpStatus.CONFLICT).body(error); |
| 116 |
} catch (Exception e) { |
| 117 |
Map<String, Object> error = new HashMap<>(); |
| 118 |
error.put("success", false); |
| 119 |
error.put("error", e.getMessage()); |
| 120 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
* GET /api/currency/rate/{from}/{to} - Get specific exchange rate |
| 126 |
*/ |
| 127 |
@GetMapping("/rate/{from}/{to}") |
| 128 |
public ResponseEntity<Map<String, Object>> getSpecificRate( |
| 129 |
@PathVariable String from, |
| 130 |
@PathVariable String to) { |
| 131 |
|
| 132 |
try { |
| 133 |
Double rate = currencyService.getRate(from, to); |
| 134 |
|
| 135 |
Map<String, Object> response = new HashMap<>(); |
| 136 |
response.put("success", true); |
| 137 |
response.put("fromCurrency", from); |
| 138 |
response.put("toCurrency", to); |
| 139 |
response.put("rate", rate); |
| 140 |
|
| 141 |
return ResponseEntity.ok(response); |
| 142 |
} catch (Exception e) { |
| 143 |
Map<String, Object> error = new HashMap<>(); |
| 144 |
error.put("success", false); |
| 145 |
error.put("error", e.getMessage()); |
| 146 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|