Added endpoint to get all users

This commit is contained in:
harish
2024-10-16 17:12:31 +05:30
parent edd9c1cb1a
commit 38db6127f7
10 changed files with 61 additions and 10 deletions

View File

@@ -230,6 +230,7 @@ public class GepafinConstant {
public static final String ATTEMPT_DATE = "attemptDate";
public static final String LOGIN_ATTEMPTED_CREATED_SUCCESSFULLY="login_attempt_successfully_created";
public static final String GET_LOGIN_ATTEMPT_MSG="get_login_attempt_se_msg";
public static final String GET_USERS_SUCCESS_MSG = "get.users.success.msg";
}

View File

@@ -33,6 +33,7 @@ import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.stream.Collectors;
import static net.gepafin.tendermanagement.util.Utils.setIfUpdated;
@@ -354,5 +355,22 @@ public class UserDao {
return authService.validateNewUserToken(token);
}
public List<UserResponseBean> getAllUsers(Long roleId) {
List<UserEntity> users;
if (roleId != null) {
log.info("Fetching users by role ID: {}", roleId);
users = userRepository.findByRoleEntityId(roleId);
} else {
log.info("Fetching all users");
users = userRepository.findAll();
}
List<UserResponseBean> userResponseBeans = users.stream()
.map(this::convertUserEntityToUserResponse)
.collect(Collectors.toList());
log.info("Total users found with role ID {}: {}", roleId, userResponseBeans.size());
return userResponseBeans;
}
}

View File

@@ -1,9 +1,11 @@
package net.gepafin.tendermanagement.repositories;
import net.gepafin.tendermanagement.entities.UserEntity;
import net.gepafin.tendermanagement.model.response.UserResponseBean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
@@ -21,5 +23,5 @@ public interface UserRepository extends JpaRepository<UserEntity, Long> {
UserEntity findByBeneficiaryId(Long beneficiaryId);
Long countByStatusAndRoleEntity_RoleType(String status, String roleName);
List<UserEntity> findByRoleEntityId(Long roleId);
}

View File

@@ -12,6 +12,8 @@ import net.gepafin.tendermanagement.model.response.UserSamlResponse;
import net.gepafin.tendermanagement.model.response.UserResponseBean;
import net.gepafin.tendermanagement.model.util.JWTToken;
import java.util.List;
public interface UserService {
JWTToken createUser(HttpServletRequest request, String tempToken, UserReq userReq);
@@ -43,5 +45,6 @@ public interface UserService {
UserEntity getUserByBeneficiaryId(Long beneficiaryId);
public UserEntity getUserEntityById(Long userId);
List<UserResponseBean> getAllUsers(Long roleId);
}

View File

@@ -72,7 +72,7 @@ public class AuthenticationService {
public JWTToken login(LoginReq loginReq,HttpServletRequest request) {
UserEntity user=null;
try {
LoginAttemptEntity loginAttemptEntity = prepareLoginAttemptEntity(loginReq, request);
log.info("Attempting login for email: {}", loginReq.getEmail());
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
@@ -89,10 +89,6 @@ public class AuthenticationService {
Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG));
}
createSuccessLoginAttempt(loginAttemptEntity);
} catch (Exception e) {
}
return getJWTTokenBean(user, loginReq.getRememberMe());
}

View File

@@ -21,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@@ -124,4 +125,10 @@ public class UserServiceImpl implements UserService {
// Calling DAO Function
return userDao.validateUser(userId);
}
@Override
@Transactional(readOnly = true)
public List<UserResponseBean> getAllUsers(Long roleId) {
// Calling DAO Function
return userDao.getAllUsers(roleId);
}
}

View File

@@ -22,6 +22,8 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Validated
public interface UserApi {
@@ -219,6 +221,18 @@ public interface UserApi {
ResponseEntity<Response<UserSamlResponse>> validateNewUserToken(HttpServletRequest request,
@Parameter(description = "The spid token", required = true) @PathVariable("token") String token);
@Operation(summary = "Api to get all users",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.NOTFOUND_ERROR_EXAMPLE)})),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.UNAUTHORIZED_ERROR_EXAMPLE)})),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@ExampleObject(value = ErrorConstants.BADREQUEST_ERROR_EXAMPLE)}))})
@RequestMapping(value = "", produces = {"application/json"}, method = RequestMethod.GET)
ResponseEntity<Response<List<UserResponseBean>>> getAllUsers(
@Parameter( required = false)@RequestParam(value ="roleId", required = false) Long roleId);
@RequestMapping("favicon.ico")

View File

@@ -22,6 +22,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("${openapi.gepafin.base-path:/v1/user}")
@@ -139,7 +141,14 @@ public class UserApiController implements UserApi {
UserSamlResponse data = userService.validateNewUserToken(request,token);
return ResponseEntity.ok(new Response<>(data, Status.SUCCESS, Translator.toLocale(GepafinConstant.TOKEN_VALIDATE_SUCCESS_MSE)));
}
@Override
public ResponseEntity<Response<List<UserResponseBean>>> getAllUsers(
Long roleId) {
log.info("Get all Users by Role ID - Role ID: {}", roleId);
List<UserResponseBean> users = userService.getAllUsers(roleId);
return ResponseEntity.status(HttpStatus.OK)
.body(new Response<>(users, Status.SUCCESS, Translator.toLocale(GepafinConstant.GET_USERS_SUCCESS_MSG)));
}
@Override
public void returnNoFavicon() {

View File

@@ -251,4 +251,4 @@ dashboard.widget.fetched.successfully=Dasboard widget fetched sucessfully.
login_attempt_successfully_created = Login attempt successfully created.
get_login_attempt_se_msg=Login attempts fetched successfully.
get.users.success.msg = Successfully fetched users.

View File

@@ -247,3 +247,4 @@ dashboard.widget.fetched.successfully=Widget dashboard recuperato correttamente.
login_attempt_successfully_created= Tentativo di login creato con successo.
get_login_attempt_se_msg=Lista dei tentativi di accesso recuperata correttamente.
get.users.success.msg = Utenti recuperati con successo