Fix expense/income creation

This commit is contained in:
Dmitrii Cravcenco
2023-10-24 18:34:51 +03:00
parent 4e00244a26
commit d402514ecb
13 changed files with 127 additions and 41 deletions

View File

@@ -4,8 +4,13 @@ import com.faf223.expensetrackerfaf.dto.ExpenseCreationDTO;
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
import com.faf223.expensetrackerfaf.dto.mappers.ExpenseMapper;
import com.faf223.expensetrackerfaf.model.Expense;
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
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 lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
@@ -23,7 +28,9 @@ import java.util.stream.Collectors;
public class ExpenseController {
private final ExpenseService expenseService;
private final UserService userService;
private final ExpenseMapper expenseMapper;
private final ExpenseCategoryService expenseCategoryService;
@GetMapping()
@PreAuthorize("hasRole('ADMIN')")
@@ -37,14 +44,24 @@ public class ExpenseController {
public ResponseEntity<ExpenseDTO> createNewExpense(@RequestBody ExpenseCreationDTO expenseDTO,
BindingResult bindingResult) {
Expense expense = expenseMapper.toExpense(expenseDTO);
if (!bindingResult.hasErrors()) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
String email = userDetails.getUsername();
User user = userService.getUserByEmail(email);
expense.setUser(user);
expenseService.createOrUpdateExpense(expense);
return ResponseEntity.ok(expenseMapper.toDto(expense));
} else {
return ResponseEntity.notFound().build();
ExpenseDTO createdExpenseDTO = expenseMapper.toDto(expense);
return ResponseEntity.status(HttpStatus.CREATED).body(createdExpenseDTO);
}
return ResponseEntity.notFound().build();
}
// TODO: has to be checked on auto extracting Uuid
@PatchMapping()
public ResponseEntity<ExpenseDTO> updateExpense(@RequestBody ExpenseCreationDTO expenseDTO,
BindingResult bindingResult) {
@@ -74,5 +91,4 @@ public class ExpenseController {
return ResponseEntity.notFound().build();
}
}
}

View File

@@ -1,11 +1,15 @@
package com.faf223.expensetrackerfaf.controller;
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
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.model.User;
import com.faf223.expensetrackerfaf.service.IncomeService;
import com.faf223.expensetrackerfaf.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
@@ -23,6 +27,7 @@ import java.util.stream.Collectors;
public class IncomeController {
private final IncomeService incomeService;
private final UserService userService;
private final IncomeMapper incomeMapper;
@GetMapping()
@@ -37,12 +42,21 @@ public class IncomeController {
public ResponseEntity<IncomeDTO> createNewIncome(@RequestBody IncomeCreationDTO incomeDTO,
BindingResult bindingResult) {
Income income = incomeMapper.toIncome(incomeDTO);
if (!bindingResult.hasErrors()) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
String email = userDetails.getUsername();
User user = userService.getUserByEmail(email);
income.setUser(user);
System.out.println(income);
incomeService.createOrUpdateIncome(income);
return ResponseEntity.ok(incomeMapper.toDto(income));
} else {
return ResponseEntity.notFound().build();
IncomeDTO createdIncomeDTO = incomeMapper.toDto(income);
return ResponseEntity.status(HttpStatus.CREATED).body(createdIncomeDTO);
}
return ResponseEntity.notFound().build();
}
@PatchMapping()
@@ -65,14 +79,13 @@ public class IncomeController {
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
String email = userDetails.getUsername();
List<IncomeDTO> expenses = incomeService.getIncomesByEmail(email).stream().map(incomeMapper::toDto).collect(Collectors.toList());
List<IncomeDTO> incomes = incomeService.getIncomesByEmail(email).stream().map(incomeMapper::toDto).collect(Collectors.toList());
if (!expenses.isEmpty()) {
return ResponseEntity.ok(expenses);
if (!incomes.isEmpty()) {
return ResponseEntity.ok(incomes);
}
}
return ResponseEntity.notFound().build();
}
}
}

View File

@@ -11,9 +11,6 @@ import java.time.LocalDate;
@Data
@AllArgsConstructor
public class ExpenseCreationDTO {
private long expenseId;
private User user;
private ExpenseCategory expenseCategory;
private LocalDate date;
private int expenseCategory;
private BigDecimal amount;
}
}

View File

@@ -1,6 +1,7 @@
package com.faf223.expensetrackerfaf.dto;
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -9,10 +10,11 @@ import java.time.LocalDate;
@Data
@AllArgsConstructor
@JsonIgnoreProperties({"expenseCategory"})
public class ExpenseDTO {
private long expenseId;
private UserDTO userDTO;
private ExpenseCategory expenseCategory;
private LocalDate date;
private BigDecimal amount;
}
}

View File

@@ -11,9 +11,6 @@ import java.time.LocalDate;
@Data
@AllArgsConstructor
public class IncomeCreationDTO {
private long incomeId;
private User user;
private IncomeCategory category;
private LocalDate date;
private int incomeCategory;
private BigDecimal amount;
}
}

View File

@@ -1,6 +1,7 @@
package com.faf223.expensetrackerfaf.dto;
import com.faf223.expensetrackerfaf.model.IncomeCategory;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -9,10 +10,11 @@ import java.time.LocalDate;
@Data
@AllArgsConstructor
@JsonIgnoreProperties({"incomeCategory"})
public class IncomeDTO {
private long incomeId;
private UserDTO userDTO;
private IncomeCategory category;
private IncomeCategory incomeCategory;
private LocalDate date;
private BigDecimal amount;
}
}

