Add expense/income update

This commit is contained in:
Dmitrii Cravcenco
2023-10-27 17:53:00 +03:00
parent 6fc12d22b5
commit eb2d0e53dc
2 changed files with 20 additions and 13 deletions

View File

@@ -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();
} }

View File

@@ -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}")
BindingResult bindingResult) { public ResponseEntity<Void> updateIncome(@PathVariable long id, @RequestBody IncomeCreationDTO incomeDTO,
Income income = incomeMapper.toIncome(incomeDTO); BindingResult bindingResult) {
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();
} }