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,60 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"reply.go",
"search.go",
],
importpath = "go-common/app/admin/main/esports/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/esports/conf:go_default_library",
"//app/admin/main/esports/model:go_default_library",
"//library/database/elastic:go_default_library",
"//library/database/orm:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/jinzhu/gorm: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"],
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"reply_test.go",
"search_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/esports/conf:go_default_library",
"//app/admin/main/esports/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,42 @@
package dao
import (
"context"
"go-common/app/admin/main/esports/conf"
"go-common/library/database/elastic"
"go-common/library/database/orm"
bm "go-common/library/net/http/blademaster"
"github.com/jinzhu/gorm"
)
const _esports = "esports"
// Dao .
type Dao struct {
c *conf.Config
DB *gorm.DB
Elastic *elastic.Elastic
// client
replyClient *bm.Client
}
// New .
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// conf
c: c,
// db
DB: orm.NewMySQL(c.ORM),
// elastic
Elastic: elastic.NewElastic(nil),
replyClient: bm.NewClient(c.HTTPReply),
}
return
}
// Ping .
func (d *Dao) Ping(c context.Context) error {
return d.DB.DB().PingContext(c)
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"flag"
"os"
"testing"
"go-common/app/admin/main/esports/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.manager.esports-admin")
flag.Set("conf_token", "8664d908294b47ec512d22c7d2ed9d0f")
flag.Set("tree_id", "38071")
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/esports-admin-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,38 @@
package dao
import (
"context"
"net/url"
"strconv"
"go-common/library/ecode"
"go-common/library/log"
)
const (
_replyState = "0" // 0: open, 1: close
_replyURL = "http://api.bilibili.co/x/internal/v2/reply/subject/regist"
_gameOfficial = 32708316
)
// RegReply opens eports's reply.
func (d *Dao) RegReply(c context.Context, maid, adid int64, replyType string) (err error) {
params := url.Values{}
params.Set("adid", strconv.FormatInt(adid, 10))
params.Set("mid", strconv.FormatInt(_gameOfficial, 10))
params.Set("oid", strconv.FormatInt(maid, 10))
params.Set("type", replyType)
params.Set("state", _replyState)
var res struct {
Code int `json:"code"`
}
if err = d.replyClient.Post(c, _replyURL, "", params, &res); err != nil {
log.Error("d.replyClient.Post(%s) error(%v)", _replyURL+"?"+params.Encode(), err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("d.replyClient.Post(%s) error(%v)", _replyURL+"?"+params.Encode(), err)
err = ecode.Int(res.Code)
}
return
}

View File

@@ -0,0 +1,22 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoRegReply(t *testing.T) {
var (
c = context.Background()
maid = int64(11)
adid = int64(123)
)
convey.Convey("RegReply", t, func(ctx convey.C) {
err := d.RegReply(c, maid, adid, "25")
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,46 @@
package dao
import (
"context"
"go-common/app/admin/main/esports/model"
"go-common/library/database/elastic"
"go-common/library/log"
)
// SearchArc search archive.
func (d *Dao) SearchArc(c context.Context, p *model.ArcListParam) (rs []*model.SearchArc, total int, err error) {
req := d.Elastic.NewRequest(_esports).Index(_esports).Pn(p.Pn).Ps(p.Ps)
req.Fields("aid", "typeid", "title", "state", "mid", "gid", "tags", "teams", "matchs", "year")
if p.Title != "" {
req.WhereLike([]string{"title"}, []string{p.Title}, true, elastic.LikeLevelLow)
}
if p.Aid > 0 {
req.WhereEq("aid", p.Aid)
}
if p.TypeID > 0 {
req.WhereEq("type_id", p.TypeID)
}
if p.Copyright > 0 {
req.WhereEq("copyright", p.Copyright)
}
if p.State != "" {
req.WhereEq("state", p.State)
}
req.WhereEq("is_deleted", 0)
res := new(struct {
Page struct {
Num int `json:"num"`
Size int `json:"size"`
Total int `json:"total"`
} `json:"page"`
Result []*model.SearchArc `json:"result"`
})
if err = req.Scan(c, &res); err != nil || res == nil {
log.Error("SearchArc req.Scan error(%v)", err)
return
}
total = res.Page.Total
rs = res.Result
return
}

View File

@@ -0,0 +1,25 @@
package dao
import (
"context"
"testing"
"go-common/app/admin/main/esports/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoSearchArc(t *testing.T) {
var (
c = context.Background()
p = &model.ArcListParam{Pn: 1, Ps: 1, Title: "dota"}
)
convey.Convey("SearchArc", t, func(ctx convey.C) {
rs, total, err := d.SearchArc(c, p)
ctx.Convey("Then err should be nil.rs,total should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldNotBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
}