add dto, refactor names, update controllers
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package com.faf223.expensetrackerfaf.controller;
|
||||
|
||||
import com.faf223.expensetrackerfaf.dto.ExpenseCreationDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.mappers.ExpenseMapper;
|
||||
import com.faf223.expensetrackerfaf.model.Expense;
|
||||
import com.faf223.expensetrackerfaf.service.ExpenseService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -8,50 +11,55 @@ import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/expenses")
|
||||
public class ExpenseController {
|
||||
|
||||
private final ExpenseService expenseService;
|
||||
private final ExpenseMapper expenseMapper;
|
||||
|
||||
@Autowired
|
||||
public ExpenseController(ExpenseService expenseService) {
|
||||
public ExpenseController(ExpenseService expenseService, ExpenseMapper expenseMapper) {
|
||||
this.expenseService = expenseService;
|
||||
this.expenseMapper = expenseMapper;
|
||||
}
|
||||
|
||||
@GetMapping()
|
||||
public ResponseEntity<List<Expense>> getAllExpenses() {
|
||||
List<Expense> expenses = expenseService.getExpenses();
|
||||
public ResponseEntity<List<ExpenseDTO>> getAllExpenses() {
|
||||
List<ExpenseDTO> expenses = expenseService.getExpenses().stream().map(expenseMapper::toDto).collect(Collectors.toList());
|
||||
if (!expenses.isEmpty()) return ResponseEntity.ok(expenses);
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public ResponseEntity<Expense> createNewExpense(@RequestBody Expense expense,
|
||||
public ResponseEntity<ExpenseDTO> createNewExpense(@RequestBody ExpenseCreationDTO expenseDTO,
|
||||
BindingResult bindingResult) {
|
||||
Expense expense = expenseMapper.toExpense(expenseDTO);
|
||||
if (!bindingResult.hasErrors()) {
|
||||
expenseService.createOrUpdateExpense(expense);
|
||||
return ResponseEntity.ok(expense);
|
||||
return ResponseEntity.ok(expenseMapper.toDto(expense));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@PatchMapping()
|
||||
public ResponseEntity<Expense> updateExpense(@RequestBody Expense expense,
|
||||
public ResponseEntity<ExpenseDTO> updateExpense(@RequestBody ExpenseCreationDTO expenseDTO,
|
||||
BindingResult bindingResult) {
|
||||
Expense expense = expenseMapper.toExpense(expenseDTO);
|
||||
if (!bindingResult.hasErrors()) {
|
||||
expenseService.createOrUpdateExpense(expense);
|
||||
return ResponseEntity.ok(expense);
|
||||
return ResponseEntity.ok(expenseMapper.toDto(expense));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/{userUuid}")
|
||||
public ResponseEntity<List<Expense>> getExpensesByUser(@PathVariable String userUuid) {
|
||||
List<Expense> expenses = expenseService.getExpensesByUserId(userUuid);
|
||||
public ResponseEntity<List<ExpenseDTO>> getExpensesByUser(@PathVariable String userUuid) {
|
||||
List<ExpenseDTO> expenses = expenseService.getExpensesByUserId(userUuid).stream().map(expenseMapper::toDto).collect(Collectors.toList());
|
||||
if (!expenses.isEmpty()) return ResponseEntity.ok(expenses);
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.faf223.expensetrackerfaf.controller;
|
||||
|
||||
import com.faf223.expensetrackerfaf.dto.IncomeCreationDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.mappers.IncomeMapper;
|
||||
import com.faf223.expensetrackerfaf.model.Income;
|
||||
import com.faf223.expensetrackerfaf.service.IncomeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -8,50 +11,55 @@ import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/incomes")
|
||||
public class IncomeController {
|
||||
|
||||
private final IncomeService incomeService;
|
||||
private final IncomeMapper incomeMapper;
|
||||
|
||||
@Autowired
|
||||
public IncomeController(IncomeService incomeService) {
|
||||
public IncomeController(IncomeService incomeService, IncomeMapper incomeMapper) {
|
||||
this.incomeService = incomeService;
|
||||
this.incomeMapper = incomeMapper;
|
||||
}
|
||||
|
||||
@GetMapping()
|
||||
public ResponseEntity<List<Income>> getAllIncomes() {
|
||||
List<Income> incomes = incomeService.getIncomes();
|
||||
public ResponseEntity<List<IncomeDTO>> getAllIncomes() {
|
||||
List<IncomeDTO> incomes = incomeService.getIncomes().stream().map(incomeMapper::toDto).collect(Collectors.toList());
|
||||
if (!incomes.isEmpty()) return ResponseEntity.ok(incomes);
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public ResponseEntity<Income> createNewIncome(@RequestBody Income income,
|
||||
public ResponseEntity<IncomeDTO> createNewIncome(@RequestBody IncomeCreationDTO incomeDTO,
|
||||
BindingResult bindingResult) {
|
||||
Income income = incomeMapper.toIncome(incomeDTO);
|
||||
if (!bindingResult.hasErrors()) {
|
||||
incomeService.createOrUpdateIncome(income);
|
||||
return ResponseEntity.ok(income);
|
||||
return ResponseEntity.ok(incomeMapper.toDto(income));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@PatchMapping()
|
||||
public ResponseEntity<Income> updateIncome(@RequestBody Income income,
|
||||
public ResponseEntity<IncomeDTO> updateIncome(@RequestBody IncomeCreationDTO incomeDTO,
|
||||
BindingResult bindingResult) {
|
||||
Income income = incomeMapper.toIncome(incomeDTO);
|
||||
if (!bindingResult.hasErrors()) {
|
||||
incomeService.createOrUpdateIncome(income);
|
||||
return ResponseEntity.ok(income);
|
||||
return ResponseEntity.ok(incomeMapper.toDto(income));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/{userUuid}")
|
||||
public ResponseEntity<List<Income>> getIncomesByUser(@PathVariable String userUuid) {
|
||||
List<Income> incomes = incomeService.getIncomesByUserId(userUuid);
|
||||
public ResponseEntity<List<IncomeDTO>> getIncomesByUser(@PathVariable String userUuid) {
|
||||
List<IncomeDTO> incomes = incomeService.getIncomesByUserId(userUuid).stream().map(incomeMapper::toDto).collect(Collectors.toList());
|
||||
if (!incomes.isEmpty()) return ResponseEntity.ok(incomes);
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.faf223.expensetrackerfaf.controller;
|
||||
|
||||
import com.faf223.expensetrackerfaf.dto.UserCreationDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.UserDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
|
||||
import com.faf223.expensetrackerfaf.model.User;
|
||||
import com.faf223.expensetrackerfaf.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -8,51 +11,56 @@ import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
private final UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
public UserController(UserService userService) {
|
||||
public UserController(UserService userService, UserMapper userMapper) {
|
||||
this.userService = userService;
|
||||
this.userMapper = userMapper;
|
||||
}
|
||||
|
||||
@GetMapping()
|
||||
public ResponseEntity<List<User>> getAllUsers() {
|
||||
List<User> users = userService.getUsers();
|
||||
public ResponseEntity<List<UserDTO>> getAllUsers() {
|
||||
List<UserDTO> users = userService.getUsers().stream().map(userMapper::toDto).collect(Collectors.toList());
|
||||
if (!users.isEmpty()) return ResponseEntity.ok(users);
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public ResponseEntity<User> createNewUser(@RequestBody User user,
|
||||
public ResponseEntity<UserDTO> createNewUser(@RequestBody UserCreationDTO userDTO,
|
||||
BindingResult bindingResult) {
|
||||
User user = userMapper.toUser(userDTO);
|
||||
if (!bindingResult.hasErrors()) {
|
||||
userService.createOrUpdateUser(user);
|
||||
return ResponseEntity.ok(user);
|
||||
return ResponseEntity.ok(userMapper.toDto(user));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@PatchMapping()
|
||||
public ResponseEntity<User> updateUser(@RequestBody User user,
|
||||
public ResponseEntity<UserDTO> updateUser(@RequestBody UserCreationDTO userDTO,
|
||||
BindingResult bindingResult) {
|
||||
User user = userMapper.toUser(userDTO);
|
||||
if (!bindingResult.hasErrors()) {
|
||||
userService.createOrUpdateUser(user);
|
||||
return ResponseEntity.ok(user);
|
||||
return ResponseEntity.ok(userMapper.toDto(user));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/{userUuid}")
|
||||
public ResponseEntity<User> getUser(@PathVariable String userUuid) {
|
||||
public ResponseEntity<UserDTO> getUser(@PathVariable String userUuid) {
|
||||
User user = userService.getUserById(userUuid);
|
||||
if (user != null) return ResponseEntity.ok(user);
|
||||
if (user != null) return ResponseEntity.ok(userMapper.toDto(user));
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user