Skip to content

Commit 38739f0

Browse files
committed
refactor: 重构saying接口,采用新的数据源
1 parent c4f5188 commit 38739f0

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

internal/controller/other/other_app_saying.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import (
44
"context"
55

66
"github.com/oldme-git/oldme-api/api/other/app"
7+
"github.com/oldme-git/oldme-api/internal/logic/sentence"
78
)
89

910
func (c *ControllerApp) Saying(ctx context.Context, req *app.SayingReq) (res *app.SayingRes, err error) {
10-
// sayingOne, err := saying.Show(ctx)
11-
// if err == nil {
12-
// res = &app.SayingRes{Saying: sayingOne}
13-
// }
14-
return
11+
saying, err := sentence.Saying(ctx)
12+
if err != nil {
13+
return nil, err
14+
}
15+
return &app.SayingRes{
16+
Saying: saying.Sentence,
17+
}, nil
1518
}

internal/logic/sentence/sentence.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/oldme-git/oldme-api/internal/model/do"
1111
"github.com/oldme-git/oldme-api/internal/model/entity"
1212
"github.com/oldme-git/oldme-api/internal/utility"
13+
"github.com/oldme-git/oldme-api/utility/uinit"
1314
)
1415

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

210211
return
211212
}
213+
214+
// Saying 随机读取一句话,用于首页展示
215+
func Saying(ctx context.Context) (info *entity.Sentence, err error) {
216+
db := dao.SentenceTag.Ctx(ctx).
217+
Fields("s_id").
218+
OrderRandom()
219+
if uinit.SayingTagId > 0 {
220+
db = db.Where("t_id", uinit.SayingTagId)
221+
}
222+
223+
idData, err := db.Limit(1).One()
224+
if err != nil {
225+
err = utility.Err.Sys(err)
226+
}
227+
id := model.Id(idData["s_id"].Int())
228+
info, err = Show(ctx, id)
229+
230+
return
231+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
1313
_ "github.com/oldme-git/oldme-api/internal/logic"
1414
_ "github.com/oldme-git/oldme-api/internal/utility"
15+
_ "github.com/oldme-git/oldme-api/utility/uinit"
1516
)
1617

1718
const (

manifest/config/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ redis:
2929
default:
3030
address: 127.0.0.1:6379
3131
db: 1
32+
33+
# 固定的saying tag_id
34+
sayingTagId: 17

utility/uinit/uinit.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// 此包可用作初始化一些配置
2+
3+
package uinit
4+
5+
import (
6+
"github.com/gogf/gf/v2/os/gcfg"
7+
"github.com/gogf/gf/v2/os/gctx"
8+
)
9+
10+
var SayingTagId uint32
11+
12+
func init() {
13+
sayingTagId()
14+
}
15+
16+
// sayingTagId 获取短句标签id,用于首页展示
17+
// 从配置文件中获取
18+
func sayingTagId() {
19+
cfg, _ := gcfg.New()
20+
idRaw, err := cfg.Get(gctx.New(), "sayingTagId")
21+
if err != nil {
22+
SayingTagId = 0
23+
} else {
24+
SayingTagId = idRaw.Uint32()
25+
}
26+
}

0 commit comments

Comments
 (0)