Skip to content

Commit 11ba4f7

Browse files
committed
Re-implement seeding
1 parent 6294937 commit 11ba4f7

File tree

4 files changed

+30
-59
lines changed

4 files changed

+30
-59
lines changed

api/models/User.go

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ type User struct {
1919
Password string `gorm:"size:100;not null;" json:"password"`
2020
CreatedAt time.Time `gorm:"default:current_timestamp()" json:"created_at"`
2121
UpdatedAt time.Time `gorm:"default:current_timestamp()" json:"updated_at"`
22-
// Posts []Post `gorm:"foreignkey:AuthorID" json:"posts"`
2322
}
2423

2524
func (u *User) BeforeSave() error {

api/seed/seeder.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package seed
33
import (
44
"log"
55

6-
"github.com/victorsteven/fullstack/api/database"
6+
"github.com/jinzhu/gorm"
77
"github.com/victorsteven/fullstack/api/models"
88
)
99

@@ -14,28 +14,26 @@ var users = []models.User{
1414
Password: "password",
1515
},
1616
models.User{
17-
Nickname: "Steven victor",
18-
Email: "steven@gmail.com",
17+
Nickname: "Martin Luther",
18+
Email: "luther@gmail.com",
1919
Password: "password",
2020
},
2121
}
2222

2323
var posts = []models.Post{
2424
models.Post{
25-
Title: "Title",
26-
Content: "Hello world",
25+
Title: "Title 1",
26+
Content: "Hello world 1",
27+
},
28+
models.Post{
29+
Title: "Title 2",
30+
Content: "Hello world 2",
2731
},
2832
}
2933

30-
func Load() {
31-
db, err := database.Connect()
34+
func Load(db *gorm.DB) {
3235

33-
if err != nil {
34-
log.Fatalf("cannot connect to the database: %v", err)
35-
}
36-
defer db.Close()
37-
38-
err = db.Debug().DropTableIfExists(&models.Post{}, &models.User{}).Error
36+
err := db.Debug().DropTableIfExists(&models.Post{}, &models.User{}).Error
3937
if err != nil {
4038
log.Fatalf("cannot drop table: %v", err)
4139
}
@@ -54,7 +52,6 @@ func Load() {
5452
if err != nil {
5553
log.Fatalf("cannot seed users table: %v", err)
5654
}
57-
5855
posts[i].AuthorID = users[i].ID
5956

6057
err = db.Debug().Model(&models.Post{}).Create(&posts[i]).Error

api/server.go

+5-34
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,20 @@
11
package api
22

33
import (
4-
"fmt"
5-
"log"
6-
"os"
7-
8-
// "github.com/victorsteven/fullstack/api/router"
9-
"github.com/joho/godotenv"
104
"github.com/victorsteven/fullstack/api/controllers"
5+
"github.com/victorsteven/fullstack/api/seed"
6+
"github.com/victorsteven/fullstack/config"
117
)
128

139
var server = controllers.Server{}
1410

15-
// func refreshTable() error {
16-
// err := server.DB.Debug().DropTableIfExists(&models.User{}).Error
17-
// if err != nil {
18-
// return err
19-
// }
20-
// err = server.DB.Debug().AutoMigrate(&models.User{}).Error
21-
// if err != nil {
22-
// return err
23-
// }
24-
// log.Printf("Successfully refreshed table")
25-
// return nil
26-
// }
27-
2811
func Run() {
2912

30-
// config.Load()
31-
// seed.Load()
32-
// fmt.Println("this is after seeding fresh")
33-
fmt.Printf("running... at port %d\n\n", 8080)
34-
// listen(8080)
35-
36-
var err error
37-
err = godotenv.Load()
38-
if err != nil {
39-
log.Fatalf("Error getting env %v", err)
40-
} else {
41-
fmt.Println("We are getting the env values")
42-
}
13+
config.Load()
4314

44-
server.Initialize(os.Getenv("DB_DRIVER"), os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_NAME"))
15+
server.Initialize(config.DBDRIVER, config.DBUSER, config.DBPASSWORD, config.DBNAME)
4516

46-
// refreshTable()
17+
seed.Load(server.DB)
4718

4819
server.Run(":8080")
4920

config/config.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,36 @@ import (
1010
)
1111

1212
var (
13-
PORT = 0
14-
SECRETKEY []byte
15-
DBURL = ""
16-
DBDRIVER = ""
13+
PORT = 0
14+
SECRETKEY []byte
15+
DBURL = ""
16+
DBDRIVER = ""
17+
DBUSER = ""
18+
DBPASSWORD = ""
19+
DBNAME = ""
1720
)
1821

1922
func Load() {
23+
2024
var err error
2125
err = godotenv.Load()
2226
if err != nil {
2327
log.Fatalf("Error getting env %v", err)
28+
} else {
29+
fmt.Println("We are getting the env values")
2430
}
2531
PORT, err = strconv.Atoi(os.Getenv("API_PORT"))
26-
2732
if err != nil {
2833
PORT = 8000
2934
}
30-
31-
DBHOST := os.Getenv("DATABASE_HOST")
35+
DBHOST := os.Getenv("DB_HOST")
3236
if DBHOST == "" {
3337
DBHOST = "127.0.0.1"
3438
}
3539
DBDRIVER = os.Getenv("DB_DRIVER")
36-
37-
DBURL = fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&parseTime=True&loc=Local", os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"), DBHOST, os.Getenv("DB_NAME"))
38-
40+
DBUSER = os.Getenv("DB_USER")
41+
DBPASSWORD = os.Getenv("DB_PASSWORD")
42+
DBNAME = os.Getenv("DB_NAME")
3943
SECRETKEY = []byte(os.Getenv("API_SECRET"))
4044

4145
}

0 commit comments

Comments
 (0)