Files
bflows-bandi-be/src/main/java/net/gepafin/tendermanagement/dao/UserDao.java
2024-08-21 20:48:06 +05:30

146 lines
7.3 KiB
Java

package net.gepafin.tendermanagement.dao;
import net.gepafin.tendermanagement.config.Translator;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import net.gepafin.tendermanagement.entities.RoleEntity;
import net.gepafin.tendermanagement.entities.UserEntity;
import net.gepafin.tendermanagement.model.request.LoginReq;
import net.gepafin.tendermanagement.model.request.UpdateUserReq;
import net.gepafin.tendermanagement.model.request.UserReq;
import net.gepafin.tendermanagement.model.response.RoleResponseBean;
import net.gepafin.tendermanagement.model.response.UserResponseBean;
import net.gepafin.tendermanagement.model.util.JWTToken;
import net.gepafin.tendermanagement.repositories.UserRepository;
import net.gepafin.tendermanagement.service.RoleService;
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());
setIfUpdated(userEntity::getOrganization, userEntity::setOrganization, userReq.getOrganization());
setIfUpdated(userEntity::getAddress, userEntity::setAddress, userReq.getAddress());
setIfUpdated(userEntity::getPhoneNumber, userEntity::setPhoneNumber, userReq.getPhoneNumber());
if (userReq.getRoleId() != null) {
RoleEntity roleEntity = roleService.getRoleById(userReq.getRoleId());
setIfUpdated(userEntity::getRoleEntity, userEntity::setRoleEntity, roleEntity);
}
userEntity = userRepository.save(userEntity);
log.info("User updated with ID: {}", userEntity.getId());
return convertUserEntityToUserResponse(userEntity);
}
private UserEntity convertUserRequestToUserEntity(UserReq userReq) {
UserEntity userEntity = new UserEntity();
userEntity.setPassword(passwordEncoder.encode(userReq.getPassword()));
userEntity.setEmail(userReq.getEmail());
userEntity.setFirstName(userReq.getFirstName());
userEntity.setStatus(userReq.getStatus());
userEntity.setLastName(userReq.getLastName());
userEntity.setOrganization(userReq.getOrganization());
userEntity.setAddress(userReq.getAddress());
userEntity.setPhoneNumber(userReq.getPhoneNumber());
userEntity.setRoleEntity(roleService.getRoleById(userReq.getRoleId()));
return userEntity;
}
private UserResponseBean convertUserEntityToUserResponse(UserEntity userEntity) {
UserResponseBean userResponseBean = new UserResponseBean();
userResponseBean.setId(userEntity.getId());
userResponseBean.setCreatedDate(userEntity.getCreatedDate());
userResponseBean.setUpdatedDate(userEntity.getUpdatedDate());
userResponseBean.setEmail(userEntity.getEmail());
userResponseBean.setFirstName(userEntity.getFirstName());
userResponseBean.setLastName(userEntity.getLastName());
userResponseBean.setPhoneNumber(userEntity.getPhoneNumber());
userResponseBean.setOrganization(userEntity.getOrganization());
userResponseBean.setAddress(userEntity.getAddress());
userResponseBean.setCity(userEntity.getCity());
userResponseBean.setCountry(userEntity.getCountry());
userResponseBean.setStatus(userEntity.getStatus());
RoleResponseBean roleResponseBean = roleDao.convertRoleEntityToRoleResponse(userEntity.getRoleEntity());
userResponseBean.setRole(roleResponseBean);
userResponseBean.setLastLogin(userEntity.getLastLogin());
return userResponseBean;
}
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)));
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)));
userRepository.deleteById(id);
log.info("User deleted with ID: {}", id);
}
public JWTToken login(LoginReq 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;
}
}