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

@@ -11,6 +11,8 @@ import net.gepafin.tendermanagement.service.RegionService;
import net.gepafin.tendermanagement.util.Utils;
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.stereotype.Component;
@@ -21,10 +23,11 @@ import static net.gepafin.tendermanagement.util.ObjectUtils.setIfUpdated;
@Component
public class RoleDao {
private final Logger log = LoggerFactory.getLogger(RoleDao.class);
@Autowired
private RoleRepository roleRepository;
@Autowired
private RegionService regionService;
@@ -32,55 +35,76 @@ public class RoleDao {
private RegionDao regionDao;
public RoleResponseBean createRole(RoleReq roleReq) {
log.info("Creating new role with details: {}", roleReq);
RoleEntity roleEntity = convertRoleRequestToRoleEntity(roleReq);
roleEntity = roleRepository.save(roleEntity);
log.info("Role created with ID: {}", roleEntity.getId());
return convertRoleEntityToRoleResponse(roleEntity);
}
private RoleEntity convertRoleRequestToRoleEntity(RoleReq roleReq) {
RoleEntity roleEntity = new RoleEntity();
roleEntity.setRoleName(roleReq.getRoleName());
roleEntity.setPermissions(roleReq.getPermissions());
roleEntity.setDescription(roleEntity.getDescription());
roleEntity.setDescription(roleReq.getDescription());
roleEntity.setRegion(regionService.getRegionById(roleReq.getRegionId()));
return roleEntity;
}
public RoleResponseBean convertRoleEntityToRoleResponse(RoleEntity roleEntity) {
RoleResponseBean roleResponseBean=new RoleResponseBean();
roleResponseBean.setId(roleEntity.getId());
roleResponseBean.setCreatedDate(roleEntity.getCreatedDate());
roleResponseBean.setUpdatedDate(roleEntity.getUpdatedDate());
roleResponseBean.setRoleName(roleEntity.getRoleName());
roleResponseBean.setDescription(roleEntity.getDescription());
roleResponseBean.setPermissions(roleEntity.getPermissions());
RegionResponseBean regionResponseBean = regionDao.convertRegionEntityToRegionResponse(roleEntity.getRegion());
roleResponseBean.setRegion(regionResponseBean);
RoleResponseBean roleResponseBean = new RoleResponseBean();
roleResponseBean.setId(roleEntity.getId());
roleResponseBean.setCreatedDate(roleEntity.getCreatedDate());
roleResponseBean.setUpdatedDate(roleEntity.getUpdatedDate());
roleResponseBean.setRoleName(roleEntity.getRoleName());
roleResponseBean.setDescription(roleEntity.getDescription());
roleResponseBean.setPermissions(roleEntity.getPermissions());
RegionResponseBean regionResponseBean = regionDao.convertRegionEntityToRegionResponse(roleEntity.getRegion());
roleResponseBean.setRegion(regionResponseBean);
return roleResponseBean;
}
public RoleResponseBean updateRole(Long id, RoleReq roleReq) {
RoleEntity existingUserRole = getRoleById(id);
setIfUpdated(existingUserRole::getRoleName, existingUserRole::setRoleName, roleReq.getRoleName());
setIfUpdated(existingUserRole::getDescription, existingUserRole::setDescription, roleReq.getDescription());
setIfUpdated(existingUserRole::getPermissions, existingUserRole::setPermissions, roleReq.getPermissions());
roleRepository.save(existingUserRole);
return Utils.convertObject(existingUserRole, RoleResponseBean.class);
public RoleResponseBean updateRole(Long id, RoleReq roleReq) {
log.info("Updating role with ID: {}", id);
RoleEntity existingRole = getRoleById(id);
// Log changes before update
log.info("Current role details: {}", existingRole);
log.info("New role details: {}", roleReq);
setIfUpdated(existingRole::getRoleName, existingRole::setRoleName, roleReq.getRoleName());
setIfUpdated(existingRole::getDescription, existingRole::setDescription, roleReq.getDescription());
setIfUpdated(existingRole::getPermissions, existingRole::setPermissions, roleReq.getPermissions());
existingRole = roleRepository.save(existingRole);
log.info("Role updated with ID: {}", existingRole.getId());
return Utils.convertObject(existingRole, RoleResponseBean.class);
}
public RoleEntity getRoleById(Long id) {
return roleRepository.findById(id)
log.info("Fetching role with ID: {}", id);
RoleEntity roleEntity = roleRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.ROLE_NOT_FOUND)));
log.info("Role found: {}", roleEntity);
return roleEntity;
}
public void deleteById(Long id) {
log.info("Deleting role with ID: {}", id);
roleRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,Translator.toLocale(GepafinConstant.ROLE_NOT_FOUND)));
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.ROLE_NOT_FOUND)));
roleRepository.deleteById(id);
log.info("Role deleted with ID: {}", id);
}
public List<RoleResponseBean> getAllRoles() {
return roleRepository.findAll()
log.info("Fetching all roles");
List<RoleResponseBean> roles = roleRepository.findAll()
.stream()
.map(roleEntity -> Utils.convertObject(roleEntity, RoleResponseBean.class))
.collect(Collectors.toList());
log.info("Total roles found: {}", roles.size());
return roles;
}
}