add dto, refactor names, update controllers
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package com.faf223.expensetrackerfaf.controller;
|
||||
|
||||
import com.faf223.expensetrackerfaf.dto.IncomeCreationDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.mappers.IncomeMapper;
|
||||
import com.faf223.expensetrackerfaf.model.Income;
|
||||
import com.faf223.expensetrackerfaf.service.IncomeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -8,50 +11,55 @@ import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/incomes")
|
||||
public class IncomeController {
|
||||
|
||||
private final IncomeService incomeService;
|
||||
private final IncomeMapper incomeMapper;
|
||||
|
||||
@Autowired
|
||||
public IncomeController(IncomeService incomeService) {
|
||||
public IncomeController(IncomeService incomeService, IncomeMapper incomeMapper) {
|
||||
this.incomeService = incomeService;
|
||||
this.incomeMapper = incomeMapper;
|
||||
}
|
||||
|
||||
@GetMapping()
|
||||
public ResponseEntity<List<Income>> getAllIncomes() {
|
||||
List<Income> incomes = incomeService.getIncomes();
|
||||
public ResponseEntity<List<IncomeDTO>> getAllIncomes() {
|
||||
List<IncomeDTO> incomes = incomeService.getIncomes().stream().map(incomeMapper::toDto).collect(Collectors.toList());
|
||||
if (!incomes.isEmpty()) return ResponseEntity.ok(incomes);
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@PostMapping()
|
||||
public ResponseEntity<Income> createNewIncome(@RequestBody Income income,
|
||||
public ResponseEntity<IncomeDTO> createNewIncome(@RequestBody IncomeCreationDTO incomeDTO,
|
||||
BindingResult bindingResult) {
|
||||
Income income = incomeMapper.toIncome(incomeDTO);
|
||||
if (!bindingResult.hasErrors()) {
|
||||
incomeService.createOrUpdateIncome(income);
|
||||
return ResponseEntity.ok(income);
|
||||
return ResponseEntity.ok(incomeMapper.toDto(income));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@PatchMapping()
|
||||
public ResponseEntity<Income> updateIncome(@RequestBody Income income,
|
||||
public ResponseEntity<IncomeDTO> updateIncome(@RequestBody IncomeCreationDTO incomeDTO,
|
||||
BindingResult bindingResult) {
|
||||
Income income = incomeMapper.toIncome(incomeDTO);
|
||||
if (!bindingResult.hasErrors()) {
|
||||
incomeService.createOrUpdateIncome(income);
|
||||
return ResponseEntity.ok(income);
|
||||
return ResponseEntity.ok(incomeMapper.toDto(income));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/{userUuid}")
|
||||
public ResponseEntity<List<Income>> getIncomesByUser(@PathVariable String userUuid) {
|
||||
List<Income> incomes = incomeService.getIncomesByUserId(userUuid);
|
||||
public ResponseEntity<List<IncomeDTO>> getIncomesByUser(@PathVariable String userUuid) {
|
||||
List<IncomeDTO> incomes = incomeService.getIncomesByUserId(userUuid).stream().map(incomeMapper::toDto).collect(Collectors.toList());
|
||||
if (!incomes.isEmpty()) return ResponseEntity.ok(incomes);
|
||||
else return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user