Make income/expense return created instance's id

This commit is contained in:
Dmitrii Cravcenco
2023-12-10 12:59:26 +02:00
parent f98b9ac3e7
commit a373daa58c
2 changed files with 11 additions and 11 deletions

View File

@@ -28,9 +28,7 @@ import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.time.Month;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
@RestController
@@ -52,7 +50,7 @@ public class ExpenseController {
}
@PostMapping()
public ResponseEntity<Void> createNewExpense(@RequestBody @Valid ExpenseCreationDTO expenseDTO,
public ResponseEntity<Map<String, Long>> createNewExpense(@RequestBody @Valid ExpenseCreationDTO expenseDTO,
BindingResult bindingResult) {
if(bindingResult.hasErrors())
throw new TransactionNotCreatedException("Could not create new expense");
@@ -68,7 +66,9 @@ public class ExpenseController {
expense.setUser(user);
expenseService.createOrUpdate(expense);
return ResponseEntity.status(HttpStatus.CREATED).build();
Map<String, Long> response = new HashMap<>();
response.put("expenseId", expense.getId());
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}
throw new TransactionNotCreatedException("Could not create new expense");

View File

@@ -28,9 +28,7 @@ import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.time.Month;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
@RestController
@@ -52,7 +50,7 @@ public class IncomeController {
}
@PostMapping()
public ResponseEntity<Void> createNewIncome(@RequestBody @Valid IncomeCreationDTO incomeDTO,
public ResponseEntity<Map<String, Long>> createNewIncome(@RequestBody @Valid IncomeCreationDTO incomeDTO,
BindingResult bindingResult) {
if(bindingResult.hasErrors())
throw new TransactionNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
@@ -68,10 +66,12 @@ public class IncomeController {
income.setUser(user);
incomeService.createOrUpdate(income);
return ResponseEntity.status(HttpStatus.CREATED).build();
Map<String, Long> response = new HashMap<>();
response.put("incomeId", income.getId());
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}
throw new TransactionNotCreatedException("Could not create new expense");
throw new TransactionNotCreatedException("Could not create new income");
}
@PatchMapping("/update/{id}")