Datenbank seeder hinzugefügt
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
FROM ubuntu:latest
|
||||
LABEL authors="Damian"
|
||||
|
||||
FROM golang:alpine AS GO_BUILD
|
||||
LABEL authors="Damian"
|
||||
COPY ./ /server
|
||||
WORKDIR /server
|
||||
ADD go.mod .
|
||||
ADD go.sum .
|
||||
RUN go mod download
|
||||
RUN go install github.com/cosmtrek/air@latest
|
||||
RUN go build -o /go/bin/server
|
||||
|
||||
|
||||
|
||||
@@ -4,12 +4,15 @@ import (
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/urfave/cli"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"moretcgshop/app/models"
|
||||
"moretcgshop/database/seeders"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
@@ -41,6 +44,7 @@ var sessionUser = "user-session"
|
||||
|
||||
func (server *Server) Initialize(appConfig AppConfig, dbConfig DBConfig) {
|
||||
fmt.Println("Willkommen zu " + appConfig.AppName)
|
||||
|
||||
server.initializeDB(dbConfig)
|
||||
server.initializeAppConfig(appConfig)
|
||||
server.routeInit()
|
||||
@@ -84,8 +88,26 @@ func (server *Server) Run(addr string) {
|
||||
log.Fatal(http.ListenAndServe(addr, server.Router))
|
||||
}
|
||||
|
||||
func (s Server) InitCommands(config AppConfig, config2 DBConfig) {
|
||||
func (server *Server) InitCommands(config AppConfig, dbConfig DBConfig) {
|
||||
server.initializeDB(dbConfig)
|
||||
cmdApp := cli.NewApp()
|
||||
cmdApp.Commands = []cli.Command{
|
||||
{
|
||||
Name: "db:seed",
|
||||
Action: func(c *cli.Context) error {
|
||||
err := seeders.DBSeed(server.DB)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := cmdApp.Run(os.Args)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func MakePassword(password string) (string, error) {
|
||||
|
||||
@@ -23,7 +23,9 @@ func (server *Server) Products(w http.ResponseWriter, r *http.Request) {
|
||||
perPage := 9
|
||||
|
||||
productModel := models.Product{}
|
||||
|
||||
products, totalRows, err := productModel.GetProducts(server.DB, perPage, page)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
46
database/fakers/product_faker.go
Normal file
46
database/fakers/product_faker.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package fakers
|
||||
|
||||
import (
|
||||
"github.com/bxcodec/faker/v3"
|
||||
"github.com/google/uuid"
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"math"
|
||||
"math/rand"
|
||||
"moretcgshop/app/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ProductFaker(db *gorm.DB) *models.Product {
|
||||
user := UserFaker(db)
|
||||
err := db.Create(&user).Error
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
name := faker.Name()
|
||||
return &models.Product{
|
||||
ID: uuid.New().String(),
|
||||
|
||||
Name: name,
|
||||
Price: decimal.NewFromFloat(fakePrice()),
|
||||
|
||||
ShortDescription: faker.Paragraph(),
|
||||
Description: faker.Paragraph(),
|
||||
|
||||
CreatedAt: time.Time{},
|
||||
UpdatedAt: time.Time{},
|
||||
}
|
||||
}
|
||||
|
||||
func fakePrice() float64 {
|
||||
return precision(rand.Float64()*math.Pow10(rand.Intn(8)), rand.Intn(2)+1)
|
||||
}
|
||||
|
||||
// precision | a helper function to set precision of price
|
||||
func precision(val float64, pre int) float64 {
|
||||
div := math.Pow10(pre)
|
||||
return float64(int64(val*div)) / div
|
||||
}
|
||||
22
database/fakers/user_faker.go
Normal file
22
database/fakers/user_faker.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package fakers
|
||||
|
||||
import (
|
||||
"github.com/bxcodec/faker/v3"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
"moretcgshop/app/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
func UserFaker(db *gorm.DB) *models.User {
|
||||
return &models.User{
|
||||
ID: uuid.New().String(),
|
||||
FirstName: faker.FirstName(),
|
||||
LastName: faker.LastName(),
|
||||
Email: faker.Email(),
|
||||
Password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi", // password
|
||||
CreatedAt: time.Time{},
|
||||
UpdatedAt: time.Time{},
|
||||
DeletedAt: gorm.DeletedAt{},
|
||||
}
|
||||
}
|
||||
28
database/seeders/seeder.go
Normal file
28
database/seeders/seeder.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package seeders
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"moretcgshop/database/fakers"
|
||||
)
|
||||
|
||||
type Seeder struct {
|
||||
Seeder interface{}
|
||||
}
|
||||
|
||||
func RegisterSeeders(db *gorm.DB) []Seeder {
|
||||
return []Seeder{
|
||||
{Seeder: fakers.UserFaker(db)},
|
||||
{Seeder: fakers.ProductFaker(db)},
|
||||
}
|
||||
}
|
||||
|
||||
func DBSeed(db *gorm.DB) error {
|
||||
for _, seeder := range RegisterSeeders(db) {
|
||||
err := db.Debug().Create(seeder.Seeder).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -45,3 +45,18 @@ services:
|
||||
networks:
|
||||
- http_network
|
||||
- moretcg
|
||||
web:
|
||||
build: .
|
||||
depends_on:
|
||||
- db
|
||||
ports:
|
||||
- "9000:9000"
|
||||
environment:
|
||||
- DB_HOST=db
|
||||
- DB_USER=moretcg
|
||||
- DB_PASS=moretcg
|
||||
- DB_DATA=moretcg
|
||||
networks:
|
||||
- moretcg
|
||||
|
||||
|
||||
|
||||
4
go.mod
4
go.mod
@@ -3,18 +3,21 @@ module moretcgshop
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/bxcodec/faker/v3 v3.8.1
|
||||
github.com/google/uuid v1.4.0
|
||||
github.com/gorilla/mux v1.8.1
|
||||
github.com/gorilla/sessions v1.2.2
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/shopspring/decimal v1.3.1
|
||||
github.com/unrolled/render v1.6.1
|
||||
github.com/urfave/cli v1.22.14
|
||||
golang.org/x/crypto v0.14.0
|
||||
gorm.io/driver/postgres v1.5.4
|
||||
gorm.io/gorm v1.25.5
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
||||
github.com/gorilla/securecookie v1.1.2 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
@@ -22,6 +25,7 @@ require (
|
||||
github.com/jackc/pgx/v5 v5.4.3 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
golang.org/x/sys v0.13.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
)
|
||||
|
||||
19
go.sum
19
go.sum
@@ -1,5 +1,11 @@
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/bxcodec/faker/v3 v3.8.1 h1:qO/Xq19V6uHt2xujwpaetgKhraGCapqY2CRWGD/SqcM=
|
||||
github.com/bxcodec/faker/v3 v3.8.1/go.mod h1:DdSDccxF5msjFo5aO4vrobRQ8nIApg8kq3QWPEQD6+o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
|
||||
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
|
||||
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
||||
@@ -25,14 +31,23 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
|
||||
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/unrolled/render v1.6.1 h1:Qa7dLBJ1/DLogeAEINpMnMuUqpFTEzBPZXDrXvyiVNc=
|
||||
github.com/unrolled/render v1.6.1/go.mod h1:LwQSeDhjml8NLjIO9GJO1/1qpFJxtfVIpzxXKjfVkoI=
|
||||
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
|
||||
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
|
||||
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
||||
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
@@ -41,8 +56,10 @@ golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo=
|
||||
gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0=
|
||||
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
|
||||
|
||||
Reference in New Issue
Block a user