package com.paymentlink.controller.api; import com.paymentlink.service.TaxService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.*; import java.util.stream.Collectors; @RestController @RequestMapping("/api/countries") public class CountryApiController { private final TaxService taxService; public CountryApiController(TaxService taxService) { this.taxService = taxService; } /** * GET /api/countries - Get supported countries */ @GetMapping public ResponseEntity> getCountries() { try { Map countriesMap = taxService.getSupportedCountries(); // Convert map to array of objects with code and name List> countries = countriesMap.entrySet().stream() .map(entry -> { Map country = new HashMap<>(); country.put("code", entry.getKey()); country.put("name", entry.getValue()); return country; }) .sorted(Comparator.comparing(c -> c.get("name"))) // Sort by name .collect(Collectors.toList()); Map response = new HashMap<>(); response.put("success", true); response.put("countries", countries); return ResponseEntity.ok(response); } catch (Exception e) { Map error = new HashMap<>(); error.put("success", false); error.put("error", e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); } } }