add validation
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -69,9 +69,8 @@
|
|||||||
<version>0.11.5</version>
|
<version>0.11.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>jakarta.validation</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>jakarta.validation-api</artifactId>
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
<version>2.0.2</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
|
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -10,9 +10,11 @@ import com.faf223.expensetrackerfaf.service.ExpenseCategoryService;
|
|||||||
import com.faf223.expensetrackerfaf.service.ExpenseService;
|
import com.faf223.expensetrackerfaf.service.ExpenseService;
|
||||||
import com.faf223.expensetrackerfaf.service.UserService;
|
import com.faf223.expensetrackerfaf.service.UserService;
|
||||||
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionDoesNotBelongToTheUserException;
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotCreatedException;
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotCreatedException;
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotUpdatedException;
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotUpdatedException;
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionsNotFoundException;
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionsNotFoundException;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -49,12 +51,12 @@ public class ExpenseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping()
|
@PostMapping()
|
||||||
public ResponseEntity<Void> createNewExpense(@RequestBody ExpenseCreationDTO expenseDTO,
|
public ResponseEntity<Void> createNewExpense(@RequestBody @Valid ExpenseCreationDTO expenseDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
Expense expense = expenseMapper.toExpense(expenseDTO);
|
|
||||||
|
|
||||||
if(bindingResult.hasErrors())
|
if(bindingResult.hasErrors())
|
||||||
throw new TransactionNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
throw new TransactionNotCreatedException("Could not create new expense");
|
||||||
|
|
||||||
|
Expense expense = expenseMapper.toExpense(expenseDTO);
|
||||||
|
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
@@ -71,25 +73,26 @@ public class ExpenseController {
|
|||||||
throw new TransactionNotCreatedException("Could not create new expense");
|
throw new TransactionNotCreatedException("Could not create new expense");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO: check if the expense belongs to the user
|
|
||||||
@PatchMapping("/update/{id}")
|
@PatchMapping("/update/{id}")
|
||||||
public ResponseEntity<Void> updateExpense(@PathVariable long id, @RequestBody ExpenseCreationDTO expenseDTO,
|
public ResponseEntity<Void> updateExpense(@PathVariable long id, @RequestBody @Valid ExpenseCreationDTO expenseDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
|
if(bindingResult.hasErrors())
|
||||||
|
throw new TransactionNotUpdatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||||
|
|
||||||
Expense expense = expenseService.getTransactionById(id);
|
Expense expense = expenseService.getTransactionById(id);
|
||||||
|
|
||||||
if(expense == null) throw new TransactionsNotFoundException("The expense has not been found");
|
if(expense == null)
|
||||||
|
throw new TransactionsNotFoundException("The expense has not been found");
|
||||||
|
|
||||||
|
if(!expenseService.belongsToUser(expense))
|
||||||
|
throw new TransactionDoesNotBelongToTheUserException("The transaction does not belong to you");
|
||||||
|
|
||||||
ExpenseCategory category = expenseCategoryService.getCategoryById(expenseDTO.getExpenseCategory());
|
ExpenseCategory category = expenseCategoryService.getCategoryById(expenseDTO.getExpenseCategory());
|
||||||
expense.setCategory(category);
|
expense.setCategory(category);
|
||||||
expense.setAmount(expenseDTO.getAmount());
|
expense.setAmount(expenseDTO.getAmount());
|
||||||
|
|
||||||
if (!bindingResult.hasErrors()) {
|
|
||||||
expenseService.createOrUpdate(expense);
|
expenseService.createOrUpdate(expense);
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||||
} else {
|
|
||||||
throw new TransactionNotUpdatedException(ErrorResponse.from(bindingResult).getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/personal-expenses")
|
@GetMapping("/personal-expenses")
|
||||||
@@ -103,8 +106,8 @@ public class ExpenseController {
|
|||||||
String email = userDetails.getUsername();
|
String email = userDetails.getUsername();
|
||||||
List<ExpenseDTO> expenses;
|
List<ExpenseDTO> expenses;
|
||||||
|
|
||||||
expenses = date.map(localDate -> expenseService.getTransactionsByDate(localDate).stream().map(expenseMapper::toDto).toList())
|
expenses = date.map(localDate -> expenseService.getTransactionsByDate(localDate, email).stream().map(expenseMapper::toDto).toList())
|
||||||
.orElseGet(() -> month.map(value -> expenseService.getTransactionsByMonth(value).stream().map(expenseMapper::toDto).toList())
|
.orElseGet(() -> month.map(value -> expenseService.getTransactionsByMonth(value, email).stream().map(expenseMapper::toDto).toList())
|
||||||
.orElseGet(() -> expenseService.getTransactionsByEmail(email).stream().map(expenseMapper::toDto).toList()));
|
.orElseGet(() -> expenseService.getTransactionsByEmail(email).stream().map(expenseMapper::toDto).toList()));
|
||||||
|
|
||||||
if (!expenses.isEmpty()) {
|
if (!expenses.isEmpty()) {
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
package com.faf223.expensetrackerfaf.controller;
|
package com.faf223.expensetrackerfaf.controller;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionDoesNotBelongToTheUserException;
|
import com.faf223.expensetrackerfaf.util.exceptions.*;
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotCreatedException;
|
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionsNotFoundException;
|
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
@@ -20,7 +17,7 @@ public class GlobalExceptionHandler {
|
|||||||
System.currentTimeMillis()
|
System.currentTimeMillis()
|
||||||
);
|
);
|
||||||
|
|
||||||
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(response, HttpStatus.FORBIDDEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler
|
@ExceptionHandler
|
||||||
@@ -30,7 +27,7 @@ public class GlobalExceptionHandler {
|
|||||||
System.currentTimeMillis()
|
System.currentTimeMillis()
|
||||||
);
|
);
|
||||||
|
|
||||||
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(response, HttpStatus.NOT_MODIFIED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler
|
@ExceptionHandler
|
||||||
@@ -53,4 +50,34 @@ public class GlobalExceptionHandler {
|
|||||||
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
private ResponseEntity<ErrorResponse> handleTransactionNotUpdatedException(TransactionNotUpdatedException e) {
|
||||||
|
ErrorResponse response = new ErrorResponse(
|
||||||
|
e.getMessage(),
|
||||||
|
System.currentTimeMillis()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.NOT_MODIFIED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
private ResponseEntity<ErrorResponse> handleUserNotAuthenticatedException(UserNotAuthenticatedException e) {
|
||||||
|
ErrorResponse response = new ErrorResponse(
|
||||||
|
e.getMessage(),
|
||||||
|
System.currentTimeMillis()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.FORBIDDEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler
|
||||||
|
private ResponseEntity<ErrorResponse> handleUserNotCreatedException(UserNotCreatedException e) {
|
||||||
|
ErrorResponse response = new ErrorResponse(
|
||||||
|
e.getMessage(),
|
||||||
|
System.currentTimeMillis()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.NOT_MODIFIED);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,9 +10,11 @@ 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;
|
||||||
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionDoesNotBelongToTheUserException;
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotCreatedException;
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotCreatedException;
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotUpdatedException;
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionNotUpdatedException;
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.TransactionsNotFoundException;
|
import com.faf223.expensetrackerfaf.util.exceptions.TransactionsNotFoundException;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -49,13 +51,13 @@ public class IncomeController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping()
|
@PostMapping()
|
||||||
public ResponseEntity<Void> createNewIncome(@RequestBody IncomeCreationDTO incomeDTO,
|
public ResponseEntity<Void> createNewIncome(@RequestBody @Valid IncomeCreationDTO incomeDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
Income income = incomeMapper.toIncome(incomeDTO);
|
|
||||||
|
|
||||||
if(bindingResult.hasErrors())
|
if(bindingResult.hasErrors())
|
||||||
throw new TransactionNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
throw new TransactionNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||||
|
|
||||||
|
Income income = incomeMapper.toIncome(incomeDTO);
|
||||||
|
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||||
@@ -71,24 +73,26 @@ public class IncomeController {
|
|||||||
throw new TransactionNotCreatedException("Could not create new expense");
|
throw new TransactionNotCreatedException("Could not create new expense");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check if the income belongs to the user, extract logic into service
|
|
||||||
@PatchMapping("/update/{id}")
|
@PatchMapping("/update/{id}")
|
||||||
public ResponseEntity<Void> updateIncome(@PathVariable long id, @RequestBody IncomeCreationDTO incomeDTO,
|
public ResponseEntity<Void> updateIncome(@PathVariable long id, @RequestBody @Valid IncomeCreationDTO incomeDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
|
if(bindingResult.hasErrors())
|
||||||
|
throw new TransactionNotUpdatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||||
|
|
||||||
Income income = incomeService.getTransactionById(id);
|
Income income = incomeService.getTransactionById(id);
|
||||||
|
|
||||||
if(income == null) throw new TransactionsNotFoundException("The income has not been found");
|
if(income == null)
|
||||||
|
throw new TransactionsNotFoundException("The income has not been found");
|
||||||
|
|
||||||
|
if(!incomeService.belongsToUser(income))
|
||||||
|
throw new TransactionDoesNotBelongToTheUserException("The transaction does not belong to you");
|
||||||
|
|
||||||
IncomeCategory category = incomeCategoryService.getCategoryById(incomeDTO.getIncomeCategory());
|
IncomeCategory category = incomeCategoryService.getCategoryById(incomeDTO.getIncomeCategory());
|
||||||
income.setCategory(category);
|
income.setCategory(category);
|
||||||
income.setAmount(incomeDTO.getAmount());
|
income.setAmount(incomeDTO.getAmount());
|
||||||
|
|
||||||
if (!bindingResult.hasErrors()) {
|
|
||||||
incomeService.createOrUpdate(income);
|
incomeService.createOrUpdate(income);
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||||
} else {
|
|
||||||
throw new TransactionNotUpdatedException(ErrorResponse.from(bindingResult).getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/personal-incomes")
|
@GetMapping("/personal-incomes")
|
||||||
@@ -102,8 +106,8 @@ public class IncomeController {
|
|||||||
String email = userDetails.getUsername();
|
String email = userDetails.getUsername();
|
||||||
List<IncomeDTO> incomes;
|
List<IncomeDTO> incomes;
|
||||||
|
|
||||||
incomes = date.map(localDate -> incomeService.getTransactionsByDate(localDate).stream().map(incomeMapper::toDto).toList())
|
incomes = date.map(localDate -> incomeService.getTransactionsByDate(localDate, email).stream().map(incomeMapper::toDto).toList())
|
||||||
.orElseGet(() -> month.map(value -> incomeService.getTransactionsByMonth(value).stream().map(incomeMapper::toDto).toList())
|
.orElseGet(() -> month.map(value -> incomeService.getTransactionsByMonth(value, email).stream().map(incomeMapper::toDto).toList())
|
||||||
.orElseGet(() -> incomeService.getTransactionsByEmail(email).stream().map(incomeMapper::toDto).toList()));
|
.orElseGet(() -> incomeService.getTransactionsByEmail(email).stream().map(incomeMapper::toDto).toList()));
|
||||||
|
|
||||||
if (!incomes.isEmpty()) {
|
if (!incomes.isEmpty()) {
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ import com.faf223.expensetrackerfaf.dto.UserDTO;
|
|||||||
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
|
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
import com.faf223.expensetrackerfaf.service.UserService;
|
import com.faf223.expensetrackerfaf.service.UserService;
|
||||||
|
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotCreatedException;
|
||||||
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
@@ -26,8 +29,11 @@ public class UserController {
|
|||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
|
|
||||||
@PatchMapping()
|
@PatchMapping()
|
||||||
public ResponseEntity<UserDTO> updateUser(@RequestBody UserCreationDTO userDTO,
|
public ResponseEntity<UserDTO> updateUser(@RequestBody @Valid UserCreationDTO userDTO,
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
|
if(bindingResult.hasErrors())
|
||||||
|
throw new UserNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||||
|
|
||||||
User user = userMapper.toUser(userDTO);
|
User user = userMapper.toUser(userDTO);
|
||||||
if (!bindingResult.hasErrors()) {
|
if (!bindingResult.hasErrors()) {
|
||||||
userService.updateUser(user);
|
userService.updateUser(user);
|
||||||
@@ -37,7 +43,7 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/getUserData")
|
@GetMapping("/get-user-data")
|
||||||
public ResponseEntity<UserDTO> getUser() {
|
public ResponseEntity<UserDTO> getUser() {
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ import lombok.NoArgsConstructor;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class RegisterRequest {
|
public class RegisterRequest {
|
||||||
|
|
||||||
private String firstname; // Change field name to match JSON
|
private String firstname;
|
||||||
private String lastname; // Change field name to match JSON
|
private String lastname;
|
||||||
private String username; // Change field name to match JSON
|
private String username;
|
||||||
private String email; // Change field name to match JSON
|
private String email;
|
||||||
private String password;
|
private String password;
|
||||||
private Role role;
|
private Role role;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,19 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class ExpenseCreationDTO {
|
public class ExpenseCreationDTO {
|
||||||
|
@NotNull(message = "Category must not be null")
|
||||||
private int expenseCategory;
|
private int expenseCategory;
|
||||||
|
|
||||||
|
@NotNull(message = "Amount must not be null")
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false, message = "Amount must be positive")
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
|
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
|
||||||
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@@ -10,9 +12,19 @@ import java.time.LocalDate;
|
|||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class ExpenseDTO {
|
public class ExpenseDTO {
|
||||||
|
@NotNull(message = "ID must not be null")
|
||||||
private long expenseId;
|
private long expenseId;
|
||||||
|
|
||||||
|
@NotNull(message = "User must not be null")
|
||||||
private UserDTO userDTO;
|
private UserDTO userDTO;
|
||||||
|
|
||||||
|
@NotNull(message = "Category must not be null")
|
||||||
private ExpenseCategory expenseCategory;
|
private ExpenseCategory expenseCategory;
|
||||||
|
|
||||||
|
@NotNull(message = "Date must not be null")
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
|
|
||||||
|
@NotNull(message = "Amount must not be null")
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false, message = "Amount must be positive")
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,19 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class IncomeCreationDTO {
|
public class IncomeCreationDTO {
|
||||||
|
@NotNull(message = "Category must not be null")
|
||||||
private int incomeCategory;
|
private int incomeCategory;
|
||||||
|
|
||||||
|
@NotNull(message = "Amount must not be null")
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false, message = "Amount must be positive")
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
||||||
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@@ -10,9 +12,19 @@ import java.time.LocalDate;
|
|||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class IncomeDTO {
|
public class IncomeDTO {
|
||||||
|
@NotNull(message = "ID must not be null")
|
||||||
private long incomeId;
|
private long incomeId;
|
||||||
|
|
||||||
|
@NotNull(message = "User must not be null")
|
||||||
private UserDTO userDTO;
|
private UserDTO userDTO;
|
||||||
|
|
||||||
|
@NotNull(message = "Category must not be null")
|
||||||
private IncomeCategory incomeCategory;
|
private IncomeCategory incomeCategory;
|
||||||
|
|
||||||
|
@NotNull(message = "Date must not be null")
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
|
|
||||||
|
@NotNull(message = "Amount must not be null")
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false, message = "Amount must be positive")
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,28 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Email;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class UserCreationDTO {
|
public class UserCreationDTO {
|
||||||
|
@NotNull(message = "First name must not be null")
|
||||||
|
@NotEmpty(message = "First name must not be empty")
|
||||||
private String firstname;
|
private String firstname;
|
||||||
|
@NotNull(message = "Last name must not be null")
|
||||||
|
@NotEmpty(message = "Last name must not be empty")
|
||||||
private String lastname;
|
private String lastname;
|
||||||
|
@NotNull(message = "Username must not be null")
|
||||||
|
@NotEmpty(message = "Username must not be empty")
|
||||||
private String username;
|
private String username;
|
||||||
|
@NotNull(message = "Email must not be null")
|
||||||
|
@NotEmpty(message = "Email must not be empty")
|
||||||
|
@Email(message = "Email must be valid")
|
||||||
private String email;
|
private String email;
|
||||||
|
@NotNull(message = "Password must not be null")
|
||||||
|
@NotEmpty(message = "Password must not be empty")
|
||||||
private String password;
|
private String password;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,21 @@
|
|||||||
package com.faf223.expensetrackerfaf.dto;
|
package com.faf223.expensetrackerfaf.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class UserDTO {
|
public class UserDTO {
|
||||||
|
@NotNull(message = "Name must not be null")
|
||||||
|
@NotEmpty(message = "Name must not be empty")
|
||||||
private String name;
|
private String name;
|
||||||
|
@NotNull(message = "Surname must not be null")
|
||||||
|
@NotEmpty(message = "Surname must not be empty")
|
||||||
private String surname;
|
private String surname;
|
||||||
|
@NotNull(message = "Username must not be null")
|
||||||
|
@NotEmpty(message = "Username must not be empty")
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.*;
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -24,9 +30,14 @@ public class Expense implements IMoneyTransaction {
|
|||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "category_id")
|
@JoinColumn(name = "category_id")
|
||||||
|
@NotNull
|
||||||
private ExpenseCategory category;
|
private ExpenseCategory category;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false)
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
|
|
||||||
public Expense(LocalDate date, BigDecimal amount) {
|
public Expense(LocalDate date, BigDecimal amount) {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -12,5 +14,7 @@ public class ExpenseCategory implements IMoneyTransactionCategory {
|
|||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@Column(name = "category_name")
|
@Column(name = "category_name")
|
||||||
|
@NotNull(message = "Name must not be null")
|
||||||
|
@NotEmpty(message = "Name must not be empty")
|
||||||
private String name;
|
private String name;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package com.faf223.expensetrackerfaf.model;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
@@ -25,9 +27,14 @@ public class Income implements IMoneyTransaction {
|
|||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "category_id")
|
@JoinColumn(name = "category_id")
|
||||||
|
@NotNull
|
||||||
private IncomeCategory category;
|
private IncomeCategory category;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@DecimalMin(value = "0.0", inclusive = false)
|
||||||
private BigDecimal amount;
|
private BigDecimal amount;
|
||||||
|
|
||||||
public Income(IncomeCategory incomeCategory, LocalDate date, BigDecimal amount) {
|
public Income(IncomeCategory incomeCategory, LocalDate date, BigDecimal amount) {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -12,6 +14,8 @@ public class IncomeCategory implements IMoneyTransactionCategory {
|
|||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@Column(name = "category_name")
|
@Column(name = "category_name")
|
||||||
|
@NotNull(message = "Name must not be null")
|
||||||
|
@NotEmpty(message = "Name must not be empty")
|
||||||
private String name;
|
private String name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -17,15 +19,23 @@ public class User {
|
|||||||
private String userUuid;
|
private String userUuid;
|
||||||
|
|
||||||
@Column(name = "name")
|
@Column(name = "name")
|
||||||
|
@NotNull(message = "First name must not be null")
|
||||||
|
@NotEmpty(message = "First name must not be empty")
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
|
||||||
@Column(name = "surname")
|
@Column(name = "surname")
|
||||||
|
@NotNull(message = "Last name must not be null")
|
||||||
|
@NotEmpty(message = "Last name must not be empty")
|
||||||
private String lastName;
|
private String lastName;
|
||||||
|
|
||||||
@Column(name = "username")
|
@Column(name = "username")
|
||||||
|
@NotNull(message = "Username must not be null")
|
||||||
|
@NotEmpty(message = "Username must not be empty")
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
|
@NotNull(message = "Password must not be null")
|
||||||
|
@NotEmpty(message = "Password must not be empty")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
||||||
|
|||||||
@@ -10,11 +10,12 @@ import com.faf223.expensetrackerfaf.model.User;
|
|||||||
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||||
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||||
import com.faf223.expensetrackerfaf.security.PersonDetails;
|
import com.faf223.expensetrackerfaf.security.PersonDetails;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotAuthenticatedException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -55,7 +56,7 @@ public class AuthenticationService {
|
|||||||
public AuthenticationResponse authenticate(AuthenticationRequest request) {
|
public AuthenticationResponse authenticate(AuthenticationRequest request) {
|
||||||
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword()));
|
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(request.getEmail(), request.getPassword()));
|
||||||
|
|
||||||
Credential credential = credentialRepository.findByEmail(request.getEmail()).orElseThrow((() -> new UsernameNotFoundException("User not found")));
|
Credential credential = credentialRepository.findByEmail(request.getEmail()).orElseThrow((() -> new UserNotFoundException("User not found")));
|
||||||
|
|
||||||
UserDetails userDetails = new PersonDetails(credential);
|
UserDetails userDetails = new PersonDetails(credential);
|
||||||
String jwtToken = jwtService.generateToken(userDetails);
|
String jwtToken = jwtService.generateToken(userDetails);
|
||||||
@@ -79,7 +80,7 @@ public class AuthenticationService {
|
|||||||
.refreshToken(refreshToken)
|
.refreshToken(refreshToken)
|
||||||
.build();
|
.build();
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("Invalid or expired refresh token");
|
throw new UserNotAuthenticatedException("Invalid or expired refresh token");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
package com.faf223.expensetrackerfaf.service;
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.Credential;
|
import com.faf223.expensetrackerfaf.model.*;
|
||||||
import com.faf223.expensetrackerfaf.model.Expense;
|
|
||||||
import com.faf223.expensetrackerfaf.model.IMoneyTransaction;
|
|
||||||
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||||
import com.faf223.expensetrackerfaf.repository.ExpenseRepository;
|
import com.faf223.expensetrackerfaf.repository.ExpenseRepository;
|
||||||
|
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotAuthenticatedException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -20,6 +24,7 @@ public class ExpenseService implements ITransactionService {
|
|||||||
|
|
||||||
private final ExpenseRepository expenseRepository;
|
private final ExpenseRepository expenseRepository;
|
||||||
private final CredentialRepository credentialRepository;
|
private final CredentialRepository credentialRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
public void createOrUpdate(IMoneyTransaction expense) {
|
public void createOrUpdate(IMoneyTransaction expense) {
|
||||||
expenseRepository.save((Expense) expense);
|
expenseRepository.save((Expense) expense);
|
||||||
@@ -40,6 +45,19 @@ public class ExpenseService implements ITransactionService {
|
|||||||
return expenseRepository.findByDate(date);
|
return expenseRepository.findByDate(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Expense> getTransactionsByDate(LocalDate date, String email) {
|
||||||
|
return getTransactionsByDate(date)
|
||||||
|
.stream()
|
||||||
|
.filter(transaction -> {
|
||||||
|
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||||
|
if(credential.isEmpty())
|
||||||
|
throw new UserNotFoundException("The user has not been found");
|
||||||
|
return credential.get().getUser().equals(transaction.getUser());
|
||||||
|
})
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: store transaction month in a separate field in the DB and change this logic
|
// TODO: store transaction month in a separate field in the DB and change this logic
|
||||||
@Override
|
@Override
|
||||||
public List<Expense> getTransactionsByMonth(Month month) {
|
public List<Expense> getTransactionsByMonth(Month month) {
|
||||||
@@ -49,6 +67,19 @@ public class ExpenseService implements ITransactionService {
|
|||||||
return expenseRepository.findByDateBetween(startOfMonth, endOfMonth);
|
return expenseRepository.findByDateBetween(startOfMonth, endOfMonth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Expense> getTransactionsByMonth(Month month, String email) {
|
||||||
|
return getTransactionsByMonth(month)
|
||||||
|
.stream()
|
||||||
|
.filter(transaction -> {
|
||||||
|
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||||
|
if(credential.isEmpty())
|
||||||
|
throw new UserNotFoundException("The user has not been found");
|
||||||
|
return credential.get().getUser().equals(transaction.getUser());
|
||||||
|
})
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
public List<Expense> getTransactions() {
|
public List<Expense> getTransactions() {
|
||||||
return expenseRepository.findAll();
|
return expenseRepository.findAll();
|
||||||
}
|
}
|
||||||
@@ -60,4 +91,26 @@ public class ExpenseService implements ITransactionService {
|
|||||||
public void deleteTransactionById(long id) {
|
public void deleteTransactionById(long id) {
|
||||||
expenseRepository.deleteById(id);
|
expenseRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean belongsToUser(IMoneyTransaction transaction) {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||||
|
|
||||||
|
if(authentication.getAuthorities().stream().noneMatch(authority -> authority.getAuthority().equals("ADMIN"))) {
|
||||||
|
|
||||||
|
Optional<Credential> credential = credentialRepository.findByEmail(userDetails.getUsername());
|
||||||
|
if(credential.isEmpty()) throw new UserNotFoundException("The user has not been found");
|
||||||
|
Optional<User> user = userRepository.findById(credential.get().getUser().getUserUuid());
|
||||||
|
if(user.isEmpty()) throw new UserNotFoundException("The user has not been found");
|
||||||
|
|
||||||
|
return user.get().getExpenses().contains((Expense) transaction);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new UserNotAuthenticatedException("You are not authenticated");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.faf223.expensetrackerfaf.service;
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.IMoneyTransaction;
|
import com.faf223.expensetrackerfaf.model.IMoneyTransaction;
|
||||||
|
import com.faf223.expensetrackerfaf.model.Income;
|
||||||
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.Month;
|
import java.time.Month;
|
||||||
@@ -12,8 +14,10 @@ public interface ITransactionService {
|
|||||||
List<? extends IMoneyTransaction> getTransactions();
|
List<? extends IMoneyTransaction> getTransactions();
|
||||||
List<? extends IMoneyTransaction> getTransactionsByEmail(String email);
|
List<? extends IMoneyTransaction> getTransactionsByEmail(String email);
|
||||||
List<? extends IMoneyTransaction> getTransactionsByDate(LocalDate date);
|
List<? extends IMoneyTransaction> getTransactionsByDate(LocalDate date);
|
||||||
|
List<? extends IMoneyTransaction> getTransactionsByDate(LocalDate date, String email);
|
||||||
List<? extends IMoneyTransaction> getTransactionsByMonth(Month month);
|
List<? extends IMoneyTransaction> getTransactionsByMonth(Month month);
|
||||||
|
List<? extends IMoneyTransaction> getTransactionsByMonth(Month month, String email);
|
||||||
IMoneyTransaction getTransactionById(long id);
|
IMoneyTransaction getTransactionById(long id);
|
||||||
void deleteTransactionById(long it);
|
void deleteTransactionById(long it);
|
||||||
|
boolean belongsToUser(IMoneyTransaction transaction);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,18 @@
|
|||||||
package com.faf223.expensetrackerfaf.service;
|
package com.faf223.expensetrackerfaf.service;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.Credential;
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
import com.faf223.expensetrackerfaf.model.Expense;
|
|
||||||
import com.faf223.expensetrackerfaf.model.IMoneyTransaction;
|
import com.faf223.expensetrackerfaf.model.IMoneyTransaction;
|
||||||
import com.faf223.expensetrackerfaf.model.Income;
|
import com.faf223.expensetrackerfaf.model.Income;
|
||||||
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||||
import com.faf223.expensetrackerfaf.repository.IncomeRepository;
|
import com.faf223.expensetrackerfaf.repository.IncomeRepository;
|
||||||
|
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotAuthenticatedException;
|
||||||
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -21,6 +27,7 @@ public class IncomeService implements ITransactionService {
|
|||||||
|
|
||||||
private final IncomeRepository incomeRepository;
|
private final IncomeRepository incomeRepository;
|
||||||
private final CredentialRepository credentialRepository;
|
private final CredentialRepository credentialRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
public void createOrUpdate(IMoneyTransaction income) {
|
public void createOrUpdate(IMoneyTransaction income) {
|
||||||
incomeRepository.save((Income) income);
|
incomeRepository.save((Income) income);
|
||||||
@@ -45,6 +52,19 @@ public class IncomeService implements ITransactionService {
|
|||||||
return incomeRepository.findByDate(date);
|
return incomeRepository.findByDate(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Income> getTransactionsByDate(LocalDate date, String email) {
|
||||||
|
return getTransactionsByDate(date)
|
||||||
|
.stream()
|
||||||
|
.filter(transaction -> {
|
||||||
|
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||||
|
if(credential.isEmpty())
|
||||||
|
throw new UserNotFoundException("The user has not been found");
|
||||||
|
return credential.get().getUser().equals(transaction.getUser());
|
||||||
|
})
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: store transaction month in a separate field in the DB and change this logic
|
// TODO: store transaction month in a separate field in the DB and change this logic
|
||||||
@Override
|
@Override
|
||||||
public List<Income> getTransactionsByMonth(Month month) {
|
public List<Income> getTransactionsByMonth(Month month) {
|
||||||
@@ -54,6 +74,19 @@ public class IncomeService implements ITransactionService {
|
|||||||
return incomeRepository.findByDateBetween(startOfMonth, endOfMonth);
|
return incomeRepository.findByDateBetween(startOfMonth, endOfMonth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Income> getTransactionsByMonth(Month month, String email) {
|
||||||
|
return getTransactionsByMonth(month)
|
||||||
|
.stream()
|
||||||
|
.filter(transaction -> {
|
||||||
|
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
||||||
|
if(credential.isEmpty())
|
||||||
|
throw new UserNotFoundException("The user has not been found");
|
||||||
|
return credential.get().getUser().equals(transaction.getUser());
|
||||||
|
})
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
public Income getTransactionById(long id) {
|
public Income getTransactionById(long id) {
|
||||||
return incomeRepository.findById(id).orElse(null);
|
return incomeRepository.findById(id).orElse(null);
|
||||||
}
|
}
|
||||||
@@ -61,4 +94,26 @@ public class IncomeService implements ITransactionService {
|
|||||||
public void deleteTransactionById(long id) {
|
public void deleteTransactionById(long id) {
|
||||||
incomeRepository.deleteById(id);
|
incomeRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean belongsToUser(IMoneyTransaction transaction) {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||||
|
|
||||||
|
if(authentication.getAuthorities().stream().noneMatch(authority -> authority.getAuthority().equals("ADMIN"))) {
|
||||||
|
|
||||||
|
Optional<Credential> credential = credentialRepository.findByEmail(userDetails.getUsername());
|
||||||
|
if(credential.isEmpty()) throw new UserNotFoundException("The user has not been found");
|
||||||
|
Optional<User> user = userRepository.findById(credential.get().getUser().getUserUuid());
|
||||||
|
if(user.isEmpty()) throw new UserNotFoundException("The user has not been found");
|
||||||
|
|
||||||
|
return user.get().getIncomes().contains((Income) transaction);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new UserNotAuthenticatedException("You are not authenticated");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||||
|
|
||||||
|
public class UserNotAuthenticatedException extends RuntimeException {
|
||||||
|
public UserNotAuthenticatedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.util.exceptions;
|
||||||
|
|
||||||
|
public class UserNotCreatedException extends RuntimeException {
|
||||||
|
public UserNotCreatedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user