73 lines
2.6 KiB
Java
Raw Normal View History

package com.contextual.help;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.nio.file.Path;
import java.nio.file.Files;
@RestController
@RequestMapping("/api/help")
public class HelpController {
/*
@GetMapping
public ResponseEntity<?> getHelp(@RequestParam String lang) {
String fileName = String.format("help/help.%s.json", lang);
try (InputStream is = getClass().getClassLoader().getResourceAsStream(fileName)) {
if (is == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("Help content not found for language: " + lang);
}
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> helpContent = mapper.readValue(is, new TypeReference<Map<String, Object>>() {});
return ResponseEntity.ok(helpContent);
} catch (IOException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Error loading help content");
}
}*/
@GetMapping
public ResponseEntity<?> getHelp(@RequestParam String lang) {
Path path = Path.of("src/main/resources/help/help." + lang + ".json");
if (!Files.exists(path)) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("Help content not found for language: " + lang);
}
try {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> helpContent = mapper.readValue(path.toFile(), new TypeReference<Map<String, Object>>() {});
return ResponseEntity.ok(helpContent);
} catch (IOException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Error loading help content: " + e.getMessage());
}
}
@PutMapping
public ResponseEntity<?> updateHelp(@RequestBody Map<String, HelpItem> newData,
@RequestParam(defaultValue = "en") String lang) {
try {
Path path = Path.of("src/main/resources/help/help." + lang + ".json");
new ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(path.toFile(), newData);
return ResponseEntity.ok().build();
} catch (IOException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to save data.");
}
}
}