Merge branch 'feat/timezone' into 'test'

style: reformat get site info code

See merge request opensource/answer!170
This commit is contained in:
linkinstar 2022-11-03 03:13:07 +00:00
commit 7934ec513c
5 changed files with 37 additions and 44 deletions

View File

@ -47,3 +47,8 @@ var (
8: ReportObjectType,
}
)
const (
SiteTypeGeneral = "general"
SiteTypeInterface = "interface"
)

View File

@ -38,4 +38,5 @@ const (
LangNotFound = "error.lang.not_found"
ReportHandleFailed = "error.report.handle_failed"
ReportNotFound = "error.report.not_found"
SiteInfoNotFound = "error.site_info.not_found"
)

View File

@ -18,30 +18,21 @@ func NewSiteinfoController(siteInfoService *service.SiteInfoService) *SiteinfoCo
}
}
// GetInfo godoc
// @Summary Get siteinfo
// @Description Get siteinfo
// GetSiteInfo get site info
// @Summary get site info
// @Description get site info
// @Tags site
// @Produce json
// @Success 200 {object} handler.RespBody{data=schema.SiteGeneralResp}
// @Router /answer/api/v1/siteinfo [get]
func (sc *SiteinfoController) GetInfo(ctx *gin.Context) {
var (
resp = &schema.SiteInfoResp{}
general schema.SiteGeneralResp
face schema.SiteInterfaceResp
err error
)
general, err = sc.siteInfoService.GetSiteGeneral(ctx)
resp.General = &general
func (sc *SiteinfoController) GetSiteInfo(ctx *gin.Context) {
var err error
resp := &schema.SiteInfoResp{}
resp.General, err = sc.siteInfoService.GetSiteGeneral(ctx)
if err != nil {
handler.HandleResponse(ctx, err, resp)
return
}
face, err = sc.siteInfoService.GetSiteInterface(ctx)
resp.Face = &face
resp.Face, err = sc.siteInfoService.GetSiteInterface(ctx)
handler.HandleResponse(ctx, err, resp)
}

View File

@ -134,7 +134,7 @@ func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
r.GET("/personal/rank/page", a.rankController.GetRankPersonalWithPage)
//siteinfo
r.GET("/siteinfo", a.siteinfoController.GetInfo)
r.GET("/siteinfo", a.siteinfoController.GetSiteInfo)
}
func (a *AnswerAPIRouter) RegisterAnswerAPIRouter(r *gin.RouterGroup) {

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/entity"
@ -26,38 +27,33 @@ func NewSiteInfoService(siteInfoRepo siteinfo_common.SiteInfoRepo, emailService
}
}
func (s *SiteInfoService) GetSiteGeneral(ctx context.Context) (resp schema.SiteGeneralResp, err error) {
var (
siteType = "general"
siteInfo *entity.SiteInfo
exist bool
)
resp = schema.SiteGeneralResp{}
siteInfo, exist, err = s.siteInfoRepo.GetByType(ctx, siteType)
// GetSiteGeneral get site info general
func (s *SiteInfoService) GetSiteGeneral(ctx context.Context) (resp *schema.SiteGeneralResp, err error) {
siteInfo, exist, err := s.siteInfoRepo.GetByType(ctx, constant.SiteTypeGeneral)
if err != nil {
return nil, err
}
if !exist {
return
return nil, errors.BadRequest(reason.SiteInfoNotFound)
}
_ = json.Unmarshal([]byte(siteInfo.Content), &resp)
return
resp = &schema.SiteGeneralResp{}
_ = json.Unmarshal([]byte(siteInfo.Content), resp)
return resp, nil
}
func (s *SiteInfoService) GetSiteInterface(ctx context.Context) (resp schema.SiteInterfaceResp, err error) {
var (
siteType = "interface"
siteInfo *entity.SiteInfo
exist bool
)
resp = schema.SiteInterfaceResp{}
siteInfo, exist, err = s.siteInfoRepo.GetByType(ctx, siteType)
if !exist {
return
// GetSiteInterface get site info interface
func (s *SiteInfoService) GetSiteInterface(ctx context.Context) (resp *schema.SiteInterfaceResp, err error) {
siteInfo, exist, err := s.siteInfoRepo.GetByType(ctx, constant.SiteTypeInterface)
if err != nil {
return nil, err
}
_ = json.Unmarshal([]byte(siteInfo.Content), &resp)
return
if !exist {
return nil, errors.BadRequest(reason.SiteInfoNotFound)
}
resp = &schema.SiteInterfaceResp{}
_ = json.Unmarshal([]byte(siteInfo.Content), resp)
return resp, nil
}
func (s *SiteInfoService) SaveSiteGeneral(ctx context.Context, req schema.SiteGeneralReq) (err error) {