56 lines
1.6 KiB
Java
56 lines
1.6 KiB
Java
package de.arminwolf.financeanalyzer.controller;
|
|
|
|
import de.arminwolf.financeanalyzer.dao.TransactionDAO;
|
|
import de.arminwolf.financeanalyzer.util.UrlUtil;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.client.RestClientException;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import java.util.Objects;
|
|
import java.util.Optional;
|
|
|
|
import static de.arminwolf.financeanalyzer.util.Constants.INDEX;
|
|
|
|
@Controller
|
|
public class MainController {
|
|
|
|
|
|
@GetMapping("/")
|
|
public String get(Model model, HttpServletRequest request) {
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
try {
|
|
final Optional<TransactionDAO[]> response = getTransactions(request, restTemplate);
|
|
if (response.isEmpty()) {
|
|
return "redirect:/upload/";
|
|
} else {
|
|
// Verarbeiten Sie hier die Antwort
|
|
model.addAttribute("data", response.get());
|
|
return INDEX;
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace(System.err);
|
|
return "redirect:/upload";
|
|
}
|
|
|
|
}
|
|
|
|
private static Optional<TransactionDAO[]> getTransactions(final HttpServletRequest request, final RestTemplate restTemplate) {
|
|
try {
|
|
TransactionDAO[] forObject = restTemplate.getForObject(UrlUtil.getRelativeUrl(request, "/file/current"), TransactionDAO[].class);
|
|
if (Objects.isNull(forObject)) {
|
|
return Optional.empty();
|
|
} else {
|
|
return Optional.of(forObject);
|
|
}
|
|
} catch (RestClientException e) {
|
|
return Optional.empty();
|
|
}
|
|
|
|
}
|
|
|
|
}
|