Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gomall/tutorial): update code after video record #66

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gomall/tutorial/ch05/demo/demo_proto/biz/dal/mysql/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func Init() {
if err != nil {
panic(err)
}
DB.AutoMigrate(model.User{})
DB.AutoMigrate(&model.User{})
fmt.Printf("%#v", DB.Debug().Exec("select version()"))
}
6 changes: 3 additions & 3 deletions gomall/tutorial/ch05/demo/demo_proto/biz/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import "gorm.io/gorm"

type User struct {
gorm.Model
Email string `gorm:"unique"`
Password string `gorm:"varchar(64),not null"`
Email string `gorm:"uniqueIndex;type:varchar(128) not null"`
Password string `gorm:"type:varchar(64) not null"`
}

func (u User) TableName() string {
func (User) TableName() string {
return "user"
}
22 changes: 18 additions & 4 deletions gomall/tutorial/ch05/demo/demo_proto/cmd/dbop/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,22 @@ func main() {
panic(err)
}
dal.Init()
mysql.DB.Create(&model.User{Email: "[email protected]", Password: "a958af3702caf245d205da6164afebe0"})
var u model.User
mysql.DB.First(&u)
fmt.Printf("%#v", u)
// CURD

// Create
// mysql.DB.Create(&model.User{Email: "[email protected]", Password: "jfiojffjsoij"})

// Update
// mysql.DB.Model(&model.User{}).Where("email = ?", "[email protected]").Update("password", "22222222")

// Read
var row model.User
mysql.DB.Model(&model.User{}).Where("email = ?", "[email protected]").First(&row)

fmt.Printf("row: %+v\n", row)

// Delete
// mysql.DB.Where("email = ?", "[email protected]").Delete(&model.User{})

// mysql.DB.Unscoped().Where("email = ?", "[email protected]").Delete(&model.User{})
}
1 change: 1 addition & 0 deletions gomall/tutorial/ch05/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ services:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=demo_proto
2 changes: 1 addition & 1 deletion gomall/tutorial/ch06/demo/demo_proto/biz/dal/mysql/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func Init() {
if err != nil {
panic(err)
}
DB.AutoMigrate(model.User{})
DB.AutoMigrate(&model.User{})
fmt.Printf("%#v", DB.Debug().Exec("select version()"))
}
6 changes: 3 additions & 3 deletions gomall/tutorial/ch06/demo/demo_proto/biz/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import "gorm.io/gorm"

type User struct {
gorm.Model
Email string `gorm:"unique"`
Password string `gorm:"varchar(64),not null"`
Email string `gorm:"uniqueIndex;type:varchar(128) not null"`
Password string `gorm:"type:varchar(64) not null"`
}

func (u User) TableName() string {
func (User) TableName() string {
return "user"
}
22 changes: 18 additions & 4 deletions gomall/tutorial/ch06/demo/demo_proto/cmd/dbop/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,22 @@ func main() {
panic(err)
}
dal.Init()
mysql.DB.Create(&model.User{Email: "[email protected]", Password: "a958af3702caf245d205da6164afebe0"})
var u model.User
mysql.DB.First(&u)
fmt.Printf("%#v", u)
// CURD

// Create
// mysql.DB.Create(&model.User{Email: "[email protected]", Password: "jfiojffjsoij"})

// Update
// mysql.DB.Model(&model.User{}).Where("email = ?", "[email protected]").Update("password", "22222222")

// Read
var row model.User
mysql.DB.Model(&model.User{}).Where("email = ?", "[email protected]").First(&row)

fmt.Printf("row: %+v\n", row)

// Delete
// mysql.DB.Where("email = ?", "[email protected]").Delete(&model.User{})

// mysql.DB.Unscoped().Where("email = ?", "[email protected]").Delete(&model.User{})
}
1 change: 1 addition & 0 deletions gomall/tutorial/ch06/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ services:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=demo_proto
2 changes: 1 addition & 1 deletion gomall/tutorial/ch07/demo/demo_proto/biz/dal/mysql/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func Init() {
if err != nil {
panic(err)
}
DB.AutoMigrate(model.User{}) //nolint: errcheck
DB.AutoMigrate(&model.User{}) //nolint: errcheck
fmt.Printf("%#v", DB.Debug().Exec("select version()"))
}
6 changes: 3 additions & 3 deletions gomall/tutorial/ch07/demo/demo_proto/biz/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import "gorm.io/gorm"

type User struct {
gorm.Model
Email string `gorm:"unique"`
Password string `gorm:"varchar(64),not null"`
Email string `gorm:"uniqueIndex;type:varchar(128) not null"`
Password string `gorm:"type:varchar(64) not null"`
}

