Add expense/income update #31
@@ -61,14 +61,18 @@ public class ExpenseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO: has to be checked on auto extracting Uuid
|
// TODO: check if the expense belongs to the user
|
||||||
@PatchMapping()
|
@PatchMapping("/update/{id}")
|
||||||
public ResponseEntity<ExpenseDTO> updateExpense(@RequestBody ExpenseCreationDTO expenseDTO,
|
public ResponseEntity<Void> updateExpense(@PathVariable long id, @RequestBody ExpenseCreationDTO expenseDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
Expense expense = expenseMapper.toExpense(expenseDTO);
|
Expense expense = expenseService.getTransactionById(id);
|
||||||
|
ExpenseCategory category = expenseCategoryService.getCategoryById(expenseDTO.getExpenseCategory());
|
||||||
|
expense.setCategory(category);
|
||||||
|
expense.setAmount(expenseDTO.getAmount());
|
||||||
|
|
||||||
if (!bindingResult.hasErrors()) {
|
if (!bindingResult.hasErrors()) {
|
||||||
expenseService.createOrUpdate(expense);
|
expenseService.createOrUpdate(expense);
|
||||||
return ResponseEntity.ok(expenseMapper.toDto(expense));
|
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||||
} else {
|
} else {
|
||||||
return ResponseEntity.notFound().build();
|
return ResponseEntity.notFound().build();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,7 @@ package com.faf223.expensetrackerfaf.controller;
|
|||||||
import com.faf223.expensetrackerfaf.dto.IncomeCreationDTO;
|
import com.faf223.expensetrackerfaf.dto.IncomeCreationDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.mappers.IncomeMapper;
|
import com.faf223.expensetrackerfaf.dto.mappers.IncomeMapper;
|
||||||
import com.faf223.expensetrackerfaf.model.Income;
|
import com.faf223.expensetrackerfaf.model.*;
|
||||||
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
|
||||||
import com.faf223.expensetrackerfaf.service.IncomeCategoryService;
|
import com.faf223.expensetrackerfaf.service.IncomeCategoryService;
|
||||||
import com.faf223.expensetrackerfaf.service.IncomeService;
|
import com.faf223.expensetrackerfaf.service.IncomeService;
|
||||||
import com.faf223.expensetrackerfaf.service.UserService;
|
import com.faf223.expensetrackerfaf.service.UserService;
|
||||||
@@ -60,13 +58,18 @@ public class IncomeController {
|
|||||||
return ResponseEntity.notFound().build();
|
return ResponseEntity.notFound().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping()
|
// TODO: check if the income belongs to the user, extract logic into service
|
||||||
public ResponseEntity<IncomeDTO> updateIncome(@RequestBody IncomeCreationDTO incomeDTO,
|
@PatchMapping("/update/{id}")
|
||||||
|
public ResponseEntity<Void> updateIncome(@PathVariable long id, @RequestBody IncomeCreationDTO incomeDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
Income income = incomeMapper.toIncome(incomeDTO);
|
Income income = incomeService.getTransactionById(id);
|
||||||
|
IncomeCategory category = incomeCategoryService.getCategoryById(incomeDTO.getIncomeCategory());
|
||||||
|
income.setCategory(category);
|
||||||
|
income.setAmount(incomeDTO.getAmount());
|
||||||
|
|
||||||
if (!bindingResult.hasErrors()) {
|
if (!bindingResult.hasErrors()) {
|
||||||
incomeService.createOrUpdate(income);
|
incomeService.createOrUpdate(income);
|
||||||
return ResponseEntity.ok(incomeMapper.toDto(income));
|
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||||
} else {
|
} else {
|
||||||
return ResponseEntity.notFound().build();
|
return ResponseEntity.notFound().build();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user