Skip to content

Commit

Permalink
exposed new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
pradipmudi committed Nov 3, 2023
1 parent 749177c commit 78fe5c6
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 40 deletions.
8 changes: 7 additions & 1 deletion application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ server:
port: 8080

spring:
application:
name: expensys
datasource:
url: jdbc:mysql://localhost:3306/expensys
username: root
Expand All @@ -19,4 +21,8 @@ spring:
flyway:
baseline-on-migrate: true
resources:
add-mappings: true
add-mappings: true

logging:
level:
org.springframework.web: DEBUG
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,18 +29,26 @@ public ExpenseManagementController(ExpenseManagementService expenseManagementSer
}

@PostMapping("/save")
public ResponseEntity<HttpStatus> saveExpense(@RequestBody NewExpense newExpense){
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){
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;
public ResponseEntity<List<ExpenseEntity>> getExpenseByMonth(@PathVariable Month month) {
return new ResponseEntity<>(expenseManagementService.getExpensesByMonth(month), HttpStatus.OK);
}

@GetMapping
public ResponseEntity<List<ExpenseEntity>> 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);
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/expensys/model/response/ReportInfo.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/expensys/service/ExpenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public ExpenseService(CategoryMappingService categoryMappingService, ExpenseRepo
}

public List<Expense> getExpensesByMonth(Month month, ReportRequest reportRequest) {
List<ExpenseEntity> expenseEntityList = getExpenseEntitiesByMonth(month);
return prepareExpenseListFromExpenseEntityList(expenseEntityList, reportRequest);
}

public List<ExpenseEntity> 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");

Expand All @@ -40,8 +45,7 @@ public List<Expense> 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<ExpenseEntity> expenseEntityList = expenseRepository.findByDateBetween(dateStart, dateEnd);
return prepareExpenseListFromExpenseEntityList(expenseEntityList, reportRequest);
return expenseRepository.findByDateBetween(dateStart, dateEnd);
}

private List<Expense> prepareExpenseListFromExpenseEntityList(List<ExpenseEntity> expenseEntityList, ReportRequest reportRequest) {
Expand All @@ -52,16 +56,20 @@ 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);
}
// for (Expense expense : expenseList) {
// logger.info("expense -> {}",expense);
// }
return expenseList;
}

List<Expense> getAllExpenses() {
return prepareExpenseListFromExpenseEntityList(expenseRepository.findAll(), null);
}

public List<ExpenseEntity> getExpenseByDateRange(LocalDate startDate, LocalDate endDate){
return expenseRepository.findByDateBetween(startDate, endDate);
}

public void saveExpense(NewExpense newExpense) {
expenseRepository.save(prepareExpenseEntity(newExpense));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,8 +40,12 @@ public boolean addExpense(NewExpense newExpense) {
return true;
}

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

public List<ExpenseEntity> getExpensesByMonth(Month month){
return expenseService.getExpenseEntitiesByMonth(month);
}

}
26 changes: 0 additions & 26 deletions src/main/resources/logback.xml

This file was deleted.

26 changes: 26 additions & 0 deletions src/main/resources/logback5.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<configuration>
<!-- <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">-->
<!-- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">-->
<!-- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} , [RI: %X{requestID},expensys] - %msg%n</pattern>-->
<!-- </encoder>-->
<!-- </appender>-->

<!-- <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">-->
<!-- <file>logs/application.log</file>-->
<!-- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">-->
<!-- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} , [RI: %X{requestID},expensys] - %msg%n</pattern>-->
<!-- </encoder>-->
<!-- <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">-->
<!-- <fileNamePattern>logs/application.%d{yyyy-MM-dd}.%i.log</fileNamePattern>-->
<!-- <maxFileSize>10MB</maxFileSize>-->
<!-- <maxHistory>7</maxHistory>-->
<!-- </rollingPolicy>-->
<!-- </appender>-->

<!-- <logger name="com.expensys" level="INFO" />-->

<!-- <root level="INFO">-->
<!-- <appender-ref ref="CONSOLE" />-->
<!-- <appender-ref ref="FILE" />-->
<!-- </root>-->
</configuration>
2 changes: 1 addition & 1 deletion src/main/resources/static/css/report.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
}

0 comments on commit 78fe5c6

Please sign in to comment.