| 1 |
package com.paymentlink.controller.api; |
| 2 |
|
| 3 |
import com.paymentlink.service.FlagService; |
| 4 |
import org.springframework.http.HttpStatus; |
| 5 |
import org.springframework.http.ResponseEntity; |
| 6 |
import org.springframework.web.bind.annotation.*; |
| 7 |
|
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.List; |
| 10 |
import java.util.Map; |
| 11 |
|
| 12 |
|
| 13 |
* REST API for country flags |
| 14 |
*/ |
| 15 |
@RestController |
| 16 |
@RequestMapping("/api/flags") |
| 17 |
public class FlagApiController { |
| 18 |
|
| 19 |
private final FlagService flagService; |
| 20 |
|
| 21 |
public FlagApiController(FlagService flagService) { |
| 22 |
this.flagService = flagService; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
* Get flag for a specific country |
| 27 |
* |
| 28 |
* GET /api/flags/{countryCode} |
| 29 |
* Example: /api/flags/US, /api/flags/TR |
| 30 |
* |
| 31 |
* @param countryCode ISO 3166-1 alpha-2 country code |
| 32 |
* @return Flag as base64 data URL |
| 33 |
*/ |
| 34 |
@GetMapping("/{countryCode}") |
| 35 |
public ResponseEntity<Map<String, Object>> getFlag( |
| 36 |
@PathVariable String countryCode) { |
| 37 |
|
| 38 |
String flag = flagService.getFlag(countryCode); |
| 39 |
|
| 40 |
Map<String, Object> response = new HashMap<>(); |
| 41 |
|
| 42 |
if (flag != null) { |
| 43 |
response.put("success", true); |
| 44 |
response.put("countryCode", countryCode.toUpperCase()); |
| 45 |
response.put("flag", flag); |
| 46 |
response.put("type", "image"); |
| 47 |
return ResponseEntity.ok(response); |
| 48 |
} else { |
| 49 |
response.put("success", false); |
| 50 |
response.put("countryCode", countryCode.toUpperCase()); |
| 51 |
response.put("flag", flagService.getFallbackIcon()); |
| 52 |
response.put("type", "svg"); |
| 53 |
response.put("message", "Flag not found, returning fallback icon"); |
| 54 |
return ResponseEntity.ok(response); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
* Get multiple flags at once |
| 60 |
* |
| 61 |
* POST /api/flags/batch |
| 62 |
* Body: { "countryCodes": ["US", "GB", "TR"] } |
| 63 |
* |
| 64 |
* @param request Request containing list of country codes |
| 65 |
* @return Map of country codes to flags |
| 66 |
*/ |
| 67 |
@PostMapping("/batch") |
| 68 |
public ResponseEntity<Map<String, Object>> getFlags( |
| 69 |
@RequestBody BatchFlagRequest request) { |
| 70 |
|
| 71 |
Map<String, String> flags = new HashMap<>(); |
| 72 |
|
| 73 |
for (String code : request.countryCodes()) { |
| 74 |
String flag = flagService.getFlag(code); |
| 75 |
if (flag != null) { |
| 76 |
flags.put(code.toUpperCase(), flag); |
| 77 |
} else { |
| 78 |
flags.put(code.toUpperCase(), flagService.getFallbackIcon()); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
Map<String, Object> response = new HashMap<>(); |
| 83 |
response.put("success", true); |
| 84 |
response.put("count", flags.size()); |
| 85 |
response.put("flags", flags); |
| 86 |
|
| 87 |
return ResponseEntity.ok(response); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
* Get cache statistics |
| 92 |
* |
| 93 |
* GET /api/flags/stats |
| 94 |
*/ |
| 95 |
@GetMapping("/stats") |
| 96 |
public ResponseEntity<Map<String, Object>> getCacheStats() { |
| 97 |
FlagService.CacheStats stats = flagService.getCacheStats(); |
| 98 |
|
| 99 |
Map<String, Object> response = new HashMap<>(); |
| 100 |
response.put("success", true); |
| 101 |
response.put("cache", Map.of( |
| 102 |
"memorySize", stats.memorySize(), |
| 103 |
"diskFiles", stats.diskFiles(), |
| 104 |
"hitCount", stats.hitCount(), |
| 105 |
"missCount", stats.missCount(), |
| 106 |
"hitRate", stats.hitCount() + stats.missCount() > 0 |
| 107 |
? (double) stats.hitCount() / (stats.hitCount() + stats.missCount()) |
| 108 |
: 0.0 |
| 109 |
)); |
| 110 |
|
| 111 |
return ResponseEntity.ok(response); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
* Clear flag cache (admin only) |
| 116 |
* |
| 117 |
* DELETE /api/flags/cache |
| 118 |
*/ |
| 119 |
@DeleteMapping("/cache") |
| 120 |
public ResponseEntity<Map<String, Object>> clearCache() { |
| 121 |
flagService.clearCache(); |
| 122 |
|
| 123 |
Map<String, Object> response = new HashMap<>(); |
| 124 |
response.put("success", true); |
| 125 |
response.put("message", "Flag cache cleared successfully"); |
| 126 |
|
| 127 |
return ResponseEntity.ok(response); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
* Prefetch flags for common countries |
| 132 |
* |
| 133 |
* POST /api/flags/prefetch |
| 134 |
*/ |
| 135 |
@PostMapping("/prefetch") |
| 136 |
public ResponseEntity<Map<String, Object>> prefetchFlags( |
| 137 |
@RequestBody(required = false) BatchFlagRequest request) { |
| 138 |
|
| 139 |
String[] codes; |
| 140 |
if (request != null && request.countryCodes() != null) { |
| 141 |
codes = request.countryCodes().toArray(new String[0]); |
| 142 |
} else { |
| 143 |
|
| 144 |
codes = new String[]{"US", "GB", "CA", "AU", "DE", "FR", "IT", "ES", |
| 145 |
"BR", "MX", "JP", "CN", "IN", "TR", "RU", "KR"}; |
| 146 |
} |
| 147 |
|
| 148 |
flagService.prefetchFlags(codes); |
| 149 |
|
| 150 |
Map<String, Object> response = new HashMap<>(); |
| 151 |
response.put("success", true); |
| 152 |
response.put("message", "Prefetched " + codes.length + " flags"); |
| 153 |
response.put("countryCodes", codes); |
| 154 |
|
| 155 |
return ResponseEntity.ok(response); |
| 156 |
} |
| 157 |
|
| 158 |
public record BatchFlagRequest(List<String> countryCodes) {} |
| 159 |
} |
| 160 |
|