Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.arturczopek.devoxx.utils
- import org.springframework.boot.CommandLineRunner
- import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
- import org.springframework.stereotype.Service
- import pl.arturczopek.devoxx.model.Authority
- import pl.arturczopek.devoxx.model.User
- import pl.arturczopek.devoxx.repository.AuthRepository
- import pl.arturczopek.devoxx.repository.UserRepository
- @Service
- class UserInsertRunner(val userRepository: UserRepository, val authRepository: AuthRepository, val bCryptPasswordEncoder: BCryptPasswordEncoder) : CommandLineRunner {
- override fun run(vararg args: String?) {
- val role = Authority(authority = "ADMIN", id = 10, username = "artur")
- authRepository.save(role)
- val firstUser = User(name = "Artur", password = bCryptPasswordEncoder.encode("artur"), id = 11, roleId = role.id)
- saveUser(firstUser)
- (1..20).forEach {
- val id = 10 + (it.toLong() * 2)
- val anotherRole = Authority(authority = "USER", id = id, username = "another$it")
- authRepository.save(anotherRole)
- val anotherUser = User(name = "another$it", password = bCryptPasswordEncoder.encode("another$it"), id = id + 1, roleId = anotherRole.id)
- saveUser(anotherUser)
- }
- }
- private fun saveUser(user: User) {
- userRepository.save(user)
- println("""
- |Added ${user.name}
- |Received green tokens: ${user.receivedGreenTokens}
- """.trimMargin())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement