Add Logger

This commit is contained in:
harish
2024-08-21 19:54:27 +05:30
parent 7a080504aa
commit 04e9ff57a7
5 changed files with 164 additions and 76 deletions

View File

@@ -18,45 +18,61 @@ import net.gepafin.tendermanagement.service.impl.AuthenticationService;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;
import static net.gepafin.tendermanagement.util.ObjectUtils.setIfUpdated;
@Repository
public class UserDao {
private final Logger log = LoggerFactory.getLogger(UserDao.class);
@Autowired
private UserRepository userRepository;
@Autowired
private AuthenticationService authService;
@Autowired
private RoleService roleService;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private RoleDao roleDao;
public UserResponseBean createUser(UserReq userReq) {
log.info("Creating user with email: {}", userReq.getEmail());
if (userRepository.existsByEmail(userReq.getEmail())) {
log.error("User creation failed: Email {} already exists", userReq.getEmail());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.EMAIL_ALREADY_EXISTS));
}
if (!userReq.getPassword().equals(userReq.getConfPassword())) {
log.error("User creation failed: Passwords do not match for email {}", userReq.getEmail());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_DOESNT_MATCH));
}
if (userReq.getPassword().length() < 8) {
log.error("User creation failed: Password length is less than 8 characters for email {}", userReq.getEmail());
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_MIN_LEN));
}
UserEntity userEntity = convertUserRequestToUserEntity(userReq);
userEntity = userRepository.save(userEntity);
log.info("User created with ID: {}", userEntity.getId());
return convertUserEntityToUserResponse(userEntity);
}
public UserResponseBean updateUser(Long userId, UpdateUserReq userReq) {
log.info("Updating user with ID: {}", userId);
UserEntity userEntity = userRepository.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.REGION_NOT_FOUND_MSG)));
log.info("Current user details: {}", userEntity);
log.info("New user details: {}", userReq);
setIfUpdated(userEntity::getStatus, userEntity::setStatus, userReq.getStatus());
setIfUpdated(userEntity::getFirstName, userEntity::setFirstName, userReq.getFirstName());
setIfUpdated(userEntity::getLastName, userEntity::setLastName, userReq.getLastName());
@@ -68,6 +84,7 @@ public class UserDao {
setIfUpdated(userEntity::getRoleEntity, userEntity::setRoleEntity, roleEntity);
}
userEntity = userRepository.save(userEntity);
log.info("User updated with ID: {}", userEntity.getId());
return convertUserEntityToUserResponse(userEntity);
}
@@ -90,7 +107,6 @@ public class UserDao {
userResponseBean.setId(userEntity.getId());
userResponseBean.setCreatedDate(userEntity.getCreatedDate());
userResponseBean.setUpdatedDate(userEntity.getUpdatedDate());
userResponseBean.setId(userEntity.getId());
userResponseBean.setEmail(userEntity.getEmail());
userResponseBean.setFirstName(userEntity.getFirstName());
userResponseBean.setLastName(userEntity.getLastName());
@@ -107,19 +123,25 @@ public class UserDao {
}
public UserResponseBean getUserById(Long id) {
log.info("Fetching user with ID: {}", id);
UserEntity userEntity = userRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG)));
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG)));
log.info("User found: {}", userEntity);
return convertUserEntityToUserResponse(userEntity);
}
public void deleteUser(Long id) {
log.info("Deleting user with ID: {}", id);
userRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG)));
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG)));
userRepository.deleteById(id);
log.info("User deleted with ID: {}", id);
}
public JWTToken login(LoginReq loginReq) {
return authService.login(loginReq);
log.info("User login attempt for email: {}", loginReq.getEmail());
JWTToken jwtToken = authService.login(loginReq);
log.info("Login successful for email: {}", loginReq.getEmail());
return jwtToken;
}
}