| 1 |
package com.paymentlink.controller.api; |
| 2 |
|
| 3 |
import com.paymentlink.model.entity.Region; |
| 4 |
import com.paymentlink.service.RegionService; |
| 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 |
|
| 13 |
@RestController |
| 14 |
@RequestMapping("/api") |
| 15 |
public class RegionApiController { |
| 16 |
|
| 17 |
private final RegionService regionService; |
| 18 |
|
| 19 |
public RegionApiController(RegionService regionService) { |
| 20 |
this.regionService = regionService; |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
* GET /api/admin/regions - List all regions |
| 25 |
*/ |
| 26 |
@GetMapping("/admin/regions") |
| 27 |
public ResponseEntity<Map<String, Object>> getAllRegions( |
| 28 |
@RequestParam(required = false) Boolean enabled) { |
| 29 |
|
| 30 |
try { |
| 31 |
List<Region> regions; |
| 32 |
|
| 33 |
if (enabled != null) { |
| 34 |
regions = regionService.getAllRegions().stream() |
| 35 |
.filter(r -> r.getEnabled().equals(enabled)) |
| 36 |
.toList(); |
| 37 |
} else { |
| 38 |
regions = regionService.getAllRegions(); |
| 39 |
} |
| 40 |
|
| 41 |
Map<String, Object> response = new HashMap<>(); |
| 42 |
response.put("success", true); |
| 43 |
response.put("regions", regions); |
| 44 |
response.put("total", regions.size()); |
| 45 |
|
| 46 |
return ResponseEntity.ok(response); |
| 47 |
} catch (Exception e) { |
| 48 |
Map<String, Object> error = new HashMap<>(); |
| 49 |
error.put("success", false); |
| 50 |
error.put("error", e.getMessage()); |
| 51 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
* GET /api/admin/regions/stats - Get region statistics |
| 57 |
*/ |
| 58 |
@GetMapping("/admin/regions/stats") |
| 59 |
public ResponseEntity<Map<String, Object>> getRegionStats() { |
| 60 |
try { |
| 61 |
Map<String, Object> stats = regionService.getStatistics(); |
| 62 |
stats.put("success", true); |
| 63 |
return ResponseEntity.ok(stats); |
| 64 |
} catch (Exception e) { |
| 65 |
Map<String, Object> error = new HashMap<>(); |
| 66 |
error.put("success", false); |
| 67 |
error.put("error", e.getMessage()); |
| 68 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
* GET /api/regions/enabled - Get all enabled regions (public) |
| 74 |
*/ |
| 75 |
@GetMapping("/regions/enabled") |
| 76 |
public ResponseEntity<Map<String, Object>> getEnabledRegions() { |
| 77 |
try { |
| 78 |
List<Region> regions = regionService.getAllEnabledRegions(); |
| 79 |
|
| 80 |
Map<String, Object> response = new HashMap<>(); |
| 81 |
response.put("success", true); |
| 82 |
response.put("regions", regions); |
| 83 |
response.put("total", regions.size()); |
| 84 |
|
| 85 |
return ResponseEntity.ok(response); |
| 86 |
} catch (Exception e) { |
| 87 |
Map<String, Object> error = new HashMap<>(); |
| 88 |
error.put("success", false); |
| 89 |
error.put("error", e.getMessage()); |
| 90 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
* POST /api/admin/regions/{countryCode}/enable - Enable a country |
| 96 |
*/ |
| 97 |
@PostMapping("/admin/regions/{countryCode}/enable") |
| 98 |
public ResponseEntity<Map<String, Object>> enableCountry(@PathVariable String countryCode) { |
| 99 |
try { |
| 100 |
regionService.enableCountry(countryCode); |
| 101 |
|
| 102 |
Map<String, Object> response = new HashMap<>(); |
| 103 |
response.put("success", true); |
| 104 |
response.put("message", "Country " + countryCode + " enabled successfully"); |
| 105 |
|
| 106 |
return ResponseEntity.ok(response); |
| 107 |
} catch (Exception e) { |
| 108 |
Map<String, Object> error = new HashMap<>(); |
| 109 |
error.put("success", false); |
| 110 |
error.put("error", e.getMessage()); |
| 111 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
* POST /api/admin/regions/{countryCode}/disable - Disable a country |
| 117 |
*/ |
| 118 |
@PostMapping("/admin/regions/{countryCode}/disable") |
| 119 |
public ResponseEntity<Map<String, Object>> disableCountry(@PathVariable String countryCode) { |
| 120 |
try { |
| 121 |
regionService.disableCountry(countryCode); |
| 122 |
|
| 123 |
Map<String, Object> response = new HashMap<>(); |
| 124 |
response.put("success", true); |
| 125 |
response.put("message", "Country " + countryCode + " disabled successfully"); |
| 126 |
|
| 127 |
return ResponseEntity.ok(response); |
| 128 |
} catch (Exception e) { |
| 129 |
Map<String, Object> error = new HashMap<>(); |
| 130 |
error.put("success", false); |
| 131 |
error.put("error", e.getMessage()); |
| 132 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
* GET /api/admin/regions/{countryCode} - Get specific region |
| 138 |
*/ |
| 139 |
@GetMapping("/admin/regions/{countryCode}") |
| 140 |
public ResponseEntity<Map<String, Object>> getRegion(@PathVariable String countryCode) { |
| 141 |
try { |
| 142 |
Region region = regionService.getRegion(countryCode); |
| 143 |
|
| 144 |
if (region == null) { |
| 145 |
Map<String, Object> error = new HashMap<>(); |
| 146 |
error.put("success", false); |
| 147 |
error.put("error", "Region not found"); |
| 148 |
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error); |
| 149 |
} |
| 150 |
|
| 151 |
Map<String, Object> response = new HashMap<>(); |
| 152 |
response.put("success", true); |
| 153 |
response.put("region", region); |
| 154 |
|
| 155 |
return ResponseEntity.ok(response); |
| 156 |
} catch (Exception e) { |
| 157 |
Map<String, Object> error = new HashMap<>(); |
| 158 |
error.put("success", false); |
| 159 |
error.put("error", e.getMessage()); |
| 160 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|