-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (73 loc) · 1.8 KB
/
main.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
package main
import (
"encoding/json"
"os"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
type Actor struct {
ID uint `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Films []Film `json:"films" gorm:"many2many:actor_films;"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type Film struct {
ID uint `json:"id"`
Title string `json:"title"`
LanguageID uint `json:"-"`
Language Language `json:"language"`
Categories []Category `json:"categories" gorm:"many2many:film_categories;"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type Language struct {
ID uint `json:"id"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type Category struct {
ID uint `json:"id"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func initialData(db *gorm.DB) {
tx := db.Begin()
defer tx.Commit()
lang := Language{
Name: "English",
}
tx.Create(&lang)
cate := Category{
Name: "Documentary",
}
tx.Create(&cate)
film := Film{
Title: "Young Language",
Language: lang,
Categories: []Category{cate},
}
tx.Create(&film)
actor := Actor{
FirstName: "Ed",
LastName: "Chase",
Films: []Film{film},
}
tx.Create(&actor)
}
func main() {
connURL := os.Getenv("POSTGRESQL_URL")
db, err := gorm.Open("postgres", connURL)
if err != nil {
panic("failed to connect database")
}
defer db.Close()
initialData(db)
var dest []Actor
db.Preload("Films.Language").Preload("Films.Categories").Find(&dest)
json.NewEncoder(os.Stdout).Encode(&dest)
}