Merge branch 'ai/0.4.0/tag' into 'test'

Ai/0.4.0/tag

See merge request opensource/answer!234
This commit is contained in:
linkinstar 2022-11-15 06:19:38 +00:00
commit c299304b66
9 changed files with 136 additions and 33 deletions

View File

@ -173,7 +173,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
reasonService := reason2.NewReasonService(reasonRepo)
reasonController := controller.NewReasonController(reasonService)
themeController := controller_backyard.NewThemeController()
siteInfoService := siteinfo.NewSiteInfoService(siteInfoRepo, emailService)
siteInfoService := siteinfo.NewSiteInfoService(siteInfoRepo, emailService, tagCommonService)
siteInfoController := controller_backyard.NewSiteInfoController(siteInfoService)
siteinfoController := controller.NewSiteinfoController(siteInfoCommonService)
notificationRepo := notification.NewNotificationRepo(dataData)

View File

@ -5936,8 +5936,20 @@ const docTemplate = `{
"required_tag"
],
"properties": {
"recommend_tags": {
"type": "array",
"items": {
"type": "string"
}
},
"required_tag": {
"type": "boolean"
},
"reserved_tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
@ -5947,8 +5959,20 @@ const docTemplate = `{
"required_tag"
],
"properties": {
"recommend_tags": {
"type": "array",
"items": {
"type": "string"
}
},
"required_tag": {
"type": "boolean"
},
"reserved_tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
},

View File

@ -5924,8 +5924,20 @@
"required_tag"
],
"properties": {
"recommend_tags": {
"type": "array",
"items": {
"type": "string"
}
},
"required_tag": {
"type": "boolean"
},
"reserved_tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
@ -5935,8 +5947,20 @@
"required_tag"
],
"properties": {
"recommend_tags": {
"type": "array",
"items": {
"type": "string"
}
},
"required_tag": {
"type": "boolean"
},
"reserved_tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
},

View File

@ -1182,15 +1182,31 @@ definitions:
type: object
schema.SiteWriteReq:
properties:
recommend_tags:
items:
type: string
type: array
required_tag:
type: boolean
reserved_tags:
items:
type: string
type: array
required:
- required_tag
type: object
schema.SiteWriteResp:
properties:
recommend_tags:
items:
type: string
type: array
required_tag:
type: boolean
reserved_tags:
items:
type: string
type: array
required:
- required_tag
type: object

View File

