You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
1.5 KiB

  1. package Seeder
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Api/Auth"
  7. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database"
  8. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
  9. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Util"
  10. )
  11. var (
  12. firstNames = []string{
  13. "John",
  14. "Mark",
  15. "Annie",
  16. "Hannah",
  17. "Shane",
  18. "Joe",
  19. "Katara",
  20. "Zuko",
  21. "Aang",
  22. "Sokka",
  23. }
  24. lastNames = []string{
  25. "Smith",
  26. "Johnson",
  27. "Williams",
  28. "Brown",
  29. "Jones",
  30. "Garcia",
  31. "Miller",
  32. "Davis",
  33. "Lopez",
  34. }
  35. )
  36. func randName(last bool) string {
  37. var (
  38. choices []string
  39. )
  40. choices = firstNames
  41. if last {
  42. choices = lastNames
  43. }
  44. return choices[rand.Intn(len(choices))]
  45. }
  46. func createUser() (Models.User, error) {
  47. var (
  48. userData Models.User
  49. now time.Time
  50. firstName, lastName string
  51. email, password string
  52. err error
  53. )
  54. now = time.Now()
  55. firstName = randName(false)
  56. lastName = randName(true)
  57. email = fmt.Sprintf("%s%s+%s@email.com", firstName, lastName, Util.RandomString(10))
  58. password, err = Auth.HashPassword("password")
  59. if err != nil {
  60. return Models.User{}, err
  61. }
  62. userData = Models.User{
  63. Email: email,
  64. Password: password,
  65. LastLogin: &now,
  66. FirstName: firstName,
  67. LastName: lastName,
  68. }
  69. err = Database.CreateUser(&userData)
  70. return userData, err
  71. }
  72. func SeedUsers() {
  73. var (
  74. i int
  75. err error
  76. )
  77. for i = 0; i <= 20; i++ {
  78. _, err = createUser()
  79. if err != nil {
  80. panic(err)
  81. }
  82. }
  83. }