Skip to content

Commit 8e434c3

Browse files
committed
feat: 增强句子更新功能,支持更新标签
1 parent 1b94794 commit 8e434c3

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

api/sentence/v1/sentence.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ type CreRes struct {
1919
type UpdReq struct {
2020
g.Meta `path:"sentence/update/{id}" method:"post" sm:"修改" tags:"句子"`
2121
*model.IdInput
22-
BookId model.Id `json:"book_id" v:"required"`
23-
Sentence string `json:"sentence" v:"required"`
22+
BookId model.Id `json:"book_id" v:"required"`
23+
TagIds []model.Id `json:"tag_ids"`
24+
Sentence string `json:"sentence" v:"required"`
2425
}
2526

2627
type UpdRes struct {

internal/controller/sentence/sentence_v1_upd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func (c *ControllerV1) Upd(ctx context.Context, req *v1.UpdReq) (res *v1.UpdRes,
1414
Id: req.Id,
1515
BookId: req.BookId,
1616
Sentence: req.Sentence,
17+
TagIds: req.TagIds,
1718
})
1819
return
1920
}

internal/logic/sentence/sentence.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,42 @@ func Cre(ctx context.Context, in *model.SentenceInput) (err error) {
5656
return err
5757
}
5858

59-
// Upd 更新句子,只更新句子内容,不更新标签
59+
// Upd 更新句子
6060
func Upd(ctx context.Context, in *model.SentenceInput) (err error) {
61-
_, err = dao.Sentence.Ctx(ctx).Data(do.Sentence{
62-
BookId: in.BookId,
63-
Sentence: in.Sentence,
64-
}).Where("id", in.Id).Update()
61+
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
62+
_, err = dao.Sentence.Ctx(ctx).Data(do.Sentence{
63+
BookId: in.BookId,
64+
Sentence: in.Sentence,
65+
}).Where("id", in.Id).Update()
66+
67+
// 删除标签关联表数据
68+
_, err = dao.SentenceTag.Ctx(ctx).Where("s_id", in.Id).Delete()
69+
if err != nil {
70+
return nil
71+
}
72+
73+
// 插入标签关联表数据
74+
var sentenceTags []do.SentenceTag
75+
for _, tagId := range in.TagIds {
76+
if tagId == 0 {
77+
continue
78+
}
79+
sentenceTags = append(sentenceTags, do.SentenceTag{
80+
SId: in.Id,
81+
TId: tagId,
82+
})
83+
}
84+
85+
if len(sentenceTags) == 0 {
86+
return nil
87+
}
88+
89+
_, err = dao.SentenceTag.Ctx(ctx).Data(sentenceTags).Insert()
90+
if err != nil {
91+
return err
92+
}
93+
return nil
94+
})
6595
if err != nil {
6696
return utility.Err.Sys(err)
6797
}

0 commit comments

Comments
 (0)