Skip to content

Commit

Permalink
bug fix on monthly report generation, renamed Report to MonthlyReport…
Browse files Browse the repository at this point in the history
… and main controller changes
  • Loading branch information
pradipmudi committed Nov 1, 2023
1 parent dee92a6 commit 749177c
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -24,4 +30,15 @@ public ExpenseManagementController(ExpenseManagementService expenseManagementSer
public ResponseEntity<HttpStatus> saveExpense(@RequestBody NewExpense newExpense){
return new ResponseEntity<>(expenseManagementService.addExpense(newExpense) ? HttpStatus.OK : HttpStatus.INTERNAL_SERVER_ERROR);
}

@GetMapping("/report")
public ResponseEntity<List<MonthlyReport>> getReport(@ModelAttribute ReportRequest reportRequest){
List<MonthlyReport> monthlyReportList = expenseManagementService.getReport(reportRequest);
return new ResponseEntity<>(monthlyReportList, HttpStatus.OK);
}

@GetMapping("/{month}")
public ResponseEntity<ExpenseEntity> getExpenseByMonth(@PathVariable Month month){
return null;
}
}
17 changes: 2 additions & 15 deletions src/main/java/com/expensys/controller/ReportController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,11 +15,4 @@ public ReportController(ExpenseManagementService expenseManagementService) {
this.expenseManagementService = expenseManagementService;
}

@GetMapping
public ResponseEntity<List<Report>> getReport(@ModelAttribute ReportRequest reportRequest){
List<Report> reportList = expenseManagementService.getReport(reportRequest);
return new ResponseEntity<>(reportList, HttpStatus.OK);
}


}
28 changes: 12 additions & 16 deletions src/main/java/com/expensys/helper/ExpenseToReportConvertor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,11 +18,12 @@

public class ExpenseToReportConvertor {
private static final Logger logger = LoggerFactory.getLogger(ExpenseToReportConvertor.class);

private ExpenseToReportConvertor() {
// Initialization code
}

public List<Report> prepareReportListFromExpenseList(List<Expense> expenseList, ReportRequest reportRequest) {
public List<MonthlyReport> prepareReportListFromExpenseList(List<Expense> expenseList, ReportRequest reportRequest) {
Map<Month, List<ReportInfo>> monthReportInfoMap;

monthReportInfoMap = prepareReportInfoListFromExpenseListByMonth(reportRequest, expenseList);
Expand All @@ -42,33 +43,28 @@ private Map<Month, List<ReportInfo>> prepareReportInfoListFromExpenseListByMonth
.build();

Month currentMonth = expense.getMonth();
List<ReportInfo> currentReportInfoList = monthReportInfoMap.get(currentMonth);
List<ReportInfo> 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<Report> prepareReportInfoFromReportInfoByMonth(Map<Month, List<ReportInfo>> monthReportInfoMap, ReportRequest reportRequest) {
List<Report> reportList = new ArrayList<>();
private List<MonthlyReport> prepareReportInfoFromReportInfoByMonth(Map<Month, List<ReportInfo>> monthReportInfoMap, ReportRequest reportRequest) {
List<MonthlyReport> monthlyReportList = new ArrayList<>();

for (Map.Entry<Month, List<ReportInfo>> 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<ReportInfo> processReportInfoList(List<ReportInfo> reportInfoList, ReportRequest reportRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import java.util.List;

public class Report {
public class MonthlyReport {
Month month;
Double totalSpendings;
List<ReportInfo> reportInfo;

public Report(Month month, List<ReportInfo> reportInfo) {
public MonthlyReport(Month month, List<ReportInfo> reportInfo) {
this.month = month;
this.reportInfo = reportInfo;
this.totalSpendings = calculateTotalSpendings();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/expensys/service/ExpenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ private List<Expense> prepareExpenseListFromExpenseEntityList(List<ExpenseEntity
Expense expense = new Expense(Month.valueOf(String.valueOf(expenseEntity.getDate().getMonth())), expenseEntity.getItem(), category, expenseEntity.getSpent(), expenseEntity.getSpentBy());
expenseList.add(expense);
}
for (Expense expense : expenseList) {
logger.info("expense -> {}",expense);
}
return expenseList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Report> prepareReport(ReportRequest reportRequest, List<Expense> expenseList);
List<MonthlyReport> prepareReport(ReportRequest reportRequest, List<Expense> expenseList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +25,7 @@ public MainCategoryReportService(CategoryMappingRepository categoryMappingReposi
}

@Override
public List<Report> prepareReport(ReportRequest reportRequest, List<Expense> expenseList) {
public List<MonthlyReport> prepareReport(ReportRequest reportRequest, List<Expense> expenseList) {

expenseList = expenseList.stream().filter(expense -> MAIN_CATEGORIES.contains(expense.getCategory())).toList();
return ExpenseToReportConvertor.getInstance().prepareReportListFromExpenseList(expenseList, reportRequest);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/expensys/service/ReportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,7 @@ public ReportService(MainCategoryReportService mainCategoryReportService, SubCat
this.expenseService = expenseService;
}

public List<Report> getReport(ReportRequest reportRequest) {
public List<MonthlyReport> getReport(ReportRequest reportRequest) {
List<Expense> expenseList;
if (Month.ALL.equals(reportRequest.getMonth())) {
expenseList = expenseService.getAllExpensesByYear(reportRequest.getYear() == 0 ? LocalDate.now().getYear() : reportRequest.getYear(), reportRequest);
Expand All @@ -38,7 +38,7 @@ public List<Report> getReport(ReportRequest reportRequest) {
return prepareReportFromExpenses(reportRequest, expenseList);
}

private List<Report> prepareReportFromExpenses(ReportRequest reportRequest, List<Expense> expenseList) {
private List<MonthlyReport> prepareReportFromExpenses(ReportRequest reportRequest, List<Expense> expenseList) {
if (Category.MAIN.equals(reportRequest.getCategory())) {
return mainCategoryReportService.prepareReport(reportRequest, expenseList);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,9 +25,7 @@ public SubCategoryReportService(CategoryMappingRepository categoryMappingReposit
}

@Override
public List<Report> prepareReport(ReportRequest reportRequest, List<Expense> expenseList) {
// for (Expense expense : expenseList)
// logger.info("expense -> {} ", expense);
public List<MonthlyReport> prepareReport(ReportRequest reportRequest, List<Expense> expenseList) {
expenseList = expenseList.stream().filter(expense -> SUB_CATEGORIES.contains(expense.getCategory())).toList();
return ExpenseToReportConvertor.getInstance().prepareReportListFromExpenseList(expenseList, reportRequest);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
Expand All @@ -24,7 +26,7 @@ public ExpenseManagementService(ReportService reportService, ExpenseService expe
this.expenseService = expenseService;
}

public List<Report> getReport(ReportRequest reportRequest) {
public List<MonthlyReport> getReport(ReportRequest reportRequest) {
return reportService.getReport(reportRequest);
}

Expand All @@ -37,4 +39,8 @@ public boolean addExpense(NewExpense newExpense) {
return true;
}

public List<ExpenseEntity> getAllExpenses(LocalDate startDate, LocalDate endDate){
return null;
}

}
2 changes: 1 addition & 1 deletion src/main/resources/static/js/reportScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 749177c

Please sign in to comment.