Skip to content

Commit

Permalink
refactor: 重构saying接口,采用新的数据源
Browse files Browse the repository at this point in the history
  • Loading branch information
oldme-git committed Nov 25, 2024
1 parent c4f5188 commit 38739f0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
13 changes: 8 additions & 5 deletions internal/controller/other/other_app_saying.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"context"

"github.com/oldme-git/oldme-api/api/other/app"
"github.com/oldme-git/oldme-api/internal/logic/sentence"
)

func (c *ControllerApp) Saying(ctx context.Context, req *app.SayingReq) (res *app.SayingRes, err error) {
// sayingOne, err := saying.Show(ctx)
// if err == nil {
// res = &app.SayingRes{Saying: sayingOne}
// }
return
saying, err := sentence.Saying(ctx)
if err != nil {
return nil, err
}
return &app.SayingRes{
Saying: saying.Sentence,
}, nil
}
20 changes: 20 additions & 0 deletions internal/logic/sentence/sentence.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/oldme-git/oldme-api/internal/model/do"
"github.com/oldme-git/oldme-api/internal/model/entity"
"github.com/oldme-git/oldme-api/internal/utility"
"github.com/oldme-git/oldme-api/utility/uinit"
)

// Cre 创建句子
Expand Down Expand Up @@ -209,3 +210,22 @@ func GetIdsByTagIds(ctx context.Context, tagIds []model.Id, p model.Paging) (ids

return
}

// Saying 随机读取一句话,用于首页展示
func Saying(ctx context.Context) (info *entity.Sentence, err error) {
db := dao.SentenceTag.Ctx(ctx).
Fields("s_id").
OrderRandom()
if uinit.SayingTagId > 0 {
db = db.Where("t_id", uinit.SayingTagId)
}

idData, err := db.Limit(1).One()
if err != nil {
err = utility.Err.Sys(err)
}
id := model.Id(idData["s_id"].Int())
info, err = Show(ctx, id)

return
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
_ "github.com/oldme-git/oldme-api/internal/logic"
_ "github.com/oldme-git/oldme-api/internal/utility"
_ "github.com/oldme-git/oldme-api/utility/uinit"
)

const (
Expand Down
3 changes: 3 additions & 0 deletions manifest/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ redis:
default:
address: 127.0.0.1:6379
db: 1

# 固定的saying tag_id
sayingTagId: 17
26 changes: 26 additions & 0 deletions utility/uinit/uinit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 此包可用作初始化一些配置

package uinit

import (
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/os/gctx"
)

var SayingTagId uint32

func init() {
sayingTagId()
}

// sayingTagId 获取短句标签id,用于首页展示
// 从配置文件中获取
func sayingTagId() {
cfg, _ := gcfg.New()
idRaw, err := cfg.Get(gctx.New(), "sayingTagId")
if err != nil {
SayingTagId = 0
} else {
SayingTagId = idRaw.Uint32()
}
}

0 comments on commit 38739f0

Please sign in to comment.