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