mirror of https://gitee.com/answerdev/answer.git
feat(tag): Add create tag API
This commit is contained in:
parent
acf89a399b
commit
ee3fb56d54
|
@ -98,6 +98,8 @@ backend:
|
|||
not_found:
|
||||
other: Report not found.
|
||||
tag:
|
||||
already_exist:
|
||||
other: Tag already exists.
|
||||
not_found:
|
||||
other: Tag not found.
|
||||
recommend_tag_not_found:
|
||||
|
|
|
@ -44,6 +44,7 @@ const (
|
|||
TagNotContainSynonym = "error.tag.not_contain_synonym_tags"
|
||||
TagCannotUpdate = "error.tag.cannot_update"
|
||||
TagIsUsedCannotDelete = "error.tag.is_used_cannot_delete"
|
||||
TagAlreadyExist = "error.tag.already_exist"
|
||||
RankFailToMeetTheCondition = "error.rank.fail_to_meet_the_condition"
|
||||
ThemeNotFound = "error.theme.not_found"
|
||||
LangNotFound = "error.lang.not_found"
|
||||
|
|
|
@ -98,6 +98,42 @@ func (tc *TagController) RemoveTag(ctx *gin.Context) {
|
|||
handler.HandleResponse(ctx, err, nil)
|
||||
}
|
||||
|
||||
// AddTag add tag
|
||||
// @Summary add tag
|
||||
// @Description add tag
|
||||
// @Tags Tag
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param data body schema.AddTagReq true "tag"
|
||||
// @Success 200 {object} handler.RespBody
|
||||
// @Router /answer/api/v1/tag [post]
|
||||
func (tc *TagController) AddTag(ctx *gin.Context) {
|
||||
req := &schema.AddTagReq{}
|
||||
if handler.BindAndCheck(ctx, req) {
|
||||
return
|
||||
}
|
||||
|
||||
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
||||
canList, err := tc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
|
||||
permission.TagAdd,
|
||||
})
|
||||
if err != nil {
|
||||
handler.HandleResponse(ctx, err, nil)
|
||||
return
|
||||
}
|
||||
if !canList[0] {
|
||||
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
|
||||
return
|
||||
}
|
||||
|
||||
err = tc.tagCommonService.AddTag(ctx, req)
|
||||
if err != nil {
|
||||
handler.HandleResponse(ctx, err, nil)
|
||||
} else {
|
||||
handler.HandleResponse(ctx, err, nil)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateTag update tag
|
||||
// @Summary update tag
|
||||
// @Description update tag
|
||||
|
|
|
@ -175,6 +175,7 @@ func (a *AnswerAPIRouter) RegisterAnswerAPIRouter(r *gin.RouterGroup) {
|
|||
|
||||
// tag
|
||||
r.GET("/question/tags", a.tagController.SearchTagLike)
|
||||
r.POST("/tag", a.tagController.AddTag)
|
||||
r.PUT("/tag", a.tagController.UpdateTag)
|
||||
r.DELETE("/tag", a.tagController.RemoveTag)
|
||||
r.PUT("/tag/synonym", a.tagController.UpdateTagSynonym)
|
||||
|
|
|
@ -161,6 +161,30 @@ type RemoveTagReq struct {
|
|||
UserID string `json:"-"`
|
||||
}
|
||||
|
||||
// AddTagReq add tag request
|
||||
type AddTagReq struct {
|
||||
// slug_name
|
||||
SlugName string `validate:"required,gt=0,lte=35" json:"slug_name"`
|
||||
// display_name
|
||||
DisplayName string `validate:"required,gt=0,lte=35" json:"display_name"`
|
||||
// original text
|
||||
OriginalText string `validate:"required" json:"original_text"`
|
||||
// parsed text
|
||||
ParsedText string `json:"-"`
|
||||
// user id
|
||||
UserID string `json:"-"`
|
||||
}
|
||||
|
||||
func (req *AddTagReq) Check() (errFields []*validator.FormErrorField, err error) {
|
||||
req.ParsedText = converter.Markdown2HTML(req.OriginalText)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// AddTagResp add tag response
|
||||
type AddTagResp struct {
|
||||
TagID string `json:"tag_id"`
|
||||
}
|
||||
|
||||
// UpdateTagReq update tag request
|
||||
type UpdateTagReq struct {
|
||||
// tag_id
|
||||
|
|
|
@ -222,6 +222,27 @@ func (ts *TagCommonService) GetObjectTag(ctx context.Context, objectId string) (
|
|||
return ts.TagFormat(ctx, tagsInfoList)
|
||||
}
|
||||
|
||||
// AddTag get object tag
|
||||
func (ts *TagCommonService) AddTag(ctx context.Context, req *schema.AddTagReq) (err error) {
|
||||
_, exist, err := ts.GetTagBySlugName(ctx, req.SlugName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exist {
|
||||
return errors.BadRequest(reason.TagAlreadyExist)
|
||||
}
|
||||
tagInfo := &entity.Tag{
|
||||
SlugName: req.SlugName,
|
||||
DisplayName: req.DisplayName,
|
||||
OriginalText: req.OriginalText,
|
||||
ParsedText: req.ParsedText,
|
||||
Status: entity.TagStatusAvailable,
|
||||
UserID: req.UserID,
|
||||
}
|
||||
tagList := []*entity.Tag{tagInfo}
|
||||
return ts.tagCommonRepo.AddTagList(ctx, tagList)
|
||||
}
|
||||
|
||||
// AddTagList get object tag
|
||||
func (ts *TagCommonService) AddTagList(ctx context.Context, tagList []*entity.Tag) (err error) {
|
||||
return ts.tagCommonRepo.AddTagList(ctx, tagList)
|
||||
|
|
Loading…
Reference in New Issue