diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml index 69a39c12..85d01f54 100644 --- a/i18n/en_US.yaml +++ b/i18n/en_US.yaml @@ -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: diff --git a/internal/base/reason/reason.go b/internal/base/reason/reason.go index a33e33cc..13801cea 100644 --- a/internal/base/reason/reason.go +++ b/internal/base/reason/reason.go @@ -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" diff --git a/internal/controller/tag_controller.go b/internal/controller/tag_controller.go index bc2a9a5b..70e63d20 100644 --- a/internal/controller/tag_controller.go +++ b/internal/controller/tag_controller.go @@ -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 diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go index b2cb22ce..dc3c271d 100644 --- a/internal/router/answer_api_router.go +++ b/internal/router/answer_api_router.go @@ -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) diff --git a/internal/schema/tag_schema.go b/internal/schema/tag_schema.go index cc046b37..c5d1c7c1 100644 --- a/internal/schema/tag_schema.go +++ b/internal/schema/tag_schema.go @@ -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 diff --git a/internal/service/tag_common/tag_common.go b/internal/service/tag_common/tag_common.go index da7ec466..82148b53 100644 --- a/internal/service/tag_common/tag_common.go +++ b/internal/service/tag_common/tag_common.go @@ -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)