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 (
|
||||
EmailOrPasswordWrong = "error.user.email_or_password_wrong"
|
||||
EmailOrPasswordWrong = "error.object.email_or_password_incorrect"
|
||||
CommentNotFound = "error.comment.not_found"
|
||||
QuestionNotFound = "error.question.not_found"
|
||||
AnswerNotFound = "error.answer.not_found"
|
||||
|
|
|
@ -26,10 +26,10 @@ type MyValidator struct {
|
|||
Lang i18n.Language
|
||||
}
|
||||
|
||||
// ErrorField error field
|
||||
type ErrorField struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
// FormErrorField indicates the current form error content. which field is error and error message.
|
||||
type FormErrorField struct {
|
||||
ErrorField string `json:"error_field"`
|
||||
ErrorMsg string `json:"error_msg"`
|
||||
}
|
||||
|
||||
// GlobalValidatorMapping is a mapping from validator to translator used
|
||||
|
@ -87,7 +87,7 @@ func GetValidatorByLang(la string) *MyValidator {
|
|||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
var valErrors validator.ValidationErrors
|
||||
|
@ -97,9 +97,9 @@ func (m *MyValidator) Check(value interface{}) (errField *ErrorField, err error)
|
|||
}
|
||||
|
||||
for _, fieldError := range valErrors {
|
||||
errField = &ErrorField{
|
||||
Key: fieldError.Field(),
|
||||
Value: fieldError.Translate(m.Tran),
|
||||
errField := &FormErrorField{
|
||||
ErrorField: fieldError.Field(),
|
||||
ErrorMsg: fieldError.Translate(m.Tran),
|
||||
}
|
||||
|
||||
// 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 {
|
||||
originalTag := getObjectTagByFieldName(value, fieldName)
|
||||
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 {
|
||||
errField, err = v.Check()
|
||||
errFields, err = v.Check()
|
||||
if err != nil {
|
||||
return errField, err
|
||||
return errFields, err
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
|
@ -126,7 +133,7 @@ func (m *MyValidator) Check(value interface{}) (errField *ErrorField, err error)
|
|||
|
||||
// Checker .
|
||||
type Checker interface {
|
||||
Check() (errField *ErrorField, err error)
|
||||
Check() (errField []*FormErrorField, err error)
|
||||
}
|
||||
|
||||
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())
|
||||
resp := schema.UserVerifyEmailErrorResponse{
|
||||
Key: "e_mail",
|
||||
Value: "error.object.email_or_password_incorrect",
|
||||
Value: reason.EmailOrPasswordWrong,
|
||||
}
|
||||
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
|
||||
}
|
||||
uc.actionService.ActionRecordDel(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
||||
|
|
|
@ -25,7 +25,7 @@ type GetTagInfoReq struct {
|
|||
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 {
|
||||
return nil, errors.BadRequest(reason.RequestFormatError)
|
||||
}
|
||||
|
@ -155,9 +155,9 @@ type UpdateTagReq struct {
|
|||
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 {
|
||||
r.EditSummary = "tag.edit.summary" // to i18n
|
||||
r.EditSummary = "tag.edit.summary" // to do i18n
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -219,10 +219,10 @@ var UserStatusShowMsg = map[int]string{
|
|||
|
||||
// EmailLogin
|
||||
type UserEmailLogin struct {
|
||||
Email string `json:"e_mail" ` // e_mail
|
||||
Pass string `json:"pass" ` // password
|
||||
CaptchaID string `json:"captcha_id" ` // captcha_id
|
||||
CaptchaCode string `json:"captcha_code" ` // captcha_code
|
||||
Email string `validate:"required,email,gt=0,lte=500" json:"e_mail"` // e_mail
|
||||
Pass string `validate:"required,gte=8,lte=32" json:"pass"` // password
|
||||
CaptchaID string `json:"captcha_id"` // captcha_id
|
||||
CaptchaCode string `json:"captcha_code"` // captcha_code
|
||||
}
|
||||
|
||||
// UserRegisterReq user register request
|
||||
|
@ -236,14 +236,16 @@ type UserRegisterReq struct {
|
|||
IP string `json:"-" `
|
||||
}
|
||||
|
||||
func (u *UserRegisterReq) Check() (errField *validator.ErrorField, err error) {
|
||||
func (u *UserRegisterReq) Check() (errFields []*validator.FormErrorField, err error) {
|
||||
// TODO i18n
|
||||
err = checker.CheckPassword(8, 32, 0, u.Pass)
|
||||
if err != nil {
|
||||
return &validator.ErrorField{
|
||||
Key: "pass",
|
||||
Value: err.Error(),
|
||||
}, err
|
||||
errField := &validator.FormErrorField{
|
||||
ErrorField: "pass",
|
||||
ErrorMsg: err.Error(),
|
||||
}
|
||||
errFields = append(errFields, errField)
|
||||
return errFields, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -255,14 +257,16 @@ type UserModifyPassWordRequest struct {
|
|||
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
|
||||
err = checker.CheckPassword(8, 32, 0, u.Pass)
|
||||
if err != nil {
|
||||
return &validator.ErrorField{
|
||||
Key: "pass",
|
||||
Value: err.Error(),
|
||||
}, err
|
||||
errField := &validator.FormErrorField{
|
||||
ErrorField: "pass",
|
||||
ErrorMsg: err.Error(),
|
||||
}
|
||||
errFields = append(errFields, errField)
|
||||
return errFields, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -292,16 +296,17 @@ type AvatarInfo struct {
|
|||
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 {
|
||||
re := regexp.MustCompile(`^[a-z0-9._-]{4,30}$`)
|
||||
match := re.MatchString(u.Username)
|
||||
if !match {
|
||||
err = errors.BadRequest(reason.UsernameInvalid)
|
||||
return &validator.ErrorField{
|
||||
Key: "username",
|
||||
Value: err.Error(),
|
||||
}, err
|
||||
errField := &validator.FormErrorField{
|
||||
ErrorField: "username",
|
||||
ErrorMsg: err.Error(),
|
||||
}
|
||||
errFields = append(errFields, errField)
|
||||
return errFields, errors.BadRequest(reason.UsernameInvalid)
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
|
@ -327,14 +332,16 @@ type UserRePassWordRequest struct {
|
|||
Content string `json:"-"`
|
||||
}
|
||||
|
||||
func (u *UserRePassWordRequest) Check() (errField *validator.ErrorField, err error) {
|
||||
func (u *UserRePassWordRequest) Check() (errFields []*validator.FormErrorField, err error) {
|
||||
// TODO i18n
|
||||
err = checker.CheckPassword(8, 32, 0, u.Pass)
|
||||
if err != nil {
|
||||
return &validator.ErrorField{
|
||||
Key: "pass",
|
||||
Value: err.Error(),
|
||||
}, err
|
||||
errField := &validator.FormErrorField{
|
||||
ErrorField: "pass",
|
||||
ErrorMsg: err.Error(),
|
||||
}
|
||||
errFields = append(errFields, errField)
|
||||
return errFields, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"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/base/validator"
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/schema"
|
||||
"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
|
||||
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 len(errData) > 0 {
|
||||
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
|
||||
return errData, err
|
||||
}
|
||||
|
||||
content, _ := json.Marshal(req)
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"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/schema"
|
||||
"github.com/answerdev/answer/internal/service/revision_common"
|
||||
|
@ -74,18 +75,52 @@ func (ts *TagCommonService) GetSiteWriteRecommendTag(ctx context.Context) (tags
|
|||
return tags, nil
|
||||
}
|
||||
|
||||
func (ts *TagCommonService) SetSiteWriteRecommendTag(ctx context.Context, tags []string, required bool, userID string) (msg string, err error) {
|
||||
err = ts.UpdateTag(ctx, tags, userID)
|
||||
if err != nil {
|
||||
return err.Error(), err
|
||||
func (ts *TagCommonService) SetSiteWriteTag(ctx context.Context, recommendTags, reservedTags []string, userID string) (
|
||||
errFields []*validator.FormErrorField, err error) {
|
||||
recommendErr := ts.CheckTag(ctx, recommendTags, userID)
|
||||
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 err != nil {
|
||||
return "", err
|
||||
if reservedErr != nil {
|
||||
errFields = append(errFields, &validator.FormErrorField{
|
||||
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) {
|
||||
tags = make([]string, 0)
|
||||
list, err := ts.tagRepo.GetReservedTagList(ctx)
|
||||
|
@ -95,20 +130,20 @@ func (ts *TagCommonService) GetSiteWriteReservedTag(ctx context.Context) (tags [
|
|||
return tags, nil
|
||||
}
|
||||
|
||||
func (ts *TagCommonService) SetSiteWriteReservedTag(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, "reserved", true)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
// func (ts *TagCommonService) SetSiteWriteReservedTag(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, "reserved")
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
// return "", nil
|
||||
// }
|
||||
|
||||
// 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
|
||||
switch attribute {
|
||||
case "recommend":
|
||||
|
@ -122,7 +157,7 @@ func (ts *TagCommonService) SetTagsAttribute(ctx context.Context, tags []string,
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ts.tagRepo.UpdateTagsAttribute(ctx, tags, attribute, value)
|
||||
err = ts.tagRepo.UpdateTagsAttribute(ctx, tags, attribute, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -240,7 +275,7 @@ func (ts *TagCommonService) BatchGetObjectTag(ctx context.Context, objectIds []s
|
|||
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 {
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue