mirror of https://gitee.com/answerdev/answer.git
Merge branch 'ai/0.4.0/tag' into 'test'
Ai/0.4.0/tag See merge request opensource/answer!252
This commit is contained in:
commit
e9dfeeb638
|
@ -14,7 +14,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EmailOrPasswordWrong = "error.user.email_or_password_wrong"
|
EmailOrPasswordWrong = "error.object.email_or_password_incorrect"
|
||||||
CommentNotFound = "error.comment.not_found"
|
CommentNotFound = "error.comment.not_found"
|
||||||
QuestionNotFound = "error.question.not_found"
|
QuestionNotFound = "error.question.not_found"
|
||||||
AnswerNotFound = "error.answer.not_found"
|
AnswerNotFound = "error.answer.not_found"
|
||||||
|
|
|
@ -26,10 +26,10 @@ type MyValidator struct {
|
||||||
Lang i18n.Language
|
Lang i18n.Language
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrorField error field
|
// FormErrorField indicates the current form error content. which field is error and error message.
|
||||||
type ErrorField struct {
|
type FormErrorField struct {
|
||||||
Key string `json:"key"`
|
ErrorField string `json:"error_field"`
|
||||||
Value string `json:"value"`
|
ErrorMsg string `json:"error_msg"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GlobalValidatorMapping is a mapping from validator to translator used
|
// GlobalValidatorMapping is a mapping from validator to translator used
|
||||||
|
@ -87,7 +87,7 @@ func GetValidatorByLang(la string) *MyValidator {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check /
|
// Check /
|
||||||
func (m *MyValidator) Check(value interface{}) (errField *ErrorField, err error) {
|
func (m *MyValidator) Check(value interface{}) (errFields []*FormErrorField, err error) {
|
||||||
err = m.Validate.Struct(value)
|
err = m.Validate.Struct(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var valErrors validator.ValidationErrors
|
var valErrors validator.ValidationErrors
|
||||||
|
@ -97,9 +97,9 @@ func (m *MyValidator) Check(value interface{}) (errField *ErrorField, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, fieldError := range valErrors {
|
for _, fieldError := range valErrors {
|
||||||
errField = &ErrorField{
|
errField := &FormErrorField{
|
||||||
Key: fieldError.Field(),
|
ErrorField: fieldError.Field(),
|
||||||
Value: fieldError.Translate(m.Tran),
|
ErrorMsg: fieldError.Translate(m.Tran),
|
||||||
}
|
}
|
||||||
|
|
||||||
// get original tag name from value for set err field key.
|
// get original tag name from value for set err field key.
|
||||||
|
@ -108,17 +108,24 @@ func (m *MyValidator) Check(value interface{}) (errField *ErrorField, err error)
|
||||||
if found {
|
if found {
|
||||||
originalTag := getObjectTagByFieldName(value, fieldName)
|
originalTag := getObjectTagByFieldName(value, fieldName)
|
||||||
if len(originalTag) > 0 {
|
if len(originalTag) > 0 {
|
||||||
errField.Key = originalTag
|
errField.ErrorField = originalTag
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return errField, myErrors.BadRequest(reason.RequestFormatError).WithMsg(fieldError.Translate(m.Tran))
|
errFields = append(errFields, errField)
|
||||||
|
}
|
||||||
|
if len(errFields) > 0 {
|
||||||
|
errMsg := ""
|
||||||
|
if len(errFields) == 1 {
|
||||||
|
errMsg = errFields[0].ErrorMsg
|
||||||
|
}
|
||||||
|
return errFields, myErrors.BadRequest(reason.RequestFormatError).WithMsg(errMsg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := value.(Checker); ok {
|
if v, ok := value.(Checker); ok {
|
||||||
errField, err = v.Check()
|
errFields, err = v.Check()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errField, err
|
return errFields, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -126,7 +133,7 @@ func (m *MyValidator) Check(value interface{}) (errField *ErrorField, err error)
|
||||||
|
|
||||||
// Checker .
|
// Checker .
|
||||||
type Checker interface {
|
type Checker interface {
|
||||||
Check() (errField *ErrorField, err error)
|
Check() (errField []*FormErrorField, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getObjectTagByFieldName(obj interface{}, fieldName string) (tag string) {
|
func getObjectTagByFieldName(obj interface{}, fieldName string) (tag string) {
|
||||||
|
|
|
@ -115,10 +115,10 @@ func (uc *UserController) UserEmailLogin(ctx *gin.Context) {
|
||||||
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
resp := schema.UserVerifyEmailErrorResponse{
|
||||||
Key: "e_mail",
|
Key: "e_mail",
|
||||||
Value: "error.object.email_or_password_incorrect",
|
Value: reason.EmailOrPasswordWrong,
|
||||||
}
|
}
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.EmailOrPasswordWrong), resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
uc.actionService.ActionRecordDel(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
uc.actionService.ActionRecordDel(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
||||||
|
|
|
@ -25,7 +25,7 @@ type GetTagInfoReq struct {
|
||||||
UserID string `json:"-"`
|
UserID string `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *GetTagInfoReq) Check() (errField *validator.ErrorField, err error) {
|
func (r *GetTagInfoReq) Check() (errFields []*validator.FormErrorField, err error) {
|
||||||
if len(r.ID) == 0 && len(r.Name) == 0 {
|
if len(r.ID) == 0 && len(r.Name) == 0 {
|
||||||
return nil, errors.BadRequest(reason.RequestFormatError)
|
return nil, errors.BadRequest(reason.RequestFormatError)
|
||||||
}
|
}
|
||||||
|
@ -155,9 +155,9 @@ type UpdateTagReq struct {
|
||||||
UserID string `json:"-"`
|
UserID string `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *UpdateTagReq) Check() (errField *validator.ErrorField, err error) {
|
func (r *UpdateTagReq) Check() (errFields []*validator.FormErrorField, err error) {
|
||||||
if len(r.EditSummary) == 0 {
|
if len(r.EditSummary) == 0 {
|
||||||
r.EditSummary = "tag.edit.summary" // to i18n
|
r.EditSummary = "tag.edit.summary" // to do i18n
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -219,10 +219,10 @@ var UserStatusShowMsg = map[int]string{
|
||||||
|
|
||||||
// EmailLogin
|
// EmailLogin
|
||||||
type UserEmailLogin struct {
|
type UserEmailLogin struct {
|
||||||
Email string `json:"e_mail" ` // e_mail
|
Email string `validate:"required,email,gt=0,lte=500" json:"e_mail"` // e_mail
|
||||||
Pass string `json:"pass" ` // password
|
Pass string `validate:"required,gte=8,lte=32" json:"pass"` // password
|
||||||
CaptchaID string `json:"captcha_id" ` // captcha_id
|
CaptchaID string `json:"captcha_id"` // captcha_id
|
||||||
CaptchaCode string `json:"captcha_code" ` // captcha_code
|
CaptchaCode string `json:"captcha_code"` // captcha_code
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserRegisterReq user register request
|
// UserRegisterReq user register request
|
||||||
|
@ -236,14 +236,16 @@ type UserRegisterReq struct {
|
||||||
IP string `json:"-" `
|
IP string `json:"-" `
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserRegisterReq) Check() (errField *validator.ErrorField, err error) {
|
func (u *UserRegisterReq) Check() (errFields []*validator.FormErrorField, err error) {
|
||||||
// TODO i18n
|
// TODO i18n
|
||||||
err = checker.CheckPassword(8, 32, 0, u.Pass)
|
err = checker.CheckPassword(8, 32, 0, u.Pass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &validator.ErrorField{
|
errField := &validator.FormErrorField{
|
||||||
Key: "pass",
|
ErrorField: "pass",
|
||||||
Value: err.Error(),
|
ErrorMsg: err.Error(),
|
||||||
}, err
|
}
|
||||||
|
errFields = append(errFields, errField)
|
||||||
|
return errFields, err
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -255,14 +257,16 @@ type UserModifyPassWordRequest struct {
|
||||||
Pass string `json:"pass" ` // password
|
Pass string `json:"pass" ` // password
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserModifyPassWordRequest) Check() (errField *validator.ErrorField, err error) {
|
func (u *UserModifyPassWordRequest) Check() (errFields []*validator.FormErrorField, err error) {
|
||||||
// TODO i18n
|
// TODO i18n
|
||||||
err = checker.CheckPassword(8, 32, 0, u.Pass)
|
err = checker.CheckPassword(8, 32, 0, u.Pass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &validator.ErrorField{
|
errField := &validator.FormErrorField{
|
||||||
Key: "pass",
|
ErrorField: "pass",
|
||||||
Value: err.Error(),
|
ErrorMsg: err.Error(),
|
||||||
}, err
|
}
|
||||||
|
errFields = append(errFields, errField)
|
||||||
|
return errFields, err
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -292,16 +296,17 @@ type AvatarInfo struct {
|
||||||
Custom string `validate:"omitempty,gt=0,lte=200" json:"custom"`
|
Custom string `validate:"omitempty,gt=0,lte=200" json:"custom"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UpdateInfoRequest) Check() (errField *validator.ErrorField, err error) {
|
func (u *UpdateInfoRequest) Check() (errFields []*validator.FormErrorField, err error) {
|
||||||
if len(u.Username) > 0 {
|
if len(u.Username) > 0 {
|
||||||
re := regexp.MustCompile(`^[a-z0-9._-]{4,30}$`)
|
re := regexp.MustCompile(`^[a-z0-9._-]{4,30}$`)
|
||||||
match := re.MatchString(u.Username)
|
match := re.MatchString(u.Username)
|
||||||
if !match {
|
if !match {
|
||||||
err = errors.BadRequest(reason.UsernameInvalid)
|
errField := &validator.FormErrorField{
|
||||||
return &validator.ErrorField{
|
ErrorField: "username",
|
||||||
Key: "username",
|
ErrorMsg: err.Error(),
|
||||||
Value: err.Error(),
|
}
|
||||||
}, err
|
errFields = append(errFields, errField)
|
||||||
|
return errFields, errors.BadRequest(reason.UsernameInvalid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -327,14 +332,16 @@ type UserRePassWordRequest struct {
|
||||||
Content string `json:"-"`
|
Content string `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserRePassWordRequest) Check() (errField *validator.ErrorField, err error) {
|
func (u *UserRePassWordRequest) Check() (errFields []*validator.FormErrorField, err error) {
|
||||||
// TODO i18n
|
// TODO i18n
|
||||||
err = checker.CheckPassword(8, 32, 0, u.Pass)
|
err = checker.CheckPassword(8, 32, 0, u.Pass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &validator.ErrorField{
|
errField := &validator.FormErrorField{
|
||||||
Key: "pass",
|
ErrorField: "pass",
|
||||||
Value: err.Error(),
|
ErrorMsg: err.Error(),
|
||||||
}, err
|
}
|
||||||
|
errFields = append(errFields, errField)
|
||||||
|
return errFields, err
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"github.com/answerdev/answer/internal/base/constant"
|
"github.com/answerdev/answer/internal/base/constant"
|
||||||
"github.com/answerdev/answer/internal/base/reason"
|
"github.com/answerdev/answer/internal/base/reason"
|
||||||
"github.com/answerdev/answer/internal/base/translator"
|
"github.com/answerdev/answer/internal/base/translator"
|
||||||
"github.com/answerdev/answer/internal/base/validator"
|
|
||||||
"github.com/answerdev/answer/internal/entity"
|
"github.com/answerdev/answer/internal/entity"
|
||||||
"github.com/answerdev/answer/internal/schema"
|
"github.com/answerdev/answer/internal/schema"
|
||||||
"github.com/answerdev/answer/internal/service/export"
|
"github.com/answerdev/answer/internal/service/export"
|
||||||
|
@ -179,26 +178,9 @@ func (s *SiteInfoService) SaveSiteBranding(ctx context.Context, req *schema.Site
|
||||||
|
|
||||||
// SaveSiteWrite save site configuration about write
|
// SaveSiteWrite save site configuration about write
|
||||||
func (s *SiteInfoService) SaveSiteWrite(ctx context.Context, req *schema.SiteWriteReq) (resp interface{}, err error) {
|
func (s *SiteInfoService) SaveSiteWrite(ctx context.Context, req *schema.SiteWriteReq) (resp interface{}, err error) {
|
||||||
errData, err := s.tagCommonService.SetSiteWriteRecommendTag(ctx, req.RecommendTags, req.RequiredTag, req.UserID)
|
errData, err := s.tagCommonService.SetSiteWriteTag(ctx, req.RecommendTags, req.ReservedTags, req.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(errData) > 0 {
|
return errData, err
|
||||||
resp = &validator.ErrorField{
|
|
||||||
Key: "recommend_tags",
|
|
||||||
Value: errData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return resp, err
|
|
||||||
}
|
|
||||||
|
|
||||||
errData, err = s.tagCommonService.SetSiteWriteReservedTag(ctx, req.ReservedTags, req.UserID)
|
|
||||||
if err != nil {
|
|
||||||
if len(errData) > 0 {
|
|
||||||
resp = &validator.ErrorField{
|
|
||||||
Key: "reserved_tags",
|
|
||||||
Value: errData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return resp, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
content, _ := json.Marshal(req)
|
content, _ := json.Marshal(req)
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/answerdev/answer/internal/base/reason"
|
"github.com/answerdev/answer/internal/base/reason"
|
||||||
|
"github.com/answerdev/answer/internal/base/validator"
|
||||||
"github.com/answerdev/answer/internal/entity"
|
"github.com/answerdev/answer/internal/entity"
|
||||||
"github.com/answerdev/answer/internal/schema"
|
"github.com/answerdev/answer/internal/schema"
|
||||||
"github.com/answerdev/answer/internal/service/revision_common"
|
"github.com/answerdev/answer/internal/service/revision_common"
|
||||||
|
@ -74,18 +75,52 @@ func (ts *TagCommonService) GetSiteWriteRecommendTag(ctx context.Context) (tags
|
||||||
return tags, nil
|
return tags, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TagCommonService) SetSiteWriteRecommendTag(ctx context.Context, tags []string, required bool, userID string) (msg string, err error) {
|
func (ts *TagCommonService) SetSiteWriteTag(ctx context.Context, recommendTags, reservedTags []string, userID string) (
|
||||||
err = ts.UpdateTag(ctx, tags, userID)
|
errFields []*validator.FormErrorField, err error) {
|
||||||
if err != nil {
|
recommendErr := ts.CheckTag(ctx, recommendTags, userID)
|
||||||
return err.Error(), err
|
reservedErr := ts.CheckTag(ctx, reservedTags, userID)
|
||||||
|
if recommendErr != nil {
|
||||||
|
errFields = append(errFields, &validator.FormErrorField{
|
||||||
|
ErrorField: "recommend_tags",
|
||||||
|
ErrorMsg: recommendErr.Error(),
|
||||||
|
})
|
||||||
|
err = recommendErr
|
||||||
}
|
}
|
||||||
err = ts.SetTagsAttribute(ctx, tags, "recommend", true)
|
if reservedErr != nil {
|
||||||
if err != nil {
|
errFields = append(errFields, &validator.FormErrorField{
|
||||||
return "", err
|
ErrorField: "reserved_tags",
|
||||||
|
ErrorMsg: reservedErr.Error(),
|
||||||
|
})
|
||||||
|
err = reservedErr
|
||||||
}
|
}
|
||||||
return "", nil
|
if len(errFields) > 0 {
|
||||||
|
return errFields, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ts.SetTagsAttribute(ctx, recommendTags, "recommend")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = ts.SetTagsAttribute(ctx, reservedTags, "reserved")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func (ts *TagCommonService) SetSiteWriteRecommendTag(ctx context.Context, tags []string, userID string) (msg string, err error) {
|
||||||
|
// err = ts.UpdateTag(ctx, tags, userID)
|
||||||
|
// if err != nil {
|
||||||
|
// return err.Error(), err
|
||||||
|
// }
|
||||||
|
// err = ts.SetTagsAttribute(ctx, tags, "recommend")
|
||||||
|
// if err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return "", nil
|
||||||
|
// }
|
||||||
|
|
||||||
func (ts *TagCommonService) GetSiteWriteReservedTag(ctx context.Context) (tags []string, err error) {
|
func (ts *TagCommonService) GetSiteWriteReservedTag(ctx context.Context) (tags []string, err error) {
|
||||||
tags = make([]string, 0)
|
tags = make([]string, 0)
|
||||||
list, err := ts.tagRepo.GetReservedTagList(ctx)
|
list, err := ts.tagRepo.GetReservedTagList(ctx)
|
||||||
|
@ -95,20 +130,20 @@ func (ts *TagCommonService) GetSiteWriteReservedTag(ctx context.Context) (tags [
|
||||||
return tags, nil
|
return tags, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TagCommonService) SetSiteWriteReservedTag(ctx context.Context, tags []string, userID string) (msg string, err error) {
|
// func (ts *TagCommonService) SetSiteWriteReservedTag(ctx context.Context, tags []string, userID string) (msg string, err error) {
|
||||||
err = ts.UpdateTag(ctx, tags, userID)
|
// err = ts.UpdateTag(ctx, tags, userID)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err.Error(), err
|
// return err.Error(), err
|
||||||
}
|
// }
|
||||||
err = ts.SetTagsAttribute(ctx, tags, "reserved", true)
|
// err = ts.SetTagsAttribute(ctx, tags, "reserved")
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return "", err
|
// return "", err
|
||||||
}
|
// }
|
||||||
return "", nil
|
// return "", nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
// SetTagsAttribute
|
// SetTagsAttribute
|
||||||
func (ts *TagCommonService) SetTagsAttribute(ctx context.Context, tags []string, attribute string, value bool) (err error) {
|
func (ts *TagCommonService) SetTagsAttribute(ctx context.Context, tags []string, attribute string) (err error) {
|
||||||
var tagslist []string
|
var tagslist []string
|
||||||
switch attribute {
|
switch attribute {
|
||||||
case "recommend":
|
case "recommend":
|
||||||
|
@ -122,7 +157,7 @@ func (ts *TagCommonService) SetTagsAttribute(ctx context.Context, tags []string,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = ts.tagRepo.UpdateTagsAttribute(ctx, tags, attribute, value)
|
err = ts.tagRepo.UpdateTagsAttribute(ctx, tags, attribute, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -240,7 +275,7 @@ func (ts *TagCommonService) BatchGetObjectTag(ctx context.Context, objectIds []s
|
||||||
return objectIDTagMap, nil
|
return objectIDTagMap, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TagCommonService) UpdateTag(ctx context.Context, tags []string, userID string) (err error) {
|
func (ts *TagCommonService) CheckTag(ctx context.Context, tags []string, userID string) (err error) {
|
||||||
if len(tags) == 0 {
|
if len(tags) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue