Advertisement
ArturCzopek

k2

Jun 3rd, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.51 KB | None | 0 0
  1. package pl.arturczopek.devoxx.utils
  2.  
  3. import org.springframework.boot.CommandLineRunner
  4. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
  5. import org.springframework.stereotype.Service
  6. import pl.arturczopek.devoxx.model.Authority
  7. import pl.arturczopek.devoxx.model.User
  8. import pl.arturczopek.devoxx.repository.AuthRepository
  9. import pl.arturczopek.devoxx.repository.UserRepository
  10.  
  11. @Service
  12. class UserInsertRunner(val userRepository: UserRepository, val authRepository: AuthRepository, val bCryptPasswordEncoder: BCryptPasswordEncoder) : CommandLineRunner {
  13.  
  14.     override fun run(vararg args: String?) {
  15.         val role = Authority(authority = "ADMIN", id = 10, username = "artur")
  16.         authRepository.save(role)
  17.  
  18.         val firstUser = User(name = "Artur", password = bCryptPasswordEncoder.encode("artur"), id = 11, roleId = role.id)
  19.         saveUser(firstUser)
  20.  
  21.         (1..20).forEach {
  22.             val id = 10 + (it.toLong() * 2)
  23.             val anotherRole = Authority(authority = "USER", id = id, username = "another$it")
  24.             authRepository.save(anotherRole)
  25.  
  26.             val anotherUser = User(name = "another$it", password = bCryptPasswordEncoder.encode("another$it"), id = id + 1, roleId = anotherRole.id)
  27.             saveUser(anotherUser)
  28.         }
  29.     }
  30.  
  31.     private fun saveUser(user: User) {
  32.         userRepository.save(user)
  33.         println("""
  34.        |Added ${user.name}
  35.        |Received green tokens: ${user.receivedGreenTokens}
  36.        """.trimMargin())
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement