Skip to content

Commit ba1cfaf

Browse files
committed
feat: 优化句子接口
1 parent 6bfa23d commit ba1cfaf

File tree

7 files changed

+60
-33
lines changed

7 files changed

+60
-33
lines changed

api/other/app/other.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,20 @@ type (
4646

4747
type (
4848
PoemReq struct {
49-
g.Meta `path:"poem" method:"get" sm:"随即查询诗词" tags:"app"`
49+
g.Meta `path:"poem" method:"get" sm:"随机查询诗词" tags:"app"`
5050
}
5151

5252
PoemRes struct {
53-
Text string `json:"text"`
53+
Poem string `json:"poem"`
54+
}
55+
)
56+
57+
type (
58+
SlangReq struct {
59+
g.Meta `path:"slang" method:"get" sm:"随机查询俚语" tags:"app"`
60+
}
61+
62+
SlangRes struct {
63+
Slang string `json:"slang"`
5464
}
5565
)

api/other/other.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/controller/other/other_app_poem.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55

66
"github.com/oldme-git/oldme-api/api/other/app"
77
"github.com/oldme-git/oldme-api/internal/logic/sentence"
8+
"github.com/oldme-git/oldme-api/utility/uinit"
89
)
910

1011
func (c *ControllerApp) Poem(ctx context.Context, req *app.PoemReq) (res *app.PoemRes, err error) {
11-
poem, err := sentence.Poem(ctx)
12+
text, err := sentence.Text(ctx, uinit.PoemTagId)
1213
if err != nil {
1314
return nil, err
1415
}
1516
return &app.PoemRes{
16-
Text: poem.Sentence,
17+
Poem: text.Sentence,
1718
}, nil
1819
}

internal/controller/other/other_app_saying.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55

66
"github.com/oldme-git/oldme-api/api/other/app"
77
"github.com/oldme-git/oldme-api/internal/logic/sentence"
8+
"github.com/oldme-git/oldme-api/utility/uinit"
89
)
910

1011
func (c *ControllerApp) Saying(ctx context.Context, req *app.SayingReq) (res *app.SayingRes, err error) {
11-
saying, err := sentence.Saying(ctx)
12+
text, err := sentence.Text(ctx, uinit.SayingTagId)
1213
if err != nil {
1314
return nil, err
1415
}
1516
return &app.SayingRes{
16-
Saying: saying.Sentence,
17+
Saying: text.Sentence,
1718
}, nil
1819
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package other
2+
3+
import (
4+
"context"
5+
6+
"github.com/oldme-git/oldme-api/internal/logic/sentence"
7+
"github.com/oldme-git/oldme-api/utility/uinit"
8+
9+
"github.com/oldme-git/oldme-api/api/other/app"
10+
)
11+
12+
func (c *ControllerApp) Slang(ctx context.Context, req *app.SlangReq) (res *app.SlangRes, err error) {
13+
text, err := sentence.Text(ctx, uinit.SlangTagId)
14+
if err != nil {
15+
return nil, err
16+
}
17+
return &app.SlangRes{
18+
Slang: text.Sentence,
19+
}, nil
20+
}

internal/logic/sentence/sentence.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ 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"
1413
)
1514

1615
// Cre 创建句子
@@ -214,33 +213,12 @@ func GetIdsByTagIds(ctx context.Context, tagIds []model.Id, p model.Paging) (ids
214213
return
215214
}
216215

217-
// Saying 随机读取一句话,用于首页展示
218-
func Saying(ctx context.Context) (info *entity.Sentence, err error) {
216+
// Text 根据 tagId 随机读取一个句子
217+
func Text(ctx context.Context, tagId uint32) (info *entity.Sentence, err error) {
219218
db := dao.SentenceTag.Ctx(ctx).
220219
Fields("s_id").
220+
Where("t_id", tagId).
221221
OrderRandom()
222-
if uinit.SayingTagId > 0 {
223-
db = db.Where("t_id", uinit.SayingTagId)
224-
}
225-
226-
idData, err := db.Limit(1).One()
227-
if err != nil {
228-
err = utility.Err.Sys(err)
229-
}
230-
id := model.Id(idData["s_id"].Int())
231-
info, err = Show(ctx, id)
232-
233-
return
234-
}
235-
236-
// Poem 随机读取诗词
237-
func Poem(ctx context.Context) (info *entity.Sentence, err error) {
238-
db := dao.SentenceTag.Ctx(ctx).
239-
Fields("s_id").
240-
OrderRandom()
241-
if uinit.PoemTagId > 0 {
242-
db = db.Where("t_id", uinit.PoemTagId)
243-
}
244222

245223
idData, err := db.Limit(1).One()
246224
if err != nil {

utility/uinit/uinit.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import (
77
"github.com/gogf/gf/v2/os/gctx"
88
)
99

10-
var SayingTagId uint32
11-
var PoemTagId uint32
10+
var (
11+
SayingTagId uint32
12+
PoemTagId uint32
13+
SlangTagId uint32
14+
)
1215

1316
func init() {
1417
sayingTagId()
1518
poemTagId()
19+
slangTagId()
1620
}
1721

1822
// sayingTagId 获取短句标签id,用于首页展示
@@ -38,3 +42,15 @@ func poemTagId() {
3842
PoemTagId = idRaw.Uint32()
3943
}
4044
}
45+
46+
// slangTagId 获取俚语标签id
47+
// 从配置文件中获取
48+
func slangTagId() {
49+
cfg, _ := gcfg.New()
50+
idRaw, err := cfg.Get(gctx.New(), "slangTagId")
51+
if err != nil {
52+
SlangTagId = 0
53+
} else {
54+
SlangTagId = idRaw.Uint32()
55+
}
56+
}

0 commit comments

Comments
 (0)