View File

@@ -3,19 +3,24 @@ package com.faf223.expensetrackerfaf.dto.mappers;
import com.faf223.expensetrackerfaf.dto.ExpenseCreationDTO;
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
import com.faf223.expensetrackerfaf.model.Expense;
import com.faf223.expensetrackerfaf.service.ExpenseCategoryService;
import com.faf223.expensetrackerfaf.service.ExpenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
@Component
public class ExpenseMapper {
private final ExpenseService expenseService;
private final ExpenseCategoryService expenseCategoryService;
private final UserMapper userMapper;
@Autowired
public ExpenseMapper(ExpenseService expenseService, UserMapper userMapper) {
public ExpenseMapper(ExpenseService expenseService, ExpenseCategoryService expenseCategoryService, UserMapper userMapper) {
this.expenseService = expenseService;
this.expenseCategoryService = expenseCategoryService;
this.userMapper = userMapper;
}
@@ -25,10 +30,8 @@ public class ExpenseMapper {
}
public Expense toExpense(ExpenseCreationDTO expenseDTO) {
Expense expense = expenseService.getExpenseById(expenseDTO.getExpenseId());
if(expense == null) return new Expense(expenseDTO.getExpenseId(), expenseDTO.getUser(),
expenseDTO.getExpenseCategory(), expenseDTO.getDate(), expenseDTO.getAmount());
return expense;
return new Expense(expenseCategoryService.getExpenseCategory(expenseDTO.getExpenseCategory()), LocalDate.now(), expenseDTO.getAmount());
}
}
}

View File

@@ -2,20 +2,26 @@ package com.faf223.expensetrackerfaf.dto.mappers;
import com.faf223.expensetrackerfaf.dto.IncomeCreationDTO;
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
import com.faf223.expensetrackerfaf.model.Expense;
import com.faf223.expensetrackerfaf.model.Income;
import com.faf223.expensetrackerfaf.service.IncomeCategoryService;
import com.faf223.expensetrackerfaf.service.IncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
@Component
public class IncomeMapper {
private final IncomeService incomeService;
private final IncomeCategoryService incomeCategoryService;
private final UserMapper userMapper;
@Autowired
public IncomeMapper(IncomeService incomeService, UserMapper userMapper) {
public IncomeMapper(IncomeService incomeService, IncomeCategoryService incomeCategoryService, UserMapper userMapper) {
this.incomeService = incomeService;
this.incomeCategoryService = incomeCategoryService;
this.userMapper = userMapper;
}
@@ -25,10 +31,8 @@ public class IncomeMapper {
}
public Income toIncome(IncomeCreationDTO incomeDTO) {
Income income = incomeService.getIncomeById(incomeDTO.getIncomeId());
if(income == null) return new Income(incomeDTO.getIncomeId(), incomeDTO.getUser(),
incomeDTO.getCategory(), incomeDTO.getDate(), incomeDTO.getAmount());
return income;
return new Income(incomeCategoryService.getExpenseCategory(incomeDTO.getIncomeCategory()), LocalDate.now(), incomeDTO.getAmount());
}
}
}

View File

@@ -1,5 +1,4 @@
package com.faf223.expensetrackerfaf.model;
import com.faf223.expensetrackerfaf.util.IMoneyTransaction;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;

View File

@@ -29,4 +29,10 @@ public class Income implements IMoneyTransaction {
private LocalDate date;
private BigDecimal amount;
public Income(IncomeCategory incomeCategory, LocalDate date, BigDecimal amount) {
this.category = incomeCategory;
this.date = date;
this.amount = amount;
}
}

View File

@@ -0,0 +1,18 @@
package com.faf223.expensetrackerfaf.service;
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
import com.faf223.expensetrackerfaf.repository.ExpenseCategoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class ExpenseCategoryService {
private final ExpenseCategoryRepository expenseCategoryRepository;
public ExpenseCategory getExpenseCategory(long category) {
return expenseCategoryRepository.getReferenceById(category);
}
}

View File

@@ -0,0 +1,17 @@
package com.faf223.expensetrackerfaf.service;
import com.faf223.expensetrackerfaf.model.IncomeCategory;
import com.faf223.expensetrackerfaf.repository.IncomeCategoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class IncomeCategoryService {
private final IncomeCategoryRepository incomeCategoryRepository;
public IncomeCategory getExpenseCategory(long category) {
return incomeCategoryRepository.getReferenceById(category);
}
}

View File

@@ -1,17 +1,21 @@
package com.faf223.expensetrackerfaf.service;
import com.faf223.expensetrackerfaf.model.Credential;
import com.faf223.expensetrackerfaf.model.User;
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
import com.faf223.expensetrackerfaf.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
private final CredentialRepository credentialRepository;
public void updateUser(User user) {
userRepository.save(user);
@@ -24,4 +28,12 @@ public class UserService {
public User getUserById(String userUuid) {
return userRepository.findById(userUuid).orElse(null);
}
public User getUserByEmail(String email) {
Optional<Credential> credential = credentialRepository.findByEmail(email);
if (credential.isPresent()) {
Optional<User> user = userRepository.findById(credential.get().getUser().getUserUuid());
return user.orElse(null);
}
return null;
}
}