@ -2,6 +2,7 @@ package controller_backyard
import (
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/middleware"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/siteinfo"
"github.com/gin-gonic/gin"
@ -152,6 +153,8 @@ func (sc *SiteInfoController) UpdateSiteWrite(ctx *gin.Context) {
if handler.BindAndCheck(ctx, req) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
err := sc.siteInfoService.SaveSiteWrite(ctx, req)
handler.HandleResponse(ctx, err, nil)
}

View File

@ -100,7 +100,7 @@ func (tr *tagRepo) GetRecommendTagList(ctx context.Context) (tagList []*entity.T
cond := &entity.Tag{}
session := tr.data.DB.Where("")
cond.Recommend = true
session.Where(builder.Eq{"status": entity.TagStatusAvailable})
// session.Where(builder.Eq{"status": entity.TagStatusAvailable})
session.Asc("slug_name")
session.UseBool("recommend")
err = session.Find(&tagList, cond)
@ -115,7 +115,7 @@ func (tr *tagRepo) GetReservedTagList(ctx context.Context) (tagList []*entity.Ta
cond := &entity.Tag{}
session := tr.data.DB.Where("")
cond.Reserved = true
session.Where(builder.Eq{"status": entity.TagStatusAvailable})
// session.Where(builder.Eq{"status": entity.TagStatusAvailable})
session.Asc("slug_name")
session.UseBool("reserved")
err = session.Find(&tagList, cond)
@ -129,7 +129,7 @@ func (tr *tagRepo) GetReservedTagList(ctx context.Context) (tagList []*entity.Ta
func (tr *tagRepo) GetTagListByNames(ctx context.Context, names []string) (tagList []*entity.Tag, err error) {
tagList = make([]*entity.Tag, 0)
session := tr.data.DB.In("slug_name", names)
session.Where(builder.Eq{"status": entity.TagStatusAvailable})
// session.Where(builder.Eq{"status": entity.TagStatusAvailable})
err = session.Find(&tagList)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()

View File

@ -39,7 +39,10 @@ type SiteBrandingReq struct {
// SiteWriteReq site write request
type SiteWriteReq struct {
RequiredTag bool `validate:"required" form:"required_tag" json:"required_tag"`
RequiredTag bool `validate:"required" form:"required_tag" json:"required_tag"`
RecommendTags []string `validate:"omitempty,dive,gt=0,lte=65536" form:"recommend_tags" json:"recommend_tags"`
ReservedTags []string `validate:"omitempty,dive,gt=0,lte=65536" form:"reserved_tags" json:"reserved_tags"`
UserID string `json:"-"`
}
// SiteLegalReq site branding request

View File

@ -11,19 +11,25 @@ import (
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/export"
"github.com/answerdev/answer/internal/service/siteinfo_common"
tagcommon "github.com/answerdev/answer/internal/service/tag_common"
"github.com/jinzhu/copier"
"github.com/segmentfault/pacman/errors"
)
type SiteInfoService struct {
siteInfoRepo siteinfo_common.SiteInfoRepo
emailService *export.EmailService
siteInfoRepo siteinfo_common.SiteInfoRepo
emailService *export.EmailService
tagCommonService *tagcommon.TagCommonService
}
func NewSiteInfoService(siteInfoRepo siteinfo_common.SiteInfoRepo, emailService *export.EmailService) *SiteInfoService {
func NewSiteInfoService(
siteInfoRepo siteinfo_common.SiteInfoRepo,
emailService *export.EmailService,
tagCommonService *tagcommon.TagCommonService) *SiteInfoService {
return &SiteInfoService{
siteInfoRepo: siteInfoRepo,
emailService: emailService,
siteInfoRepo: siteInfoRepo,
emailService: emailService,
tagCommonService: tagCommonService,
}
}
@ -71,7 +77,7 @@ func (s *SiteInfoService) GetSiteBranding(ctx context.Context) (resp *schema.Sit
}
// GetSiteWrite get site info write
func (s *SiteInfoService) GetSiteWrite(ctx context.Context) (resp *schema.SiteWriteReq, err error) {
func (s *SiteInfoService) GetSiteWrite(ctx context.Context) (resp *schema.SiteWriteResp, err error) {
siteInfo, exist, err := s.siteInfoRepo.GetByType(ctx, constant.SiteTypeWrite)
if err != nil {
return nil, err
@ -79,8 +85,17 @@ func (s *SiteInfoService) GetSiteWrite(ctx context.Context) (resp *schema.SiteWr
if !exist {
return nil, errors.BadRequest(reason.SiteInfoNotFound)
}
resp = &schema.SiteWriteReq{}
resp = &schema.SiteWriteResp{}
_ = json.Unmarshal([]byte(siteInfo.Content), resp)
resp.RecommendTags, err = s.tagCommonService.GetSiteWriteRecommendTag(ctx)
if err != nil {
return nil, err
}
resp.ReservedTags, err = s.tagCommonService.GetSiteWriteReservedTag(ctx)
if err != nil {
return nil, err
}
return resp, nil
}
@ -164,6 +179,16 @@ func (s *SiteInfoService) SaveSiteBranding(ctx context.Context, req *schema.Site
// SaveSiteWrite save site configuration about write
func (s *SiteInfoService) SaveSiteWrite(ctx context.Context, req *schema.SiteWriteReq) (err error) {
err = s.tagCommonService.SetSiteWriteRecommendTag(ctx, req.RecommendTags, req.RequiredTag, req.UserID)
if err != nil {
return err
}
err = s.tagCommonService.SetSiteWriteReservedTag(ctx, req.ReservedTags, req.UserID)
if err != nil {
return err
}
content, _ := json.Marshal(req)
data := &entity.SiteInfo{
Type: constant.SiteTypeWrite,

View File

@ -3,13 +3,15 @@ package tagcommon
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/answerdev/answer/internal/service/revision_common"
"github.com/answerdev/answer/internal/service/siteinfo_common"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/revision_common"
"github.com/answerdev/answer/internal/service/siteinfo_common"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
)
@ -244,6 +246,7 @@ func (ts *TagCommonService) UpdateTag(ctx context.Context, tags []string, userID
}
addTagList := make([]*entity.Tag, 0)
addTagMsgList := make([]string, 0)
for _, tag := range tags {
_, ok := tagInDbMapping[tag]
if ok {
@ -256,27 +259,32 @@ func (ts *TagCommonService) UpdateTag(ctx context.Context, tags []string, userID
item.ParsedText = ""
item.Status = entity.TagStatusAvailable
addTagList = append(addTagList, item)
addTagMsgList = append(addTagMsgList, tag)
}
if len(addTagList) > 0 {
err = ts.tagRepo.AddTagList(ctx, addTagList)
if err != nil {
return err
}
for _, tag := range addTagList {
thisTagIDList = append(thisTagIDList, tag.ID)
revisionDTO := &schema.AddRevisionDTO{
UserID: userID,
ObjectID: tag.ID,
Title: tag.SlugName,
}
tagInfoJson, _ := json.Marshal(tag)
revisionDTO.Content = string(tagInfoJson)
err = ts.revisionService.AddRevision(ctx, revisionDTO, true)
if err != nil {
return err
}
}
err = errors.BadRequest(reason.TagNotFound).WithMsg(fmt.Sprintf("tag [%s] does not exist",
strings.Join(addTagMsgList, ",")))
return err
// todo if need add
// err = ts.tagRepo.AddTagList(ctx, addTagList)
// if err != nil {
// return err
// }
// for _, tag := range addTagList {
// thisTagIDList = append(thisTagIDList, tag.ID)
// revisionDTO := &schema.AddRevisionDTO{
// UserID: userID,
// ObjectID: tag.ID,
// Title: tag.SlugName,
// }
// tagInfoJson, _ := json.Marshal(tag)
// revisionDTO.Content = string(tagInfoJson)
// err = ts.revisionService.AddRevision(ctx, revisionDTO, true)
// if err != nil {
// return err
// }
// }
}
return nil