func (u User) TableName() string {
func (User) TableName() string {
return "user"
}
22 changes: 18 additions & 4 deletions gomall/tutorial/ch07/demo/demo_proto/cmd/dbop/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,22 @@ func main() {
panic(err)
}
dal.Init()
mysql.DB.Create(&model.User{Email: "[email protected]", Password: "a958af3702caf245d205da6164afebe0"})
var u model.User
mysql.DB.First(&u)
fmt.Printf("%#v", u)
// CURD

// Create
// mysql.DB.Create(&model.User{Email: "[email protected]", Password: "jfiojffjsoij"})

// Update
// mysql.DB.Model(&model.User{}).Where("email = ?", "[email protected]").Update("password", "22222222")

// Read
var row model.User
mysql.DB.Model(&model.User{}).Where("email = ?", "[email protected]").First(&row)

fmt.Printf("row: %+v\n", row)

// Delete
// mysql.DB.Where("email = ?", "[email protected]").Delete(&model.User{})

// mysql.DB.Unscoped().Where("email = ?", "[email protected]").Delete(&model.User{})
}
1 change: 1 addition & 0 deletions gomall/tutorial/ch07/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ services:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=demo_proto
42 changes: 0 additions & 42 deletions gomall/tutorial/ch08/app/frontend/.air.toml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/cloudwego/biz-demo/gomall/app/frontend/biz/service"
"github.com/cloudwego/biz-demo/gomall/app/frontend/biz/utils"
common "github.com/cloudwego/biz-demo/gomall/app/frontend/hertz_gen/frontend/common"
home "github.com/cloudwego/biz-demo/gomall/app/frontend/hertz_gen/frontend/home"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)
Expand All @@ -28,19 +28,18 @@ import (
// @router / [GET]
func Home(ctx context.Context, c *app.RequestContext) {
var err error
var req common.Empty
var req home.Empty
err = c.BindAndValidate(&req)
if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}

// resp, err :=
resp, err := service.NewHomeService(ctx, c).Run(&req)
if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}

c.HTML(consts.StatusOK, "home", resp)
c.HTML(consts.StatusOK, "home.tmpl", resp)
}
24 changes: 18 additions & 6 deletions gomall/tutorial/ch08/app/frontend/biz/service/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ package service
import (
"context"

common "github.com/cloudwego/biz-demo/gomall/app/frontend/hertz_gen/frontend/common"
home "github.com/cloudwego/biz-demo/gomall/app/frontend/hertz_gen/frontend/home"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/utils"
)

type HomeService struct {
Expand All @@ -31,8 +30,21 @@ func NewHomeService(Context context.Context, RequestContext *app.RequestContext)
return &HomeService{RequestContext: RequestContext, Context: Context}
}

func (h *HomeService) Run(req *common.Empty) (res map[string]any, err error) {
return utils.H{
"title": "Hot sale",
}, nil
func (h *HomeService) Run(req *home.Empty) (map[string]any, error) {
//defer func() {
// hlog.CtxInfof(h.Context, "req = %+v", req)
// hlog.CtxInfof(h.Context, "resp = %+v", resp)
//}()
var resp = make(map[string]any)
items := []map[string]any{
{"Name": "T-shirt-1", "Price": 100, "Picture": "/static/image/t-shirt-1.jpeg"},
{"Name": "T-shirt-2", "Price": 110, "Picture": "/static/image/t-shirt-1.jpeg"},
{"Name": "T-shirt-3", "Price": 120, "Picture": "/static/image/t-shirt-2.jpeg"},
{"Name": "T-shirt-4", "Price": 130, "Picture": "/static/image/notebook.jpeg"},
{"Name": "T-shirt-5", "Price": 140, "Picture": "/static/image/t-shirt-1.jpeg"},
{"Name": "T-shirt-6", "Price": 150, "Picture": "/static/image/t-shirt.jpeg"},
}
resp["Title"] = "Hot Sales"
resp["Items"] = items
return resp, nil
}
67 changes: 22 additions & 45 deletions gomall/tutorial/ch08/app/frontend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,50 @@ go 1.21.5
replace github.com/apache/thrift => github.com/apache/thrift v0.13.0

require (
github.com/cloudwego/hertz v0.7.3
github.com/cloudwego/hertz v0.8.1
github.com/hertz-contrib/cors v0.1.0
github.com/hertz-contrib/gzip v0.0.3
github.com/hertz-contrib/logger/accesslog v0.0.0-20231211035138-acc7b4e2984b
github.com/hertz-contrib/obs-opentelemetry/provider v0.2.3
github.com/hertz-contrib/pprof v0.1.1
github.com/joho/godotenv v1.5.1
github.com/hertz-contrib/logger/accesslog v0.0.0-20240128134225-6b18af47a115
github.com/hertz-contrib/logger/logrus v1.0.1
github.com/hertz-contrib/pprof v0.1.2
github.com/kr/pretty v0.3.1
github.com/redis/go-redis/v9 v9.3.1
google.golang.org/protobuf v1.32.0
github.com/redis/go-redis/v9 v9.5.1
go.uber.org/zap v1.27.0
google.golang.org/protobuf v1.34.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/validator.v2 v2.0.1
gopkg.in/yaml.v2 v2.4.0
gorm.io/driver/mysql v1.5.2
gorm.io/gorm v1.25.5
gorm.io/driver/mysql v1.5.6
gorm.io/gorm v1.25.10
)

require (
github.com/bytedance/go-tagexpr/v2 v2.9.2 // indirect
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect
github.com/bytedance/mockey v1.2.7 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7 // indirect
github.com/bytedance/sonic v1.8.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/cloudwego/netpoll v0.5.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cloudwego/netpoll v0.5.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/felixge/fgprof v0.9.3 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
github.com/henrylee2cn/ameda v1.4.10 // indirect
github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/nyaruka/phonenumbers v1.0.55 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
go.opentelemetry.io/contrib/instrumentation/runtime v0.45.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.20.0 // indirect
go.opentelemetry.io/contrib/propagators/ot v1.20.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.42.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.42.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.2.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.59.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
Loading
Loading