update controllers and services, stick to crud #13

Merged
mirrerror merged 6 commits from dimas into master 2023-10-04 13:28:03 +00:00
9 changed files with 164 additions and 110 deletions

View File

@@ -38,10 +38,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@@ -4,10 +4,8 @@ import com.faf223.expensetrackerfaf.model.Expense;
import com.faf223.expensetrackerfaf.service.ExpenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -15,17 +13,47 @@ import java.util.List;
@RequestMapping("/expenses")
public class ExpenseController {
@Autowired
private ExpenseService expenseService;
private final ExpenseService expenseService;
@GetMapping("/user/{userUuid}")
public ResponseEntity<List<Expense>> getExpensesByUser(@PathVariable String userUuid) {
List<Expense> expenses = expenseService.getExpensesByUserId(userUuid);
if (!expenses.isEmpty()) {
return ResponseEntity.ok(expenses);
@Autowired
public ExpenseController(ExpenseService expenseService) {
this.expenseService = expenseService;
}
@GetMapping()
public ResponseEntity<List<Expense>> getAllExpenses() {
List<Expense> expenses = expenseService.getExpenses();
if (!expenses.isEmpty()) return ResponseEntity.ok(expenses);
else return ResponseEntity.notFound().build();
}
@PostMapping()
public ResponseEntity<Expense> createNewExpense(@RequestBody Expense expense,
BindingResult bindingResult) {
if (!bindingResult.hasErrors()) {
expenseService.createOrUpdateExpense(expense);
return ResponseEntity.ok(expense);
} else {
return ResponseEntity.notFound().build();
}
}
@PatchMapping()
public ResponseEntity<Expense> updateExpense(@RequestBody Expense expense,
BindingResult bindingResult) {
if (!bindingResult.hasErrors()) {
expenseService.createOrUpdateExpense(expense);
return ResponseEntity.ok(expense);
} else {
return ResponseEntity.notFound().build();
}
}
@GetMapping("/{userUuid}")
public ResponseEntity<List<Expense>> getExpensesByUser(@PathVariable String userUuid) {
List<Expense> expenses = expenseService.getExpensesByUserId(userUuid);
if (!expenses.isEmpty()) return ResponseEntity.ok(expenses);
else return ResponseEntity.notFound().build();
}
}

View File

@@ -4,27 +4,57 @@ import com.faf223.expensetrackerfaf.model.Income;
import com.faf223.expensetrackerfaf.service.IncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/incomes")
public class IncomeController {
@Autowired
private IncomeService incomeService;
@GetMapping("/user/{userUuid}")
public ResponseEntity<List<Income>> getIncomesByUser(@PathVariable String userUuid) {
List<Income> incomes = incomeService.getIncomesByUserId(userUuid);
if (!incomes.isEmpty()) {
return ResponseEntity.ok(incomes);
private final IncomeService incomeService;
@Autowired
public IncomeController(IncomeService incomeService) {
this.incomeService = incomeService;
}
@GetMapping()
public ResponseEntity<List<Income>> getAllIncomes() {
List<Income> incomes = incomeService.getIncomes();
if (!incomes.isEmpty()) return ResponseEntity.ok(incomes);
else return ResponseEntity.notFound().build();
}
@PostMapping()
public ResponseEntity<Income> createNewIncome(@RequestBody Income income,
BindingResult bindingResult) {
if (!bindingResult.hasErrors()) {
incomeService.createOrUpdateIncome(income);
return ResponseEntity.ok(income);
} else {
return ResponseEntity.notFound().build();
}
}
@PatchMapping()
public ResponseEntity<Income> updateIncome(@RequestBody Income income,
BindingResult bindingResult) {
if (!bindingResult.hasErrors()) {
System.out.println("amount: " + income.getAmount());
incomeService.createOrUpdateIncome(income);
return ResponseEntity.ok(income);
} else {
return ResponseEntity.notFound().build();
}
}
@GetMapping("/{userUuid}")
public ResponseEntity<List<Income>> getIncomesByUser(@PathVariable String userUuid) {
List<Income> incomes = incomeService.getIncomesByUserId(userUuid);
if (!incomes.isEmpty()) return ResponseEntity.ok(incomes);
else return ResponseEntity.notFound().build();
}
}

View File

@@ -1,66 +0,0 @@
//package com.faf223.expensetrackerfaf.controller;
//
//import com.faf223.expensetrackerfaf.model.Expense;
//import com.faf223.expensetrackerfaf.model.Income;
//import com.faf223.expensetrackerfaf.model.Role;
//import com.faf223.expensetrackerfaf.model.User;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.List;
//
//@RestController
//public class MainController {
//
// @GetMapping("/")
// public String helloWorld() {
// return "Hello, World!";
// }
//
// @GetMapping("/users/get/{id}")
// public User getUser(@PathVariable int id) {
// return new User(id, "Test", null, null, null, null, null, null);
// }
//
// @PostMapping("/users/set/{id}/name")
// public String setName(@PathVariable int id,
// @RequestParam("name") String name) {
// throw new UnsupportedOperationException("Waiting for the DB.");
// }
//
// @PostMapping("/users/set/{id}/email")
// public String setEmail(@PathVariable int id,
// @RequestParam("email") String email) {
// throw new UnsupportedOperationException("Waiting for the DB.");
// }
//
// @PostMapping("/users/set/{id}/login")
// public String setLogin(@PathVariable int id,
// @RequestParam("login") String login) {
// throw new UnsupportedOperationException("Waiting for the DB.");
// }
//
// @PostMapping("/users/set/{id}/password")
// public String setPassword(@PathVariable int id,
// @RequestParam("password") String password) {
// throw new UnsupportedOperationException("Waiting for the DB.");
// }
//
// @PostMapping("/users/set/{id}/role")
// public String setRole(@PathVariable int id,
// @RequestParam("role") Role role) {
// throw new UnsupportedOperationException("Waiting for the DB.");
// }
//
// @PostMapping("/users/set/{id}/expenses")
// public String setExpenses(@PathVariable int id,
// @RequestParam("expenses") List<Expense> expenses) {
// throw new UnsupportedOperationException("Waiting for the DB.");
// }
//
// @PostMapping("/users/set/{id}/incomes")
// public String setIncomes(@PathVariable int id,
// @RequestParam("incomes")List<Income> incomes) {
// throw new UnsupportedOperationException("Waiting for the DB.");
// }
//
//}

View File

@@ -4,26 +4,56 @@ import com.faf223.expensetrackerfaf.model.User;
import com.faf223.expensetrackerfaf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
private final UserService userService;
@GetMapping("/{userUuid}")
public ResponseEntity<User> getUser(@PathVariable String userUuid) {
User user = userService.getUserById(userUuid);
if (user != null) {
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping()
public ResponseEntity<List<User>> getAllUsers() {
List<User> users = userService.getUsers();
if (!users.isEmpty()) return ResponseEntity.ok(users);
else return ResponseEntity.notFound().build();
}
@PostMapping()
public ResponseEntity<User> createNewUser(@RequestBody User user,
BindingResult bindingResult) {
if (!bindingResult.hasErrors()) {
userService.createOrUpdateUser(user);
return ResponseEntity.ok(user);
} else {
return ResponseEntity.notFound().build();
}
}
@PatchMapping()
public ResponseEntity<User> updateUser(@RequestBody User user,
BindingResult bindingResult) {
if (!bindingResult.hasErrors()) {
userService.createOrUpdateUser(user);
return ResponseEntity.ok(user);
} else {
return ResponseEntity.notFound().build();
}
}
@GetMapping("/{userUuid}")
public ResponseEntity<User> getUser(@PathVariable String userUuid) {
User user = userService.getUserById(userUuid);
if (user != null) return ResponseEntity.ok(user);
else return ResponseEntity.notFound().build();
}
}

View File

@@ -10,10 +10,22 @@ import java.util.List;
@Service
public class ExpenseService {
private final ExpenseRepository expenseRepository;
@Autowired
private ExpenseRepository expenseRepository;
public ExpenseService(ExpenseRepository expenseRepository) {
this.expenseRepository = expenseRepository;
}
public void createOrUpdateExpense(Expense expense) {
expenseRepository.save(expense);
}
public List<Expense> getExpensesByUserId(String userUuid) {
return expenseRepository.findByUserUserUuid(userUuid);
}
public List<Expense> getExpenses() {
return expenseRepository.findAll();
}
}

View File

@@ -9,8 +9,21 @@ import java.util.List;
@Service
public class IncomeService {
private final IncomeRepository incomeRepository;
@Autowired
private IncomeRepository incomeRepository;
public IncomeService(IncomeRepository incomeRepository) {
this.incomeRepository = incomeRepository;
}
public void createOrUpdateIncome(Income income) {
incomeRepository.save(income);
}
public List<Income> getIncomes() {
return incomeRepository.findAll();
}
public List<Income> getIncomesByUserId(String userUuid) {
return incomeRepository.findByUserUserUuid(userUuid);

View File

@@ -5,11 +5,25 @@ import com.faf223.expensetrackerfaf.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
private UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void createOrUpdateUser(User user) {
userRepository.save(user);
}
public List<User> getUsers() {
return userRepository.findAll();
}
public User getUserById(String userUuid) {
return userRepository.findById(userUuid).orElse(null);

View File

@@ -1,9 +0,0 @@
# MySQL Connection Configuration
spring.datasource.url=jdbc:mariadb://194.195.242.137:3306/expensetracker
spring.datasource.username=root
spring.datasource.password=daniel$$$javatop
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
# Hibernate Configuration
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
server.port=8081