Skip to content

Commit 898d2cf

Browse files
authored
Add files via upload
1 parent 42bdd25 commit 898d2cf

File tree

9 files changed

+472
-0
lines changed

9 files changed

+472
-0
lines changed

pom.xml

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.3.4</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.javatechie</groupId>
12+
<artifactId>budget-buddy</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>budget-buddy</name>
15+
<description>Demo project for Spring Boot</description>
16+
<url/>
17+
<licenses>
18+
<license/>
19+
</licenses>
20+
<developers>
21+
<developer/>
22+
</developers>
23+
<scm>
24+
<connection/>
25+
<developerConnection/>
26+
<tag/>
27+
<url/>
28+
</scm>
29+
<properties>
30+
<java.version>21</java.version>
31+
</properties>
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-data-jpa</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-web</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>gg.jte</groupId>
43+
<artifactId>jte</artifactId>
44+
<version>3.1.12</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>gg.jte</groupId>
48+
<artifactId>jte-spring-boot-starter-3</artifactId>
49+
<version>3.1.12</version>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>com.mysql</groupId>
54+
<artifactId>mysql-connector-j</artifactId>
55+
<scope>runtime</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.projectlombok</groupId>
59+
<artifactId>lombok</artifactId>
60+
<optional>true</optional>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-test</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<groupId>gg.jte</groupId>
73+
<artifactId>jte-maven-plugin</artifactId>
74+
<version>3.1.12</version>
75+
<executions>
76+
<execution>
77+
<id>jte-generate</id>
78+
<phase>generate-sources</phase>
79+
<goals>
80+
<goal>generate</goal>
81+
</goals>
82+
<configuration>
83+
<sourceDirectory>${project.basedir}/src/main/jte</sourceDirectory>
84+
<contentType>Html</contentType>
85+
<binaryStaticContent>true</binaryStaticContent>
86+
<targetResourceDirectory>${project.build.outputDirectory}</targetResourceDirectory>
87+
</configuration>
88+
</execution>
89+
</executions>
90+
</plugin>
91+
<plugin>
92+
<groupId>org.springframework.boot</groupId>
93+
<artifactId>spring-boot-maven-plugin</artifactId>
94+
<configuration>
95+
<excludes>
96+
<exclude>
97+
<groupId>org.projectlombok</groupId>
98+
<artifactId>lombok</artifactId>
99+
</exclude>
100+
</excludes>
101+
</configuration>
102+
</plugin>
103+
</plugins>
104+
</build>
105+
106+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.javatechie;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class BudgetBuddyApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(BudgetBuddyApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.javatechie.controller;
2+
3+
import com.javatechie.entity.Expense;
4+
import com.javatechie.service.ExpenseService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
@Controller
11+
@RequestMapping("/")
12+
public class ExpenseController {
13+
14+
@Autowired
15+
private ExpenseService expenseService;
16+
17+
// Display the main page
18+
@GetMapping
19+
public String index(Model model) {
20+
model.addAttribute("expenses", expenseService.getAllExpenses());
21+
model.addAttribute("expense", new Expense()); // For adding a new expense
22+
return "index";
23+
}
24+
25+
// Add or update an expense
26+
@PostMapping("/save")
27+
public String saveExpense(@ModelAttribute Expense expense) {
28+
if (expense.getId() != null) {
29+
// Update existing expense
30+
Expense existingExpense = expenseService.getExpenseById(expense.getId());
31+
if (existingExpense != null) {
32+
existingExpense.setDescription(expense.getDescription());
33+
existingExpense.setAmount(expense.getAmount());
34+
existingExpense.setDate(expense.getDate());
35+
existingExpense.setCategory(expense.getCategory());
36+
expenseService.saveExpense(existingExpense);
37+
}
38+
} else {
39+
// Add new expense
40+
expenseService.saveExpense(expense);
41+
}
42+
return "redirect:/";
43+
}
44+
45+
// Display form to edit an existing expense
46+
@GetMapping("/edit/{id}")
47+
public String editForm(@PathVariable Long id, Model model) {
48+
Expense expense = expenseService.getExpenseById(id);
49+
if (expense != null) {
50+
model.addAttribute("expense", expense); // Populate the model with the existing expense
51+
} else {
52+
model.addAttribute("expense", new Expense()); // Fallback to a new expense if not found
53+
}
54+
model.addAttribute("expenses", expenseService.getAllExpenses()); // List of all expenses
55+
return "index"; // Return the same index page with the form populated for editing
56+
}
57+
58+
59+
60+
// Delete an expense
61+
@GetMapping("/delete/{id}")
62+
public String deleteExpense(@PathVariable Long id) {
63+
expenseService.deleteExpense(id);
64+
return "redirect:/";
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.javatechie.entity;
2+
3+
import jakarta.persistence.*;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.time.LocalDate;
9+
10+
@Entity
11+
@Data
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
public class Expense {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
public Long id; // Unique identifier for the expense
19+
public String description; // Description of the expense
20+
public double amount; // Amount spent
21+
public String date; // Date of the expense
22+
public String category; // Category of the expense (e.g., Food, Transport, etc.)
23+
// public String paymentMethod; // Payment method (e.g., Cash, Credit Card)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.javatechie.respository;
2+
3+
import com.javatechie.entity.Expense;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface ExpenseRepository extends JpaRepository<Expense,Long> {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.javatechie.service;
2+
3+
import com.javatechie.entity.Expense;
4+
import com.javatechie.respository.ExpenseRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
import java.util.List;
9+
10+
@Service
11+
public class ExpenseService {
12+
13+
@Autowired
14+
private ExpenseRepository expenseRepository;
15+
16+
public List<Expense> getAllExpenses() {
17+
return expenseRepository.findAll();
18+
}
19+
20+
public Expense saveExpense(Expense expense) {
21+
return expenseRepository.save(expense);
22+
}
23+
24+
public void deleteExpense(Long id) {
25+
expenseRepository.deleteById(id);
26+
}
27+
28+
public Expense getExpenseById(Long id) {
29+
return expenseRepository.findById(id).orElse(null);
30+
}
31+
}

0 commit comments

Comments
 (0)