Skip to content

Commit

Permalink
Upgrade components.
Browse files Browse the repository at this point in the history
  • Loading branch information
8treenet committed May 8, 2021
1 parent 16fa742 commit 81a73d6
Show file tree
Hide file tree
Showing 52 changed files with 476 additions and 443 deletions.
4 changes: 1 addition & 3 deletions example/base/adapter/repository/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package repository

import (
"github.com/8treenet/freedom"
"github.com/jinzhu/gorm"
"gorm.io/gorm"
)

func init() {
Expand Down Expand Up @@ -38,8 +38,6 @@ func (repo *Default) db() *gorm.DB {
if err := repo.FetchDB(&db); err != nil {
panic(err)
}
db = db.New()
db.SetLogger(repo.Worker().Logger())
return db
}

Expand Down
16 changes: 10 additions & 6 deletions example/base/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/8treenet/freedom/infra/requests"
"github.com/8treenet/freedom/middleware"
"github.com/go-redis/redis"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

func main() {
Expand Down Expand Up @@ -65,14 +65,18 @@ func installMiddleware(app freedom.Application) {
func installDatabase(app freedom.Application) {
app.InstallDB(func() interface{} {
conf := conf.Get().DB
db, e := gorm.Open("mysql", conf.Addr)
db, e := gorm.Open(mysql.Open(conf.Addr), &gorm.Config{})
if e != nil {
freedom.Logger().Fatal(e.Error())
}

db.DB().SetMaxIdleConns(conf.MaxIdleConns)
db.DB().SetMaxOpenConns(conf.MaxOpenConns)
db.DB().SetConnMaxLifetime(time.Duration(conf.ConnMaxLifeTime) * time.Second)
sqlDB, err := db.DB()
if err != nil {
freedom.Logger().Fatal(e.Error())
}
sqlDB.SetMaxIdleConns(conf.MaxIdleConns)
sqlDB.SetMaxOpenConns(conf.MaxOpenConns)
sqlDB.SetConnMaxLifetime(time.Duration(conf.ConnMaxLifeTime) * time.Second)
return db
})
}
Expand Down
6 changes: 2 additions & 4 deletions example/fshop/adapter/repository/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package repository
import (
"github.com/8treenet/freedom/example/fshop/domain/dependency"
"github.com/8treenet/freedom/example/fshop/domain/entity"
"github.com/jinzhu/gorm"
"gorm.io/gorm"

"github.com/8treenet/freedom"
)
Expand All @@ -29,7 +29,7 @@ type Admin struct {
func (repo *Admin) Get(id int) (adminEntity *entity.Admin, e error) {
adminEntity = &entity.Admin{}
adminEntity.ID = id
e = findUser(repo, adminEntity)
e = findAdmin(repo, &adminEntity.Admin)
if e != nil {
return
}
Expand All @@ -44,7 +44,5 @@ func (repo *Admin) db() *gorm.DB {
if err := repo.FetchDB(&db); err != nil {
panic(err)
}
db = db.New()
db.SetLogger(repo.Worker().Logger())
return db
}
11 changes: 6 additions & 5 deletions example/fshop/adapter/repository/cart.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/8treenet/freedom/example/fshop/domain/dependency"
"github.com/8treenet/freedom/example/fshop/domain/entity"
"github.com/8treenet/freedom/example/fshop/domain/po"
"github.com/jinzhu/gorm"
"gorm.io/gorm"

"github.com/8treenet/freedom"
)
Expand All @@ -30,11 +30,14 @@ type Cart struct {

// FindAll 获取用户购物车实体
func (repo *Cart) FindAll(userID int) (entitys []*entity.Cart, e error) {
e = findCartList(repo, po.Cart{UserID: userID}, &entitys)
list, e := findCartList(repo, po.Cart{UserID: userID})
if e != nil {
return
}

for _, obj := range list {
entitys = append(entitys, &entity.Cart{Cart: obj})
}
//注入基础Entity
repo.InjectBaseEntitys(entitys)
return
Expand All @@ -43,7 +46,7 @@ func (repo *Cart) FindAll(userID int) (entitys []*entity.Cart, e error) {
// FindByGoodsID 获取用户某商品的购物车
func (repo *Cart) FindByGoodsID(userID, goodsID int) (cartEntity *entity.Cart, e error) {
cartEntity = &entity.Cart{Cart: po.Cart{UserID: userID, GoodsID: goodsID}}
e = findCart(repo, cartEntity)
e = findCart(repo, &cartEntity.Cart)
if e != nil {
return
}
Expand Down Expand Up @@ -81,7 +84,5 @@ func (repo *Cart) db() *gorm.DB {
if err := repo.FetchDB(&db); err != nil {
panic(err)
}
db = db.New()
db.SetLogger(repo.Worker().Logger())
return db
}
4 changes: 1 addition & 3 deletions example/fshop/adapter/repository/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/8treenet/freedom/example/fshop/domain/dependency"
"github.com/8treenet/freedom/example/fshop/domain/entity"
"github.com/8treenet/freedom/example/fshop/domain/po"
"github.com/jinzhu/gorm"
"gorm.io/gorm"
)

func init() {
Expand Down Expand Up @@ -51,7 +51,5 @@ func (repo *Delivery) db() *gorm.DB {
if err := repo.FetchDB(&db); err != nil {
panic(err)
}
db = db.New()
db.SetLogger(repo.Worker().Logger())
return db
}
Loading

0 comments on commit 81a73d6

Please sign in to comment.