finance-analyzer/src/main/java/de/arminwolf/financeanalyzer/controller/FileController.java

39 lines
1.2 KiB
Java

package de.arminwolf.financeanalyzer.controller;
import de.arminwolf.financeanalyzer.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;
@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
CacheService cacheService;
@RequestMapping(value = "/current", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<String> get() throws IOException {
if (Objects.nonNull(cacheService.get("currentJsonFile"))) {
String jsonFile = Files.readString(Paths.get(cacheService.get("currentJsonFile").toString()));
if (Objects.nonNull(jsonFile) && !jsonFile.isEmpty()) {
return ResponseEntity.ok(jsonFile);
}
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("No persistent JSON found. Upload a new one.");
}
}