-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.go
89 lines (71 loc) · 4.05 KB
/
seed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"log"
"time"
"github.com/ikennarichard/movie-reservation/config"
"github.com/ikennarichard/movie-reservation/models"
"golang.org/x/crypto/bcrypt"
)
func seedData() {
// Seed Users
users := []models.User{
{Name: "Ceci", Email: "[email protected]", Password: "adminpassword120", Role: "admin"},
}
for _, user := range users {
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
user.Password = string(hashedPassword)
if err := config.DB.FirstOrCreate(&models.User{}, user).Error; err != nil {
log.Fatalf("Could not seed user: %v", err)
}
}
// Seed Genres
genres := []models.Genre{
{Name: "Action"},
{Name: "Comedy"},
{Name: "Drama"},
{Name: "Horror"},
{Name: "Romance"},
}
var genreMap = make(map[string]models.Genre)
for _, genre := range genres {
if err := config.DB.FirstOrCreate(&genre, models.Genre{Name: genre.Name}).Error; err != nil {
log.Fatalf("Could not seed genre: %v", err)
}
genreMap[genre.Name] = genre
}
movies := []models.Movie{
{Title: "Avengers: Endgame", Description: "The Avengers must assemble once again to stop Thanos and save the universe.", Duration: 136, PosterImage: "avengers-endgame.jpg", Genres: []models.Genre{genreMap["Action"], genreMap["Drama"]}, Showtimes: []models.Showtime{} },
{Title: "The Hangover", Description: "A group of friends get into a wild adventure after a bachelor party in Las Vegas.", Duration: 136, PosterImage: "the-hangover.jpg", Genres: []models.Genre{genreMap["Comedy"]}, Showtimes: []models.Showtime{}},
{Title: "Titanic", Description: "A love story aboard the ill-fated RMS Titanic.", Duration: 136, PosterImage: "titanic.jpg", Genres: []models.Genre{genreMap["Romance"], genreMap["Drama"]}, Showtimes: []models.Showtime{}},
{Title: "It", Description: "A group of children must face their fears and fight a shape-shifting entity known as 'It'.", Duration: 136, PosterImage: "it.jpg", Genres: []models.Genre{genreMap["Horror"]}, Showtimes: []models.Showtime{}},
{Title: "Spider-Man: No Way Home", Description: "Peter Parker faces challenges that force him to make difficult decisions about his identity.", Duration: 136, PosterImage: "spiderman-no-way-home.jpg", Genres: []models.Genre{genreMap["Action"], genreMap["Comedy"]}, Showtimes: []models.Showtime{}},
}
for _, movie := range movies {
if err := config.DB.Create(&movie).Error; err != nil {
log.Fatalf("Could not seed movie: %v", err)
}
}
var movieList []models.Movie
if err := config.DB.Find(&movieList).Error; err != nil {
log.Fatalf("Could not fetch movies for showtimes: %v", err)
}
movieMap := make(map[string]uint)
for _, movie := range movieList {
movieMap[movie.Title] = movie.ID
}
// Seed Showtimes
showtimes := []models.Showtime{
{MovieID: movieMap["Avengers: Endgame"], StartTime: time.Date(2023, 12, 25, 15, 0, 0, 0, time.UTC), AvailableSeats: 100, Price: 10, EndTime: time.Date(2023, 12, 25, 18, 0, 0, 0, time.UTC)},
{MovieID: movieMap["The Hangover"], StartTime: time.Date(2023, 12, 25, 20, 0, 0, 0, time.UTC), AvailableSeats: 100, Price: 10, EndTime: time.Date(2023, 12, 25, 22, 0, 0, 0, time.UTC)},
{MovieID: movieMap["The Hangover"], StartTime: time.Date(2023, 12, 26, 14, 0, 0, 0, time.UTC), AvailableSeats: 100, Price: 10, EndTime: time.Date(2023, 12, 26, 17, 0, 0, 0, time.UTC)},
{MovieID: movieMap["Titanic"], StartTime: time.Date(2023, 12, 26, 19, 0, 0, 0, time.UTC), AvailableSeats: 100, Price: 10, EndTime: time.Date(2023, 12, 26, 21, 30, 0, 0, time.UTC)},
{MovieID: movieMap["It"], StartTime: time.Date(2023, 12, 27, 16, 0, 0, 0, time.UTC), AvailableSeats: 100, Price: 10, EndTime: time.Date(2023, 12, 27, 18, 30, 0, 0, time.UTC)},
{MovieID: movieMap["Spider-Man: No Way Home"], StartTime: time.Date(2023, 12, 28, 19, 0, 0, 0, time.UTC), AvailableSeats: 100, Price: 10, EndTime: time.Date(2023, 12, 28, 20, 30, 0, 0, time.UTC)},
}
for _, showtime := range showtimes {
if err := config.DB.Create(&showtime).Error; err != nil {
log.Fatalf("Could not seed showtime: %v", err)
}
}
log.Println("Database seeded successfully!")
}