update controllers and services
This commit is contained in:
@@ -49,7 +49,7 @@ public class ExpenseController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/user/{userUuid}")
|
@GetMapping("/{userUuid}")
|
||||||
public ResponseEntity<List<Expense>> getExpensesByUser(@PathVariable String userUuid) {
|
public ResponseEntity<List<Expense>> getExpensesByUser(@PathVariable String userUuid) {
|
||||||
List<Expense> expenses = expenseService.getExpensesByUserId(userUuid);
|
List<Expense> expenses = expenseService.getExpensesByUserId(userUuid);
|
||||||
if (!expenses.isEmpty()) return ResponseEntity.ok(expenses);
|
if (!expenses.isEmpty()) return ResponseEntity.ok(expenses);
|
||||||
|
|||||||
@@ -4,10 +4,8 @@ import com.faf223.expensetrackerfaf.model.Income;
|
|||||||
import com.faf223.expensetrackerfaf.service.IncomeService;
|
import com.faf223.expensetrackerfaf.service.IncomeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -22,14 +20,40 @@ public class IncomeController {
|
|||||||
this.incomeService = incomeService;
|
this.incomeService = incomeService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/user/{userUuid}")
|
@GetMapping()
|
||||||
public ResponseEntity<List<Income>> getIncomesByUser(@PathVariable String userUuid) {
|
public ResponseEntity<List<Income>> getAllIncomes() {
|
||||||
List<Income> incomes = incomeService.getIncomesByUserId(userUuid);
|
List<Income> incomes = incomeService.getIncomes();
|
||||||
if (!incomes.isEmpty()) {
|
if (!incomes.isEmpty()) return ResponseEntity.ok(incomes);
|
||||||
return ResponseEntity.ok(incomes);
|
else return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping()
|
||||||
|
public ResponseEntity<Income> createNewIncome(@ModelAttribute("income") Income income,
|
||||||
|
BindingResult bindingResult) {
|
||||||
|
if (!bindingResult.hasErrors()) {
|
||||||
|
incomeService.createOrUpdateIncome(income);
|
||||||
|
return ResponseEntity.ok(income);
|
||||||
} else {
|
} else {
|
||||||
return ResponseEntity.notFound().build();
|
return ResponseEntity.notFound().build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PatchMapping()
|
||||||
|
public ResponseEntity<Income> updateIncome(@ModelAttribute("income") Income income,
|
||||||
|
BindingResult bindingResult) {
|
||||||
|
if (!bindingResult.hasErrors()) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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.");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
@@ -4,10 +4,10 @@ import com.faf223.expensetrackerfaf.model.User;
|
|||||||
import com.faf223.expensetrackerfaf.service.UserService;
|
import com.faf223.expensetrackerfaf.service.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/users")
|
@RequestMapping("/users")
|
||||||
@@ -20,14 +20,40 @@ public class UserController {
|
|||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{userUuid}")
|
@GetMapping()
|
||||||
public ResponseEntity<User> getUser(@PathVariable String userUuid) {
|
public ResponseEntity<List<User>> getAllUsers() {
|
||||||
User user = userService.getUserById(userUuid);
|
List<User> users = userService.getUsers();
|
||||||
if (user != null) {
|
if (!users.isEmpty()) return ResponseEntity.ok(users);
|
||||||
|
else return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping()
|
||||||
|
public ResponseEntity<User> createNewUser(@ModelAttribute("user") User user,
|
||||||
|
BindingResult bindingResult) {
|
||||||
|
if (!bindingResult.hasErrors()) {
|
||||||
|
userService.createOrUpdateUser(user);
|
||||||
return ResponseEntity.ok(user);
|
return ResponseEntity.ok(user);
|
||||||
} else {
|
} else {
|
||||||
return ResponseEntity.notFound().build();
|
return ResponseEntity.notFound().build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PatchMapping()
|
||||||
|
public ResponseEntity<User> updateUser(@ModelAttribute("user") 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,14 @@ public class IncomeService {
|
|||||||
this.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) {
|
public List<Income> getIncomesByUserId(String userUuid) {
|
||||||
return incomeRepository.findByUserUserUuid(userUuid);
|
return incomeRepository.findByUserUserUuid(userUuid);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import com.faf223.expensetrackerfaf.repository.UserRepository;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService {
|
public class UserService {
|
||||||
|
|
||||||
@@ -15,6 +17,14 @@ public class UserService {
|
|||||||
this.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) {
|
public User getUserById(String userUuid) {
|
||||||
return userRepository.findById(userUuid).orElse(null);
|
return userRepository.findById(userUuid).orElse(null);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user