package com.eactive.httpmockserver.common.validator;

import com.eactive.httpmockserver.common.exception.UserNotFoundException;
import com.eactive.httpmockserver.common.util.ApplicationContextUtil;
import com.eactive.httpmockserver.user.service.UserService;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class UniqueUserIdValidator implements ConstraintValidator<UniqueId, String> {
    @Override
    public boolean isValid(String id, ConstraintValidatorContext constraintValidatorContext) {

        UserService userService = ApplicationContextUtil.getContext().getBean(UserService.class);
        try {
            userService.findByUserId(id);
            return false;
        } catch (UserNotFoundException e) {
            return true;
        }
    }
}
