add exception handling

This commit is contained in:
mirrerror
2023-11-13 10:11:44 +02:00
parent acbb6285d9
commit 3ba95647b4
10 changed files with 168 additions and 36 deletions

View File

@@ -9,6 +9,10 @@ import com.faf223.expensetrackerfaf.model.User;
import com.faf223.expensetrackerfaf.service.ExpenseCategoryService;
import com.faf223.expensetrackerfaf.service.ExpenseService;
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;
@@ -41,7 +45,7 @@ public class ExpenseController {
public ResponseEntity<List<ExpenseDTO>> getAllExpenses() {
List<ExpenseDTO> expenses = expenseService.getTransactions().stream().map(expenseMapper::toDto).collect(Collectors.toList());
if (!expenses.isEmpty()) return ResponseEntity.ok(expenses);
else return ResponseEntity.notFound().build();
else throw new TransactionsNotFoundException("Transactions not found");
}
@PostMapping()
@@ -49,6 +53,9 @@ public class ExpenseController {
BindingResult bindingResult) {
Expense expense = expenseMapper.toExpense(expenseDTO);
if(bindingResult.hasErrors())
throw new TransactionNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
@@ -61,7 +68,7 @@ public class ExpenseController {
return ResponseEntity.status(HttpStatus.CREATED).build();
}
return ResponseEntity.notFound().build();
throw new TransactionNotCreatedException("Could not create new expense");
}
@@ -70,6 +77,9 @@ public class ExpenseController {
public ResponseEntity<Void> updateExpense(@PathVariable long id, @RequestBody ExpenseCreationDTO expenseDTO,
BindingResult bindingResult) {
Expense expense = expenseService.getTransactionById(id);
if(expense == null) throw new TransactionsNotFoundException("The expense has not been found");
ExpenseCategory category = expenseCategoryService.getCategoryById(expenseDTO.getExpenseCategory());
expense.setCategory(category);
expense.setAmount(expenseDTO.getAmount());
@@ -78,7 +88,7 @@ public class ExpenseController {
expenseService.createOrUpdate(expense);
return ResponseEntity.status(HttpStatus.CREATED).build();
} else {
return ResponseEntity.notFound().build();
throw new TransactionNotUpdatedException(ErrorResponse.from(bindingResult).getMessage());
}
}
@@ -104,14 +114,14 @@ public class ExpenseController {
}
}
return ResponseEntity.notFound().build();
throw new TransactionsNotFoundException("The expenses have not been found");
}
@GetMapping("/categories")
public ResponseEntity<List<ExpenseCategory>> getAllCategories() {
List<ExpenseCategory> categories = expenseCategoryService.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}")