From 78fe5c67d96a11d203a29bbcf32e8d506cc4721e Mon Sep 17 00:00:00 2001 From: Pradip Mudi Date: Fri, 3 Nov 2023 08:07:40 +0530 Subject: [PATCH] exposed new APIs --- application.yml | 8 +++++- build.gradle | 2 +- .../ExpenseManagementController.java | 18 ++++++++++--- .../expensys/model/response/ReportInfo.java | 3 +++ .../com/expensys/service/ExpenseService.java | 18 +++++++++---- .../main/ExpenseManagementService.java | 9 +++++-- src/main/resources/logback.xml | 26 ------------------- src/main/resources/logback5.xml | 26 +++++++++++++++++++ src/main/resources/static/css/report.css | 2 +- 9 files changed, 72 insertions(+), 40 deletions(-) delete mode 100644 src/main/resources/logback.xml create mode 100644 src/main/resources/logback5.xml diff --git a/application.yml b/application.yml index 62dc9b8..d3b8c04 100644 --- a/application.yml +++ b/application.yml @@ -2,6 +2,8 @@ server: port: 8080 spring: + application: + name: expensys datasource: url: jdbc:mysql://localhost:3306/expensys username: root @@ -19,4 +21,8 @@ spring: flyway: baseline-on-migrate: true resources: - add-mappings: true \ No newline at end of file + add-mappings: true + +logging: + level: + org.springframework.web: DEBUG diff --git a/build.gradle b/build.gradle index c9dfe58..3a177cc 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.1.5' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'mysql:mysql-connector-java:8.0.27' - testImplementation 'ch.qos.logback:logback-classic:1.4.11' +// testImplementation 'ch.qos.logback:logback-classic:1.4.11' implementation 'io.swagger.core.v3:swagger-annotations:2.2.14' implementation 'org.apache.commons:commons-text:1.10.0' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' diff --git a/src/main/java/com/expensys/controller/ExpenseManagementController.java b/src/main/java/com/expensys/controller/ExpenseManagementController.java index b0b590d..4dcf280 100644 --- a/src/main/java/com/expensys/controller/ExpenseManagementController.java +++ b/src/main/java/com/expensys/controller/ExpenseManagementController.java @@ -9,10 +9,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.time.LocalDate; import java.util.List; @RestController @@ -27,18 +29,26 @@ public ExpenseManagementController(ExpenseManagementService expenseManagementSer } @PostMapping("/save") - public ResponseEntity saveExpense(@RequestBody NewExpense newExpense){ + 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){ + 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; + public ResponseEntity> getExpenseByMonth(@PathVariable Month month) { + return new ResponseEntity<>(expenseManagementService.getExpensesByMonth(month), HttpStatus.OK); + } + + @GetMapping + public ResponseEntity> getExpenseByDateRange( + @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate, + @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate + ) { + return new ResponseEntity<>(expenseManagementService.getExpenseByDateRange(startDate, endDate), HttpStatus.OK); } } diff --git a/src/main/java/com/expensys/model/response/ReportInfo.java b/src/main/java/com/expensys/model/response/ReportInfo.java index c6875a6..7f1f472 100644 --- a/src/main/java/com/expensys/model/response/ReportInfo.java +++ b/src/main/java/com/expensys/model/response/ReportInfo.java @@ -1,6 +1,7 @@ package com.expensys.model.response; import com.expensys.model.enums.Category; +import com.fasterxml.jackson.annotation.JsonIgnore; import java.util.Objects; @@ -44,7 +45,9 @@ public void setSpentBy(String spentBy) { this.spentBy = spentBy; } + @JsonIgnore public String getMainCategorySpentByKey(){ return mainCategory +"_"+spentBy;} + @JsonIgnore public String getSubCategorySpentByKey(){ return subCategory +"_"+spentBy;} public static class Builder { diff --git a/src/main/java/com/expensys/service/ExpenseService.java b/src/main/java/com/expensys/service/ExpenseService.java index 63b6a6f..934ba53 100644 --- a/src/main/java/com/expensys/service/ExpenseService.java +++ b/src/main/java/com/expensys/service/ExpenseService.java @@ -32,6 +32,11 @@ public ExpenseService(CategoryMappingService categoryMappingService, ExpenseRepo } public List getExpensesByMonth(Month month, ReportRequest reportRequest) { + List expenseEntityList = getExpenseEntitiesByMonth(month); + return prepareExpenseListFromExpenseEntityList(expenseEntityList, reportRequest); + } + + public List getExpenseEntitiesByMonth(Month month){ int year = LocalDate.now().getYear(); // You can use the current year or specify a year as needed DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); @@ -40,8 +45,7 @@ public List getExpensesByMonth(Month month, ReportRequest reportRequest // Create the LocalDate for the end of the month LocalDate dateEnd = LocalDate.parse(dateStart.plusMonths(1).minusDays(1).format(formatter)); - List expenseEntityList = expenseRepository.findByDateBetween(dateStart, dateEnd); - return prepareExpenseListFromExpenseEntityList(expenseEntityList, reportRequest); + return expenseRepository.findByDateBetween(dateStart, dateEnd); } private List prepareExpenseListFromExpenseEntityList(List expenseEntityList, ReportRequest reportRequest) { @@ -52,9 +56,9 @@ private List prepareExpenseListFromExpenseEntityList(List {}",expense); - } +// for (Expense expense : expenseList) { +// logger.info("expense -> {}",expense); +// } return expenseList; } @@ -62,6 +66,10 @@ List getAllExpenses() { return prepareExpenseListFromExpenseEntityList(expenseRepository.findAll(), null); } + public List getExpenseByDateRange(LocalDate startDate, LocalDate endDate){ + return expenseRepository.findByDateBetween(startDate, endDate); + } + public void saveExpense(NewExpense newExpense) { expenseRepository.save(prepareExpenseEntity(newExpense)); } diff --git a/src/main/java/com/expensys/service/main/ExpenseManagementService.java b/src/main/java/com/expensys/service/main/ExpenseManagementService.java index e7eb134..f50dd1c 100644 --- a/src/main/java/com/expensys/service/main/ExpenseManagementService.java +++ b/src/main/java/com/expensys/service/main/ExpenseManagementService.java @@ -1,6 +1,7 @@ package com.expensys.service.main; 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; @@ -39,8 +40,12 @@ public boolean addExpense(NewExpense newExpense) { return true; } - public List getAllExpenses(LocalDate startDate, LocalDate endDate){ - return null; + public List getExpenseByDateRange(LocalDate startDate, LocalDate endDate){ + return expenseService.getExpenseByDateRange(startDate, endDate); + } + + public List getExpensesByMonth(Month month){ + return expenseService.getExpenseEntitiesByMonth(month); } } diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml deleted file mode 100644 index 0c0e5db..0000000 --- a/src/main/resources/logback.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} , [RI: %X{requestID},expensys] - %msg%n - - - - - logs/application.log - - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} , [RI: %X{requestID},expensys] - %msg%n - - - logs/application.%d{yyyy-MM-dd}.%i.log - 10MB - 7 - - - - - - - - - - \ No newline at end of file diff --git a/src/main/resources/logback5.xml b/src/main/resources/logback5.xml new file mode 100644 index 0000000..46ba2df --- /dev/null +++ b/src/main/resources/logback5.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/static/css/report.css b/src/main/resources/static/css/report.css index a9117c0..eac3129 100644 --- a/src/main/resources/static/css/report.css +++ b/src/main/resources/static/css/report.css @@ -124,7 +124,7 @@ th, td { padding: 10px; white-space: nowrap; /* Prevent text wrapping */ - font-size: 9px; /* Decrease the font size for smaller screens */ + font-size: 8.5px; /* Decrease the font size for smaller screens */ } }