add exception handling
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
package com.faf223.expensetrackerfaf.controller;
|
||||
|
||||
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.IncomeCreationDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.mappers.IncomeMapper;
|
||||
import com.faf223.expensetrackerfaf.model.*;
|
||||
import com.faf223.expensetrackerfaf.model.Income;
|
||||
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
||||
import com.faf223.expensetrackerfaf.model.User;
|
||||
import com.faf223.expensetrackerfaf.service.IncomeCategoryService;
|
||||
import com.faf223.expensetrackerfaf.service.IncomeService;
|
||||
import com.faf223.expensetrackerfaf.service.UserService;
|
||||
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotCreatedException;
|
||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotUpdatedException;
|
||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionsNotFoundException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -40,13 +45,17 @@ public class IncomeController {
|
||||
public ResponseEntity<List<IncomeDTO>> getAllIncomes() {
|
||||
List<IncomeDTO> incomes = incomeService.getTransactions().stream().map(incomeMapper::toDto).collect(Collectors.toList());
|
||||
if (!incomes.isEmpty()) return ResponseEntity.ok(incomes);
|
||||
else return ResponseEntity.notFound().build();
|
||||
else throw new TransactionsNotFoundException("Transactions not found");
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public ResponseEntity<Void> createNewIncome(@RequestBody IncomeCreationDTO incomeDTO,
|
||||
BindingResult bindingResult) {
|
||||
Income income = incomeMapper.toIncome(incomeDTO);
|
||||
|
||||
if(bindingResult.hasErrors())
|
||||
throw new TransactionNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||
@@ -55,12 +64,11 @@ public class IncomeController {
|
||||
User user = userService.getUserByEmail(email);
|
||||
income.setUser(user);
|
||||
|
||||
System.out.println(income);
|
||||
incomeService.createOrUpdate(income);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
}
|
||||
|
||||
return ResponseEntity.notFound().build();
|
||||
throw new TransactionNotCreatedException("Could not create new expense");
|
||||
}
|
||||
|
||||
// TODO: check if the income belongs to the user, extract logic into service
|
||||
@@ -68,6 +76,9 @@ public class IncomeController {
|
||||
public ResponseEntity<Void> updateIncome(@PathVariable long id, @RequestBody IncomeCreationDTO incomeDTO,
|
||||
BindingResult bindingResult) {
|
||||
Income income = incomeService.getTransactionById(id);
|
||||
|
||||
if(income == null) throw new TransactionsNotFoundException("The income has not been found");
|
||||
|
||||
IncomeCategory category = incomeCategoryService.getCategoryById(incomeDTO.getIncomeCategory());
|
||||
income.setCategory(category);
|
||||
income.setAmount(incomeDTO.getAmount());
|
||||
@@ -76,30 +87,10 @@ public class IncomeController {
|
||||
incomeService.createOrUpdate(income);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
throw new TransactionNotUpdatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/personal-incomes")
|
||||
public ResponseEntity<List<IncomeDTO>> getIncomesByUser() {
|
||||
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||
|
||||
String email = userDetails.getUsername();
|
||||
List<IncomeDTO> incomes = incomeService.getTransactionsByEmail(email).stream().map(incomeMapper::toDto).collect(Collectors.toList());
|
||||
|
||||
if (!incomes.isEmpty()) {
|
||||
return ResponseEntity.ok(incomes);
|
||||
} else {
|
||||
return ResponseEntity.ok(Collections.emptyList());
|
||||
}
|
||||
}
|
||||
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@GetMapping("/personal-incomes")
|
||||
public ResponseEntity<List<IncomeDTO>> getExpensesByUser(@RequestParam Optional<LocalDate> date,
|
||||
@RequestParam Optional<Month> month) {
|
||||
@@ -122,14 +113,14 @@ public class IncomeController {
|
||||
}
|
||||
}
|
||||
|
||||
return ResponseEntity.notFound().build();
|
||||
throw new TransactionsNotFoundException("The expenses have not been found");
|
||||
}
|
||||
|
||||
@GetMapping("/categories")
|
||||
public ResponseEntity<List<IncomeCategory>> getAllCategories() {
|
||||
List<IncomeCategory> categories = incomeCategoryService.getAllCategories();
|
||||
if (!categories.isEmpty()) return ResponseEntity.ok(categories);
|
||||
else return ResponseEntity.notFound().build();
|
||||
else throw new TransactionsNotFoundException("The expenses have not been found");
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
|
||||
Reference in New Issue
Block a user