Done ticket GEPAFINBE-3
This commit is contained in:
99
src/main/java/net/gepafin/tendermanagement/dao/UserDao.java
Normal file
99
src/main/java/net/gepafin/tendermanagement/dao/UserDao.java
Normal file
@@ -0,0 +1,99 @@
|
||||
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.UpdateUserReq;
|
||||
import net.gepafin.tendermanagement.model.request.UserReq;
|
||||
import net.gepafin.tendermanagement.model.response.UserResponseBean;
|
||||
import net.gepafin.tendermanagement.repositories.UserRepository;
|
||||
import net.gepafin.tendermanagement.service.RoleService;
|
||||
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.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import static net.gepafin.tendermanagement.util.ObjectUtils.setIfUpdated;
|
||||
|
||||
@Repository
|
||||
public class UserDao {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
public UserResponseBean createUser(UserReq userReq) {
|
||||
if (!userReq.getPassword().equals(userReq.getConfPassword())) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.PASSWORD_DOESNT_MATCH));
|
||||
}
|
||||
UserEntity userEntity = convertUserRequestToUserEntity(userReq);
|
||||
userEntity = userRepository.save(userEntity);
|
||||
return convertUserEntityToUserResponse(userEntity);
|
||||
}
|
||||
|
||||
public UserResponseBean updateUser(Long userId, UpdateUserReq userReq) {
|
||||
UserEntity userEntity = userRepository.findById(userId)
|
||||
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.REGION_NOT_FOUND_MSG)));
|
||||
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);
|
||||
return convertUserEntityToUserResponse(userEntity);
|
||||
}
|
||||
|
||||
private UserEntity convertUserRequestToUserEntity(UserReq userReq) {
|
||||
UserEntity userEntity = new UserEntity();
|
||||
userEntity.setPassword(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.setId(userEntity.getId());
|
||||
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());
|
||||
userResponseBean.setRole(userEntity.getRoleEntity());
|
||||
userResponseBean.setLastLogin(userEntity.getLastLogin());
|
||||
return userResponseBean;
|
||||
}
|
||||
|
||||
public UserResponseBean getUserById(Long id) {
|
||||
UserEntity userEntity = userRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG)));
|
||||
return convertUserEntityToUserResponse(userEntity);
|
||||
}
|
||||
|
||||
public void deleteUser(Long id) {
|
||||
userRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG)));
|
||||
userRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user