package com.paymentlink.controller.api; import com.paymentlink.model.entity.Translation; import com.paymentlink.service.TranslationService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping("/api/admin/translations") public class TranslationApiController { private final TranslationService translationService; public TranslationApiController(TranslationService translationService) { this.translationService = translationService; } /** * GET /api/admin/translations - List all translations */ @GetMapping public ResponseEntity> getAllTranslations() { try { // In a production app, this would be paginated Map response = new HashMap<>(); response.put("success", true); response.put("message", "Use specific endpoints to get translations for entities"); response.put("supportedLanguages", translationService.getSupportedLanguages()); 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); } } /** * GET /api/admin/translations/{entityType}/{entityId} - Get all translations for an entity */ @GetMapping("/{entityType}/{entityId}") public ResponseEntity> getTranslationsForEntity( @PathVariable String entityType, @PathVariable String entityId) { try { List translations = translationService.getTranslationsForEntity(entityType, entityId); Map response = new HashMap<>(); response.put("success", true); response.put("translations", translations); response.put("total", translations.size()); 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); } } /** * POST /api/admin/translations - Create or update a translation */ @PostMapping public ResponseEntity> saveTranslation(@RequestBody Map request) { try { String entityType = request.get("entityType"); String entityId = request.get("entityId"); String fieldName = request.get("fieldName"); String languageCode = request.get("languageCode"); String translatedText = request.get("translatedText"); // Validate required fields if (entityType == null || entityId == null || fieldName == null || languageCode == null || translatedText == null) { Map error = new HashMap<>(); error.put("success", false); error.put("error", "Missing required fields"); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error); } Translation translation = translationService.saveTranslation( entityType, entityId, fieldName, languageCode, translatedText); Map response = new HashMap<>(); response.put("success", true); response.put("translation", translation); response.put("message", "Translation saved successfully"); 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); } } /** * DELETE /api/admin/translations/{id} - Delete a translation */ @DeleteMapping("/{id}") public ResponseEntity> deleteTranslation(@PathVariable Long id) { try { translationService.deleteTranslation(id); Map response = new HashMap<>(); response.put("success", true); response.put("message", "Translation deleted successfully"); 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); } } /** * GET /api/admin/translations/languages - Get supported languages */ @GetMapping("/languages") public ResponseEntity> getSupportedLanguages() { try { List languages = translationService.getSupportedLanguages(); Map response = new HashMap<>(); response.put("success", true); response.put("languages", languages); 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); } } }