Task: Add check username already existed when try to mutation a new user in graphql demo
This commit is contained in:
parent
66defb6669
commit
0bb930a128
@ -16,6 +16,8 @@ class UserMutation @Autowired constructor(
|
|||||||
) : GraphQLMutationResolver {
|
) : GraphQLMutationResolver {
|
||||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||||
fun createUser(input: UserInput): User {
|
fun createUser(input: UserInput): User {
|
||||||
|
if (userRepository.existsAllByUsername(input.username ?: "")) throw Exception("Username has been already existed!")
|
||||||
|
|
||||||
val user = UserMapper.fromInputToUser(input)
|
val user = UserMapper.fromInputToUser(input)
|
||||||
return userRepository.save(user)
|
return userRepository.save(user)
|
||||||
}
|
}
|
||||||
|
@ -11,4 +11,7 @@ import org.springframework.stereotype.Repository
|
|||||||
interface UserRepository : JpaRepository<User, Long> {
|
interface UserRepository : JpaRepository<User, Long> {
|
||||||
@Query("select u from User u where u.enabled = true")
|
@Query("select u from User u where u.enabled = true")
|
||||||
fun queryAllByEnabledIsTrue(pageable: Pageable): Page<User>
|
fun queryAllByEnabledIsTrue(pageable: Pageable): Page<User>
|
||||||
|
|
||||||
|
@Query("select (count(u) > 0) from User u where u.username = ?1")
|
||||||
|
fun existsAllByUsername(username: String): Boolean
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user