diff --git a/src/main/java/com/expensys/controller/ExpenseManagementController.java b/src/main/java/com/expensys/controller/ExpenseManagementController.java index 680d0b1..b0b590d 100644 --- a/src/main/java/com/expensys/controller/ExpenseManagementController.java +++ b/src/main/java/com/expensys/controller/ExpenseManagementController.java @@ -1,6 +1,10 @@ package com.expensys.controller; +import com.expensys.entity.ExpenseEntity; +import com.expensys.model.enums.Month; import com.expensys.model.request.NewExpense; +import com.expensys.model.request.ReportRequest; +import com.expensys.model.response.MonthlyReport; import com.expensys.service.main.ExpenseManagementService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,6 +13,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @RequestMapping("/expense") public class ExpenseManagementController { @@ -24,4 +30,15 @@ public ExpenseManagementController(ExpenseManagementService expenseManagementSer public ResponseEntity saveExpense(@RequestBody NewExpense newExpense){ return new ResponseEntity<>(expenseManagementService.addExpense(newExpense) ? HttpStatus.OK : HttpStatus.INTERNAL_SERVER_ERROR); } + + @GetMapping("/report") + public ResponseEntity> getReport(@ModelAttribute ReportRequest reportRequest){ + List monthlyReportList = expenseManagementService.getReport(reportRequest); + return new ResponseEntity<>(monthlyReportList, HttpStatus.OK); + } + + @GetMapping("/{month}") + public ResponseEntity getExpenseByMonth(@PathVariable Month month){ + return null; + } } diff --git a/src/main/java/com/expensys/controller/ReportController.java b/src/main/java/com/expensys/controller/ReportController.java index ff07a4c..8b78f0d 100644 --- a/src/main/java/com/expensys/controller/ReportController.java +++ b/src/main/java/com/expensys/controller/ReportController.java @@ -1,18 +1,12 @@ package com.expensys.controller; -import com.expensys.model.request.ReportRequest; -import com.expensys.model.response.Report; import com.expensys.service.main.ExpenseManagementService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import java.util.List; +import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("/report") +//@RequestMapping("/report") public class ReportController { Logger logger = LoggerFactory.getLogger(ReportController.class); private final ExpenseManagementService expenseManagementService; @@ -21,11 +15,4 @@ public ReportController(ExpenseManagementService expenseManagementService) { this.expenseManagementService = expenseManagementService; } - @GetMapping - public ResponseEntity> getReport(@ModelAttribute ReportRequest reportRequest){ - List reportList = expenseManagementService.getReport(reportRequest); - return new ResponseEntity<>(reportList, HttpStatus.OK); - } - - } diff --git a/src/main/java/com/expensys/helper/ExpenseToReportConvertor.java b/src/main/java/com/expensys/helper/ExpenseToReportConvertor.java index 8eefa18..6f377cb 100644 --- a/src/main/java/com/expensys/helper/ExpenseToReportConvertor.java +++ b/src/main/java/com/expensys/helper/ExpenseToReportConvertor.java @@ -6,7 +6,7 @@ import com.expensys.model.enums.Month; import com.expensys.model.enums.SpentBy; import com.expensys.model.request.ReportRequest; -import com.expensys.model.response.Report; +import com.expensys.model.response.MonthlyReport; import com.expensys.model.response.ReportInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,11 +18,12 @@ public class ExpenseToReportConvertor { private static final Logger logger = LoggerFactory.getLogger(ExpenseToReportConvertor.class); + private ExpenseToReportConvertor() { // Initialization code } - public List prepareReportListFromExpenseList(List expenseList, ReportRequest reportRequest) { + public List prepareReportListFromExpenseList(List expenseList, ReportRequest reportRequest) { Map> monthReportInfoMap; monthReportInfoMap = prepareReportInfoListFromExpenseListByMonth(reportRequest, expenseList); @@ -42,33 +43,28 @@ private Map> prepareReportInfoListFromExpenseListByMonth .build(); Month currentMonth = expense.getMonth(); - List currentReportInfoList = monthReportInfoMap.get(currentMonth); + List currentReportInfoList = monthReportInfoMap.computeIfAbsent(currentMonth, k -> new ArrayList<>()); + + currentReportInfoList.add(reportInfo); - if (currentReportInfoList == null) { - currentReportInfoList = new ArrayList<>(); - monthReportInfoMap.put(currentMonth, currentReportInfoList); - } else { - currentReportInfoList.add(reportInfo); - monthReportInfoMap.put(currentMonth, currentReportInfoList); - } } return monthReportInfoMap; } - private List prepareReportInfoFromReportInfoByMonth(Map> monthReportInfoMap, ReportRequest reportRequest) { - List reportList = new ArrayList<>(); + private List prepareReportInfoFromReportInfoByMonth(Map> monthReportInfoMap, ReportRequest reportRequest) { + List monthlyReportList = new ArrayList<>(); for (Map.Entry> monthEntry : monthReportInfoMap.entrySet()) { Month month = monthEntry.getKey(); - Report currentReport = new Report(month, processReportInfoList(monthReportInfoMap.get(month), reportRequest)); - reportList.add(currentReport); + MonthlyReport currentMonthlyReport = new MonthlyReport(month, processReportInfoList(monthReportInfoMap.get(month), reportRequest)); + monthlyReportList.add(currentMonthlyReport); } // Sort the reportList based on the month's integer values in descending order - Collections.sort(reportList, (r1, r2) -> Integer.compare(Integer.valueOf(r2.getMonth().getMonthValue()), Integer.valueOf(r1.getMonth().getMonthValue()))); + Collections.sort(monthlyReportList, (r1, r2) -> Integer.compare(Integer.valueOf(r2.getMonth().getMonthValue()), Integer.valueOf(r1.getMonth().getMonthValue()))); - return reportList; + return monthlyReportList; } public List processReportInfoList(List reportInfoList, ReportRequest reportRequest) { diff --git a/src/main/java/com/expensys/model/response/Report.java b/src/main/java/com/expensys/model/response/MonthlyReport.java similarity index 93% rename from src/main/java/com/expensys/model/response/Report.java rename to src/main/java/com/expensys/model/response/MonthlyReport.java index ce675e6..5fc406c 100644 --- a/src/main/java/com/expensys/model/response/Report.java +++ b/src/main/java/com/expensys/model/response/MonthlyReport.java @@ -4,12 +4,12 @@ import java.util.List; -public class Report { +public class MonthlyReport { Month month; Double totalSpendings; List reportInfo; - public Report(Month month, List reportInfo) { + public MonthlyReport(Month month, List reportInfo) { this.month = month; this.reportInfo = reportInfo; this.totalSpendings = calculateTotalSpendings(); diff --git a/src/main/java/com/expensys/service/ExpenseService.java b/src/main/java/com/expensys/service/ExpenseService.java index 3891670..63b6a6f 100644 --- a/src/main/java/com/expensys/service/ExpenseService.java +++ b/src/main/java/com/expensys/service/ExpenseService.java @@ -52,6 +52,9 @@ private List prepareExpenseListFromExpenseEntityList(List {}",expense); + } return expenseList; } diff --git a/src/main/java/com/expensys/service/ICategoryReportService.java b/src/main/java/com/expensys/service/ICategoryReportService.java index 4f4e81d..b9e4d1f 100644 --- a/src/main/java/com/expensys/service/ICategoryReportService.java +++ b/src/main/java/com/expensys/service/ICategoryReportService.java @@ -2,10 +2,10 @@ import com.expensys.model.Expense; import com.expensys.model.request.ReportRequest; -import com.expensys.model.response.Report; +import com.expensys.model.response.MonthlyReport; import java.util.List; public interface ICategoryReportService { - List prepareReport(ReportRequest reportRequest, List expenseList); + List prepareReport(ReportRequest reportRequest, List expenseList); } diff --git a/src/main/java/com/expensys/service/MainCategoryReportService.java b/src/main/java/com/expensys/service/MainCategoryReportService.java index 58a253f..e170835 100644 --- a/src/main/java/com/expensys/service/MainCategoryReportService.java +++ b/src/main/java/com/expensys/service/MainCategoryReportService.java @@ -3,7 +3,7 @@ import com.expensys.helper.ExpenseToReportConvertor; import com.expensys.model.Expense; import com.expensys.model.request.ReportRequest; -import com.expensys.model.response.Report; +import com.expensys.model.response.MonthlyReport; import com.expensys.repository.CategoryMappingRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,7 +25,7 @@ public MainCategoryReportService(CategoryMappingRepository categoryMappingReposi } @Override - public List prepareReport(ReportRequest reportRequest, List expenseList) { + public List prepareReport(ReportRequest reportRequest, List expenseList) { expenseList = expenseList.stream().filter(expense -> MAIN_CATEGORIES.contains(expense.getCategory())).toList(); return ExpenseToReportConvertor.getInstance().prepareReportListFromExpenseList(expenseList, reportRequest); diff --git a/src/main/java/com/expensys/service/ReportService.java b/src/main/java/com/expensys/service/ReportService.java index f20df27..19bd42a 100644 --- a/src/main/java/com/expensys/service/ReportService.java +++ b/src/main/java/com/expensys/service/ReportService.java @@ -4,7 +4,7 @@ import com.expensys.model.enums.Category; import com.expensys.model.enums.Month; import com.expensys.model.request.ReportRequest; -import com.expensys.model.response.Report; +import com.expensys.model.response.MonthlyReport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -27,7 +27,7 @@ public ReportService(MainCategoryReportService mainCategoryReportService, SubCat this.expenseService = expenseService; } - public List getReport(ReportRequest reportRequest) { + public List getReport(ReportRequest reportRequest) { List expenseList; if (Month.ALL.equals(reportRequest.getMonth())) { expenseList = expenseService.getAllExpensesByYear(reportRequest.getYear() == 0 ? LocalDate.now().getYear() : reportRequest.getYear(), reportRequest); @@ -38,7 +38,7 @@ public List getReport(ReportRequest reportRequest) { return prepareReportFromExpenses(reportRequest, expenseList); } - private List prepareReportFromExpenses(ReportRequest reportRequest, List expenseList) { + private List prepareReportFromExpenses(ReportRequest reportRequest, List expenseList) { if (Category.MAIN.equals(reportRequest.getCategory())) { return mainCategoryReportService.prepareReport(reportRequest, expenseList); } else { diff --git a/src/main/java/com/expensys/service/SubCategoryReportService.java b/src/main/java/com/expensys/service/SubCategoryReportService.java index 4793bbd..58ff8b3 100644 --- a/src/main/java/com/expensys/service/SubCategoryReportService.java +++ b/src/main/java/com/expensys/service/SubCategoryReportService.java @@ -3,7 +3,7 @@ import com.expensys.helper.ExpenseToReportConvertor; import com.expensys.model.Expense; import com.expensys.model.request.ReportRequest; -import com.expensys.model.response.Report; +import com.expensys.model.response.MonthlyReport; import com.expensys.repository.CategoryMappingRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,9 +25,7 @@ public SubCategoryReportService(CategoryMappingRepository categoryMappingReposit } @Override - public List prepareReport(ReportRequest reportRequest, List expenseList) { -// for (Expense expense : expenseList) -// logger.info("expense -> {} ", expense); + public List prepareReport(ReportRequest reportRequest, List expenseList) { expenseList = expenseList.stream().filter(expense -> SUB_CATEGORIES.contains(expense.getCategory())).toList(); return ExpenseToReportConvertor.getInstance().prepareReportListFromExpenseList(expenseList, reportRequest); } diff --git a/src/main/java/com/expensys/service/main/ExpenseManagementService.java b/src/main/java/com/expensys/service/main/ExpenseManagementService.java index 66ca810..e7eb134 100644 --- a/src/main/java/com/expensys/service/main/ExpenseManagementService.java +++ b/src/main/java/com/expensys/service/main/ExpenseManagementService.java @@ -1,8 +1,9 @@ package com.expensys.service.main; +import com.expensys.entity.ExpenseEntity; import com.expensys.model.request.NewExpense; import com.expensys.model.request.ReportRequest; -import com.expensys.model.response.Report; +import com.expensys.model.response.MonthlyReport; import com.expensys.service.ExpenseService; import com.expensys.service.ReportService; import org.slf4j.Logger; @@ -10,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.time.LocalDate; import java.util.List; @Service @@ -24,7 +26,7 @@ public ExpenseManagementService(ReportService reportService, ExpenseService expe this.expenseService = expenseService; } - public List getReport(ReportRequest reportRequest) { + public List getReport(ReportRequest reportRequest) { return reportService.getReport(reportRequest); } @@ -37,4 +39,8 @@ public boolean addExpense(NewExpense newExpense) { return true; } + public List getAllExpenses(LocalDate startDate, LocalDate endDate){ + return null; + } + } diff --git a/src/main/resources/static/js/reportScript.js b/src/main/resources/static/js/reportScript.js index 87752cb..0bd40d6 100644 --- a/src/main/resources/static/js/reportScript.js +++ b/src/main/resources/static/js/reportScript.js @@ -14,7 +14,7 @@ const selectedSpentBy = spentByFilter.value; // Make an API request based on selected filters - const apiUrl = `http://localhost:8080/report?month=${selectedMonth}&category=${selectedCategory}&spentBy=${selectedSpentBy}`; + const apiUrl = `http://localhost:8080/expense/report?month=${selectedMonth}&category=${selectedCategory}&spentBy=${selectedSpentBy}`; const response = await fetch(apiUrl); if (!response.ok) {