Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/com/satwik/splitora/constants/ErrorMessages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.satwik.splitora.constants;

public final class ErrorMessages {

private ErrorMessages() {}

public static final String EXPENSE_NOT_FOUND = "Expense not found";

public static final String GROUP_NOT_FOUND = "Group not found";

}
23 changes: 5 additions & 18 deletions src/main/java/com/satwik/splitora/controller/AuthController.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.satwik.splitora.controller;

import com.satwik.splitora.configuration.jwt.JwtUtil;
import com.satwik.splitora.persistence.dto.ResponseModel;
import com.satwik.splitora.persistence.dto.user.AuthenticationResponse;
import com.satwik.splitora.persistence.dto.user.LoginRequest;
import com.satwik.splitora.persistence.dto.user.RefreshTokenRequest;
import com.satwik.splitora.repository.UserRepository;
import com.satwik.splitora.service.interfaces.AuthService;
import com.satwik.splitora.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

@Slf4j
Expand All @@ -22,20 +17,12 @@
@RequestMapping("/api/v1/auth")
public class AuthController {

@Autowired
private AuthenticationManager authenticationManager;
private final AuthService authService;

@Autowired
private AuthService authService;

@Autowired
private UserRepository userRepository;

@Autowired
private PasswordEncoder passwordEncoder;

@Autowired
private JwtUtil jwtUtil;
public AuthController(
AuthService authService) {
this.authService = authService;
}

/**
* Handles the login request for a user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.satwik.splitora.service.interfaces.ExpenseService;
import com.satwik.splitora.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -18,8 +17,11 @@
@RequestMapping("/api/v1/expense")
public class ExpenseController {

@Autowired
ExpenseService expenseService;
private final ExpenseService expenseService;

public ExpenseController (ExpenseService expenseService) {
this.expenseService = expenseService;
}

/**
* Creates a new expense which is not grouped.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.satwik.splitora.controller;

import com.satwik.splitora.configuration.security.LoggedInUser;
import com.satwik.splitora.persistence.dto.ResponseModel;
import com.satwik.splitora.persistence.dto.group.GroupDTO;
import com.satwik.splitora.persistence.dto.group.GroupListDTO;
Expand All @@ -10,7 +9,6 @@
import com.satwik.splitora.util.ResponseUtil;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -23,11 +21,11 @@
@RequestMapping("/api/v1/group")
public class GroupController {

@Autowired
GroupService groupService;
private final GroupService groupService;

@Autowired
LoggedInUser loggedInUser;
public GroupController (GroupService groupService) {
this.groupService = groupService;
}

/**
* Creates a group for a user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.satwik.splitora.service.interfaces.OAuthService;
import com.satwik.splitora.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -16,8 +15,11 @@
@RequestMapping("/api/v1/oauth2")
public class OAuthController {

@Autowired
OAuthService oAuthService;
private final OAuthService oAuthService;

public OAuthController(OAuthService oAuthService) {
this.oAuthService = oAuthService;
}

/**
* Handles the OAuth2 callback.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
@RequestMapping("/api/v1/report")
public class ReportController {

@Autowired
ReportService reportService;

@Autowired
LoggedInUser loggedInUser;
public ReportController (ReportService reportService) {
this.reportService = reportService;
}

/**
* Generates a report for a specific group.
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/satwik/splitora/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.satwik.splitora.util.ResponseUtil;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -18,11 +17,14 @@
@RequestMapping("/api/v1/user")
public class UserController {

@Autowired
private UserService userService;
private final UserService userService;

@Autowired
private LoggedInUser loggedInUser;
private final LoggedInUser loggedInUser;

public UserController (UserService userService, LoggedInUser loggedInUser) {
this.userService = userService;
this.loggedInUser = loggedInUser;
}

/**
* Registers a new user.
Expand Down Expand Up @@ -59,7 +61,7 @@ public ResponseEntity<ResponseModel<UserDTO>> getUser() {
* @param updateUserRequest the request object containing updated user details.
* @return a ResponseEntity containing a ResponseModel with the result of the update process.
*/
@PutMapping("update/{userId}")
@PutMapping("/update")
public ResponseEntity<ResponseModel<String>> updateUser(@Valid @RequestBody RegisterUserRequest updateUserRequest) {
log.info("Put Endpoint: update user with id: {}, and register user request: {}", loggedInUser.getUserId(), updateUserRequest);
String response = userService.updateUser(updateUserRequest);
Expand All @@ -73,7 +75,7 @@ public ResponseEntity<ResponseModel<String>> updateUser(@Valid @RequestBody Regi
*
* @return a ResponseEntity containing a ResponseModel with the result of the deletion process.
*/
@DeleteMapping("delete")
@DeleteMapping("/delete")
public ResponseEntity<ResponseModel<String>> deleteUser() {
log.info("Delete Endpoint: delete user");
String response = userService.deleteUser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,4 @@ public class UserDTO {

@NotNull
PhoneDTO phone;

UserDTO(UUID userId, String username, String email, String countryCode, Long phoneNumber) {
this.userId = userId;
this.username = username;
this.email = email;
this.phone = new PhoneDTO(countryCode, phoneNumber);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class BaseEntity {

@Id
@GenericGenerator(name = "uuid", strategy = "uuid2")
@GenericGenerator(name = "uuid")
@GeneratedValue(generator = "uuid")
private UUID id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ public interface UserRepository extends JpaRepository<User, UUID> {

Optional<User> findByEmail(String email);

boolean existsByEmail(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@
import com.satwik.splitora.persistence.entities.User;
import com.satwik.splitora.repository.UserRepository;
import com.satwik.splitora.service.interfaces.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl implements AuthService {

@Autowired
JwtUtil jwtUtil;

@Autowired
UserRepository userRepository;

@Autowired
PasswordEncoder passwordEncoder;

public AuthServiceImpl(JwtUtil jwtUtil, UserRepository userRepository, PasswordEncoder passwordEncoder) {
this.jwtUtil = jwtUtil;
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}

@Override
public AuthenticationResponse authenticateUser(LoginRequest loginRequest) {
User user = userRepository.findByEmail(loginRequest.getUserEmail()).orElseThrow(() -> new DataNotFoundException("User not found."));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.satwik.splitora.service.implementations;

import com.satwik.splitora.configuration.security.LoggedInUser;
import com.satwik.splitora.constants.ErrorMessages;
import com.satwik.splitora.constants.enums.UserRole;
import com.satwik.splitora.exception.DataNotFoundException;
import com.satwik.splitora.persistence.entities.Expense;
Expand All @@ -9,26 +10,30 @@
import com.satwik.splitora.repository.ExpenseRepository;
import com.satwik.splitora.repository.GroupRepository;
import com.satwik.splitora.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component("authorizationService")
public class AuthorizationService {

@Autowired
LoggedInUser loggedInUser;
private final LoggedInUser loggedInUser;

@Autowired
UserRepository userRepository;
private final UserRepository userRepository;

@Autowired
GroupRepository groupRepository;
private final GroupRepository groupRepository;

@Autowired
ExpenseRepository expenseRepository;
private final ExpenseRepository expenseRepository;

public AuthorizationService(LoggedInUser loggedInUser,
UserRepository userRepository,
GroupRepository groupRepository,
ExpenseRepository expenseRepository) {
this.loggedInUser = loggedInUser;
this.userRepository = userRepository;
this.groupRepository = groupRepository;
this.expenseRepository = expenseRepository;
}

public User getAuthorizedUser() {
return userRepository.findByEmail(loggedInUser.getUserEmail()).orElseThrow(() -> new DataNotFoundException("User not found"));
Expand All @@ -38,7 +43,7 @@ public boolean isGroupOwner(UUID groupId) {
if (loggedInUser.hasRole(UserRole.ADMIN))
return true;

Group group = groupRepository.findById(groupId).orElseThrow(() -> new DataNotFoundException("Group not found"));
Group group = groupRepository.findById(groupId).orElseThrow(() -> new DataNotFoundException(ErrorMessages.GROUP_NOT_FOUND));
UUID ownerId = group.getUser().getId();
return loggedInUser.getUserId().equals(ownerId);
}
Expand All @@ -47,7 +52,7 @@ public boolean isExpenseOwner(UUID expenseId) {
if (loggedInUser.hasRole(UserRole.ADMIN))
return true;

Expense expense = expenseRepository.findById(expenseId).orElseThrow(() -> new DataNotFoundException("Expense not found"));
Expense expense = expenseRepository.findById(expenseId).orElseThrow(() -> new DataNotFoundException(ErrorMessages.EXPENSE_NOT_FOUND));
UUID ownerId = expense.getPayer().getId();
return loggedInUser.getUserId().equals(ownerId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.satwik.splitora.persistence.entities.User;
import com.satwik.splitora.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
Expand All @@ -11,8 +10,11 @@
@Service
public class CustomUserDetailsService implements UserDetailsService {

@Autowired
private UserRepository userRepository;
private final UserRepository userRepository;

public CustomUserDetailsService(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
Expand Down
Loading