Task: Add check username already existed when try to mutation a new user in graphql demo

This commit is contained in:
Sambo Chea 2021-08-07 12:31:48 +07:00
parent 66defb6669
commit 0bb930a128
2 changed files with 5 additions and 0 deletions

View File

@ -16,6 +16,8 @@ class UserMutation @Autowired constructor(
) : GraphQLMutationResolver {
@Transactional(propagation = Propagation.REQUIRES_NEW)
fun createUser(input: UserInput): User {
if (userRepository.existsAllByUsername(input.username ?: "")) throw Exception("Username has been already existed!")
val user = UserMapper.fromInputToUser(input)
return userRepository.save(user)
}

View File

@ -11,4 +11,7 @@ import org.springframework.stereotype.Repository
interface UserRepository : JpaRepository<User, Long> {
@Query("select u from User u where u.enabled = true")
fun queryAllByEnabledIsTrue(pageable: Pageable): Page<User>
@Query("select (count(u) > 0) from User u where u.username = ?1")
fun existsAllByUsername(username: String): Boolean
}