feat: site info update write config

This commit is contained in:
LinkinStar 2022-11-15 11:24:39 +08:00
parent 3662182ff3
commit ba64be6845
4 changed files with 40 additions and 9 deletions

View File

@ -171,7 +171,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

@ -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"
@ -139,6 +140,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

@ -40,6 +40,9 @@ type SiteBrandingReq struct {
// SiteWriteReq site write request
type SiteWriteReq struct {
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:"-"`
}
// SiteGeneralResp site general response

View File

@ -11,6 +11,7 @@ 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"
)
@ -18,12 +19,17 @@ import (
type SiteInfoService struct {
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,
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
}
@ -150,6 +165,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,