read_notice.go 857 字节
package command

import (
	"fmt"

	"github.com/beego/beego/v2/core/validation"
)

type ReadNoticeCommand struct {
	UserId   int  `json:"userId" valid:"Required"` //读取谁的消息读
	NoticeId int  `json:"noticeId"`                //消息id
	ReadAll  bool `json:"readAll"`                 //是否全部标记为已读
}

func (readNoticeCommand *ReadNoticeCommand) Valid(validation *validation.Validation) {
	if !readNoticeCommand.ReadAll {
		if readNoticeCommand.NoticeId <= 0 {
			validation.AddError("消息id", "未填写")
		}
	}
}

func (readNoticeCommand *ReadNoticeCommand) ValidateCommand() error {
	valid := validation.Validation{}
	b, err := valid.Valid(readNoticeCommand)
	if err != nil {
		return err
	}
	if !b {
		for _, validErr := range valid.Errors {
			return fmt.Errorf("%s  %s", validErr.Key, validErr.Message)
		}
	}
	return nil
}