OneCompiler

wet

107

package com.example.LeavesProj.Service;

import com.example.LeavesProj.Dto.EmployeeDto;
import com.example.LeavesProj.Dto.ManagerDto;
import com.example.LeavesProj.Enum.Role;
import com.example.LeavesProj.Model.Manager;
import com.example.LeavesProj.Model.Users;
import com.example.LeavesProj.Repo.ManagerRepo;
import com.example.LeavesProj.Repo.UsersRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class RegisterService {
@Autowired
private UsersRepo usersRepo;
@Autowired
private ManagerRepo managerRepo;
@Autowired
private PasswordEncoder passwordEncoder;

public EmployeeDto createuser(Users users){
    if(usersRepo.existsByUsername(users.getUsername())){
        throw new RuntimeException("User Already Exists");
    }
    Manager manager=managerRepo.findByProject(users.getProject());
    users.setPassword(passwordEncoder.encode(users.getPassword()));
    users.setManager(manager);
    users.setBalanceleaves(10);
    usersRepo.save(users);
    EmployeeDto employeeDto=new EmployeeDto();
    employeeDto.setId(users.getId());
    employeeDto.setEmail(users.getEmail());
    employeeDto.setRole(users.getRole());
    employeeDto.setManagerid(manager.getManagerId());
    employeeDto.setLeavebalance(users.getBalanceleaves());
    employeeDto.setUsername(users.getUsername());
    employeeDto.setProject(users.getProject());
    return employeeDto;
}
public ManagerDto createManager(Manager manager){
    if (managerRepo.existsByProject(manager.getProject())|| managerRepo.existsByUsername(manager.getUsername())){
        throw new RuntimeException("Manager Already Exists");
    }
    manager.setPassword(passwordEncoder.encode(manager.getPassword()));
    manager.setRole(Role.MANAGER);
    managerRepo.save(manager);
    ManagerDto managerDto=new ManagerDto();
    managerDto.setEmail(manager.getEmail());
    managerDto.setUsername(manager.getUsername());
    managerDto.setManagerid(manager.getManagerId());
    managerDto.setProject(manager.getProject());
    managerDto.setRole(manager.getRole());
    return managerDto;
}
public Users findByUsername(String username){
    return usersRepo.findByUsername(username);
}
public Manager findByManagerUsername(String username){
    return managerRepo.findByUsername(username);
}

}

package com.example.LeavesProj.Exception;

public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
}

public UserNotFoundException(String message, Throwable cause) {
    super(message, cause);
}

}

package com.example.LeavesProj.Exception;

import org.springframework.http.HttpStatus;

public class UserException {
private final String message;
private final Throwable throwable;
private final HttpStatus httpStatus;
//constructor
public UserException(String message, Throwable throwable, HttpStatus httpStatus) {
this.message = message;
this.throwable = throwable;
this.httpStatus = httpStatus;
}

public String getMessage() {
    return message;
}

public Throwable getThrowable() {
    return throwable;
}

public HttpStatus getHttpStatus() {
    return httpStatus;
}

}

package com.example.LeavesProj.Exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class UserExceptionController {

@ExceptionHandler(value = UserNotFoundException.class)

public ResponseEntity<Object> handleUserNotFoundException (
        UserNotFoundException userNotFoundException) {
    UserException userException = new UserException(
            userNotFoundException.getMessage(),
            userNotFoundException.getCause(),
            HttpStatus.NOT_FOUND);
    return new ResponseEntity<>(userException, HttpStatus.NOT_FOUND);
}

}