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

58 lines
2.0 KiB
Java

package de.arminwolf.financeanalyzer.controller;
import de.arminwolf.financeanalyzer.conf.Configuration;
import de.arminwolf.financeanalyzer.dao.TransactionDAO;
import de.arminwolf.financeanalyzer.dao.ReportDAO;
import de.arminwolf.financeanalyzer.dao.TableDataDAO;
import de.arminwolf.financeanalyzer.service.CacheService;
import de.arminwolf.financeanalyzer.service.ReportService;
import de.arminwolf.financeanalyzer.util.Constants;
import de.arminwolf.financeanalyzer.util.DateUtil;
import de.arminwolf.financeanalyzer.util.UrlUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@Controller
@RequestMapping("/reports")
public class ReportController {
@Autowired
private ReportService reportService;
@GetMapping("/")
public String reports(@ModelAttribute("account") String iban,
@ModelAttribute("month") String month,
Model model, HttpServletRequest request) {
RestTemplate restTemplate = new RestTemplate();
if (Objects.isNull(iban) || iban.isEmpty()) {
return "redirect:/upload/";
}
try {
final String fileURL = UrlUtil.getRelativeUrl(request, "/file/current");
final TransactionDAO[] response = restTemplate.getForObject(fileURL, TransactionDAO[].class);
if (Objects.isNull(response)) {
return "redirect:/upload";
} else {
reportService.createReport(response, month, iban, model);
return Constants.REPORTS;
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
return "redirect:/upload/";
}
}
}