Fix bugs caused by Expense/Income controllers
This commit is contained in:
@@ -3,7 +3,7 @@ package com.faf223.expensetrackerfaf.config;
|
|||||||
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 org.springframework.beans.factory.annotation.Autowired;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
@@ -16,17 +16,12 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class ApplicationConfig {
|
public class ApplicationConfig {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final CredentialRepository credentialRepository;
|
private final CredentialRepository credentialRepository;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public ApplicationConfig(UserRepository userRepository, CredentialRepository credentialRepository) {
|
|
||||||
this.userRepository = userRepository;
|
|
||||||
this.credentialRepository = credentialRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public UserDetailsService userDetailsService() {
|
public UserDetailsService userDetailsService() {
|
||||||
return username -> new PersonDetails(credentialRepository.findByEmail(username).orElseThrow((() -> new UsernameNotFoundException("User not found"))));
|
return username -> new PersonDetails(credentialRepository.findByEmail(username).orElseThrow((() -> new UsernameNotFoundException("User not found"))));
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import jakarta.servlet.FilterChain;
|
|||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
@@ -16,16 +17,12 @@ import org.springframework.web.filter.OncePerRequestFilter;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
private final JwtService jwtService;
|
private final JwtService jwtService;
|
||||||
private final UserDetailsService userDetailsService;
|
private final UserDetailsService userDetailsService;
|
||||||
|
|
||||||
public JwtAuthenticationFilter(JwtService jwtService, UserDetailsService userDetailsService) {
|
|
||||||
this.jwtService = jwtService;
|
|
||||||
this.userDetailsService = userDetailsService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(
|
protected void doFilterInternal(
|
||||||
@NonNull HttpServletRequest request,
|
@NonNull HttpServletRequest request,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.faf223.expensetrackerfaf.config;
|
package com.faf223.expensetrackerfaf.config;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.authentication.AuthenticationProvider;
|
import org.springframework.security.authentication.AuthenticationProvider;
|
||||||
@@ -12,17 +12,12 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class SecurityConfiguration {
|
public class SecurityConfiguration {
|
||||||
|
|
||||||
private final JwtAuthenticationFilter jwtAuthFilter;
|
private final JwtAuthenticationFilter jwtAuthFilter;
|
||||||
private final AuthenticationProvider authenticationProvider;
|
private final AuthenticationProvider authenticationProvider;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public SecurityConfiguration(JwtAuthenticationFilter jwtAuthFilter, AuthenticationProvider authenticationProvider) {
|
|
||||||
this.jwtAuthFilter = jwtAuthFilter;
|
|
||||||
this.authenticationProvider = authenticationProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||||
http
|
http
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.faf223.expensetrackerfaf.controller;
|
|||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.Expense;
|
import com.faf223.expensetrackerfaf.model.Expense;
|
||||||
import com.faf223.expensetrackerfaf.service.ExpenseService;
|
import com.faf223.expensetrackerfaf.service.ExpenseService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
@@ -13,10 +13,10 @@ import java.util.List;
|
|||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/expenses")
|
@RequestMapping("/expenses")
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class ExpenseController {
|
public class ExpenseController {
|
||||||
|
|
||||||
@Autowired
|
private final ExpenseService expenseService;
|
||||||
private ExpenseService expenseService;
|
|
||||||
|
|
||||||
@GetMapping("/user/{userUuid}")
|
@GetMapping("/user/{userUuid}")
|
||||||
public ResponseEntity<List<Expense>> getExpensesByUser(@PathVariable String userUuid) {
|
public ResponseEntity<List<Expense>> getExpensesByUser(@PathVariable String userUuid) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.faf223.expensetrackerfaf.controller;
|
|||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.Income;
|
import com.faf223.expensetrackerfaf.model.Income;
|
||||||
import com.faf223.expensetrackerfaf.service.IncomeService;
|
import com.faf223.expensetrackerfaf.service.IncomeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
@@ -13,9 +13,10 @@ import java.util.List;
|
|||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/incomes")
|
@RequestMapping("/incomes")
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class IncomeController {
|
public class IncomeController {
|
||||||
@Autowired
|
|
||||||
private IncomeService incomeService;
|
private final IncomeService incomeService;
|
||||||
|
|
||||||
@GetMapping("/user/{userUuid}")
|
@GetMapping("/user/{userUuid}")
|
||||||
public ResponseEntity<List<Income>> getIncomesByUser(@PathVariable String userUuid) {
|
public ResponseEntity<List<Income>> getIncomesByUser(@PathVariable String userUuid) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.faf223.expensetrackerfaf.controller;
|
|||||||
|
|
||||||
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 org.springframework.beans.factory.annotation.Autowired;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
@@ -11,13 +11,14 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/users")
|
@RequestMapping("/users")
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
@Autowired
|
private final UserService userService;
|
||||||
private UserService userService;
|
|
||||||
|
|
||||||
@GetMapping("/{userUuid}")
|
@GetMapping("/{userUuid}")
|
||||||
public ResponseEntity<User> getUser(@PathVariable String userUuid) {
|
public ResponseEntity<User> getUser(@PathVariable String userUuid) {
|
||||||
|
// TODO: Create a DTO class that will be returned instead of User(password: null and uuid are returned inside of the user object)
|
||||||
User user = userService.getUserById(userUuid);
|
User user = userService.getUserById(userUuid);
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
return ResponseEntity.ok(user);
|
return ResponseEntity.ok(user);
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -12,8 +14,10 @@ public class Expense {
|
|||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long expenseId;
|
private Long expenseId;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne()
|
||||||
@JoinColumn(name = "user_uuid")
|
@JoinColumn(name = "user_uuid")
|
||||||
|
@ToString.Exclude
|
||||||
|
@JsonIgnore
|
||||||
private User user;
|
private User user;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -15,6 +17,8 @@ public class Income {
|
|||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "user_uuid")
|
@JoinColumn(name = "user_uuid")
|
||||||
|
@ToString.Exclude
|
||||||
|
@JsonIgnore
|
||||||
private User user;
|
private User user;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
|
|||||||
@@ -2,10 +2,7 @@
|
|||||||
package com.faf223.expensetrackerfaf.model;
|
package com.faf223.expensetrackerfaf.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.*;
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -32,9 +29,11 @@ public class User {
|
|||||||
@Transient
|
@Transient
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user")
|
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
||||||
|
@ToString.Exclude
|
||||||
private List<Expense> expenses;
|
private List<Expense> expenses;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user")
|
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
||||||
|
@ToString.Exclude
|
||||||
private List<Income> incomes;
|
private List<Income> incomes;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.faf223.expensetrackerfaf.security;
|
package com.faf223.expensetrackerfaf.security;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.Credential;
|
import com.faf223.expensetrackerfaf.model.Credential;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
@@ -14,15 +15,11 @@ import java.util.List;
|
|||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor(force = true)
|
@NoArgsConstructor(force = true)
|
||||||
//@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class PersonDetails implements UserDetails {
|
public class PersonDetails implements UserDetails {
|
||||||
|
|
||||||
private final Credential credential;
|
private final Credential credential;
|
||||||
|
|
||||||
public PersonDetails(Credential credential) {
|
|
||||||
this.credential = credential;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||||
return List.of(new SimpleGrantedAuthority(credential.getRole().name()));
|
return List.of(new SimpleGrantedAuthority(credential.getRole().name()));
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ 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 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.UsernameNotFoundException;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
@@ -16,6 +17,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class AuthenticationService {
|
public class AuthenticationService {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
@@ -24,14 +26,6 @@ public class AuthenticationService {
|
|||||||
private final JwtService jwtService;
|
private final JwtService jwtService;
|
||||||
private final AuthenticationManager authenticationManager;
|
private final AuthenticationManager authenticationManager;
|
||||||
|
|
||||||
public AuthenticationService(UserRepository repository, CredentialRepository credentialRepository, PasswordEncoder passwordEncoder, JwtService jwtService, AuthenticationManager authenticationManager) {
|
|
||||||
this.userRepository = repository;
|
|
||||||
this.credentialRepository = credentialRepository;
|
|
||||||
this.passwordEncoder = passwordEncoder;
|
|
||||||
this.jwtService = jwtService;
|
|
||||||
this.authenticationManager = authenticationManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AuthenticationResponse register(RegisterRequest request) {
|
public AuthenticationResponse register(RegisterRequest request) {
|
||||||
|
|
||||||
User user = User.builder()
|
User user = User.builder()
|
||||||
|
|||||||
@@ -2,16 +2,16 @@ package com.faf223.expensetrackerfaf.service;
|
|||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.Expense;
|
import com.faf223.expensetrackerfaf.model.Expense;
|
||||||
import com.faf223.expensetrackerfaf.repository.ExpenseRepository;
|
import com.faf223.expensetrackerfaf.repository.ExpenseRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class ExpenseService {
|
public class ExpenseService {
|
||||||
|
|
||||||
@Autowired
|
private final ExpenseRepository expenseRepository;
|
||||||
private ExpenseRepository expenseRepository;
|
|
||||||
|
|
||||||
public List<Expense> getExpensesByUserId(String userUuid) {
|
public List<Expense> getExpensesByUserId(String userUuid) {
|
||||||
return expenseRepository.findByUserUserUuid(userUuid);
|
return expenseRepository.findByUserUserUuid(userUuid);
|
||||||
|
|||||||
@@ -2,15 +2,16 @@ package com.faf223.expensetrackerfaf.service;
|
|||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.Income;
|
import com.faf223.expensetrackerfaf.model.Income;
|
||||||
import com.faf223.expensetrackerfaf.repository.IncomeRepository;
|
import com.faf223.expensetrackerfaf.repository.IncomeRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class IncomeService {
|
public class IncomeService {
|
||||||
@Autowired
|
|
||||||
private IncomeRepository incomeRepository;
|
private final IncomeRepository incomeRepository;
|
||||||
|
|
||||||
public List<Income> getIncomesByUserId(String userUuid) {
|
public List<Income> getIncomesByUserId(String userUuid) {
|
||||||
return incomeRepository.findByUserUserUuid(userUuid);
|
return incomeRepository.findByUserUserUuid(userUuid);
|
||||||
|
|||||||
@@ -2,14 +2,14 @@ package com.faf223.expensetrackerfaf.service;
|
|||||||
|
|
||||||
import com.faf223.expensetrackerfaf.model.User;
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class UserService {
|
public class UserService {
|
||||||
|
|
||||||
@Autowired
|
private final UserRepository userRepository;
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
public User getUserById(String userUuid) {
|
public User getUserById(String userUuid) {
|
||||||
return userRepository.findById(userUuid).orElse(null);
|
return userRepository.findById(userUuid).orElse(null);
|
||||||
|
|||||||
Reference in New Issue
Block a user