| 1 |
package com.paymentlink.controller.api; |
| 2 |
|
| 3 |
import com.paymentlink.model.entity.Translation; |
| 4 |
import com.paymentlink.service.TranslationService; |
| 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/admin/translations") |
| 15 |
public class TranslationApiController { |
| 16 |
|
| 17 |
private final TranslationService translationService; |
| 18 |
|
| 19 |
public TranslationApiController(TranslationService translationService) { |
| 20 |
this.translationService = translationService; |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
* GET /api/admin/translations - List all translations |
| 25 |
*/ |
| 26 |
@GetMapping |
| 27 |
public ResponseEntity<Map<String, Object>> getAllTranslations() { |
| 28 |
try { |
| 29 |
|
| 30 |
Map<String, Object> response = new HashMap<>(); |
| 31 |
response.put("success", true); |
| 32 |
response.put("message", "Use specific endpoints to get translations for entities"); |
| 33 |
response.put("supportedLanguages", translationService.getSupportedLanguages()); |
| 34 |
|
| 35 |
return ResponseEntity.ok(response); |
| 36 |
} catch (Exception e) { |
| 37 |
Map<String, Object> error = new HashMap<>(); |
| 38 |
error.put("success", false); |
| 39 |
error.put("error", e.getMessage()); |
| 40 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
* GET /api/admin/translations/{entityType}/{entityId} - Get all translations for an entity |
| 46 |
*/ |
| 47 |
@GetMapping("/{entityType}/{entityId}") |
| 48 |
public ResponseEntity<Map<String, Object>> getTranslationsForEntity( |
| 49 |
@PathVariable String entityType, |
| 50 |
@PathVariable String entityId) { |
| 51 |
|
| 52 |
try { |
| 53 |
List<Translation> translations = translationService.getTranslationsForEntity(entityType, entityId); |
| 54 |
|
| 55 |
Map<String, Object> response = new HashMap<>(); |
| 56 |
response.put("success", true); |
| 57 |
response.put("translations", translations); |
| 58 |
response.put("total", translations.size()); |
| 59 |
|
| 60 |
return ResponseEntity.ok(response); |
| 61 |
} catch (Exception e) { |
| 62 |
Map<String, Object> error = new HashMap<>(); |
| 63 |
error.put("success", false); |
| 64 |
error.put("error", e.getMessage()); |
| 65 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
* POST /api/admin/translations - Create or update a translation |
| 71 |
*/ |
| 72 |
@PostMapping |
| 73 |
public ResponseEntity<Map<String, Object>> saveTranslation(@RequestBody Map<String, String> request) { |
| 74 |
try { |
| 75 |
String entityType = request.get("entityType"); |
| 76 |
String entityId = request.get("entityId"); |
| 77 |
String fieldName = request.get("fieldName"); |
| 78 |
String languageCode = request.get("languageCode"); |
| 79 |
String translatedText = request.get("translatedText"); |
| 80 |
|
| 81 |
|
| 82 |
if (entityType == null || entityId == null || fieldName == null || |
| 83 |
languageCode == null || translatedText == null) { |
| 84 |
Map<String, Object> error = new HashMap<>(); |
| 85 |
error.put("success", false); |
| 86 |
error.put("error", "Missing required fields"); |
| 87 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error); |
| 88 |
} |
| 89 |
|
| 90 |
Translation translation = translationService.saveTranslation( |
| 91 |
entityType, entityId, fieldName, languageCode, translatedText); |
| 92 |
|
| 93 |
Map<String, Object> response = new HashMap<>(); |
| 94 |
response.put("success", true); |
| 95 |
response.put("translation", translation); |
| 96 |
response.put("message", "Translation saved successfully"); |
| 97 |
|
| 98 |
return ResponseEntity.ok(response); |
| 99 |
} catch (Exception e) { |
| 100 |
Map<String, Object> error = new HashMap<>(); |
| 101 |
error.put("success", false); |
| 102 |
error.put("error", e.getMessage()); |
| 103 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
* DELETE /api/admin/translations/{id} - Delete a translation |
| 109 |
*/ |
| 110 |
@DeleteMapping("/{id}") |
| 111 |
public ResponseEntity<Map<String, Object>> deleteTranslation(@PathVariable Long id) { |
| 112 |
try { |
| 113 |
translationService.deleteTranslation(id); |
| 114 |
|
| 115 |
Map<String, Object> response = new HashMap<>(); |
| 116 |
response.put("success", true); |
| 117 |
response.put("message", "Translation deleted successfully"); |
| 118 |
|
| 119 |
return ResponseEntity.ok(response); |
| 120 |
} catch (Exception e) { |
| 121 |
Map<String, Object> error = new HashMap<>(); |
| 122 |
error.put("success", false); |
| 123 |
error.put("error", e.getMessage()); |
| 124 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
* GET /api/admin/translations/languages - Get supported languages |
| 130 |
*/ |
| 131 |
@GetMapping("/languages") |
| 132 |
public ResponseEntity<Map<String, Object>> getSupportedLanguages() { |
| 133 |
try { |
| 134 |
List<String> languages = translationService.getSupportedLanguages(); |
| 135 |
|
| 136 |
Map<String, Object> response = new HashMap<>(); |
| 137 |
response.put("success", true); |
| 138 |
response.put("languages", languages); |
| 139 |
|
| 140 |
return ResponseEntity.ok(response); |
| 141 |
} catch (Exception e) { |
| 142 |
Map<String, Object> error = new HashMap<>(); |
| 143 |
error.put("success", false); |
| 144 |
error.put("error", e.getMessage()); |
| 145 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|