Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"kfc_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/admin/main/activity/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"kfc.go",
],
importpath = "go-common/app/admin/main/activity/dao/kfc",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/activity/conf:go_default_library",
"//app/admin/main/activity/model/kfc:go_default_library",
"//library/database/orm:go_default_library",
"//vendor/github.com/jinzhu/gorm:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,51 @@
package kfc
import (
"context"
"go-common/app/admin/main/activity/conf"
"go-common/library/database/orm"
"github.com/jinzhu/gorm"
)
// Dao struct user of Dao.
type Dao struct {
c *conf.Config
DB *gorm.DB
}
// New create a instance of Dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
DB: orm.NewMySQL(c.ORM),
}
d.initORM()
return
}
func (d *Dao) initORM() {
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
if defaultTableName == "act_matchs" {
return defaultTableName
}
return defaultTableName
}
d.DB.LogMode(true)
}
// Ping check connection of db , mc.
func (d *Dao) Ping(c context.Context) (err error) {
if d.DB != nil {
err = d.DB.DB().PingContext(c)
}
return
}
// Close close connection of db , mc.
func (d *Dao) Close() {
if d.DB != nil {
d.DB.Close()
}
}

View File

@@ -0,0 +1,35 @@
package kfc
import (
"flag"
"os"
"testing"
"go-common/app/admin/main/activity/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.web-svr.activity-admin")
flag.Set("conf_token", "a0c33c892e0c08476ecbb5d28e5880cf")
flag.Set("tree_id", "34245")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../../cmd/activity-admin-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,26 @@
package kfc
import (
"context"
kfcmdl "go-common/app/admin/main/activity/model/kfc"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
)
// SearchList .
func (d *Dao) SearchList(c context.Context, code string, mid int64, pn, ps int) (list []*kfcmdl.BnjKfcCoupon, err error) {
db := d.DB
if code != "" {
db = db.Where("coupon_code = ?", code)
}
if mid != 0 {
db = db.Where("mid = ?", mid)
}
offset := (pn - 1) * ps
if err = db.Offset(offset).Limit(ps).Find(&list).Error; err != nil && err != gorm.ErrRecordNotFound {
err = errors.Wrap(err, "find error")
}
return
}

View File

@@ -0,0 +1,31 @@
package kfc
import (
"context"
"fmt"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoSearchList(t *testing.T) {
convey.Convey("SearchList", t, func(ctx convey.C) {
var (
c = context.Background()
code = ""
mid = int64(1505589)
pn = 1
ps = 15
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rp, err := d.SearchList(c, code, mid, pn, ps)
ctx.Convey("Then err should be nil.rp should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
for _, v := range rp {
fmt.Printf("%+v", v)
}
})
})
})
}