43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/i18n"
|
|
"spark-server/internal/pkg/reqctx"
|
|
"spark-server/internal/pkg/validate"
|
|
)
|
|
|
|
type RemarkInput struct {
|
|
Remark string `json:"remark" validate:"omitempty,max=64" label:"field.remark" errmsg:"max:validate.friend.remark_invalid"`
|
|
}
|
|
|
|
func (s *Service) SetRemark(ctx context.Context, userID, friendID int64, in RemarkInput) error {
|
|
if !s.ready() || userID <= 0 {
|
|
return errs.ErrUnauthorized
|
|
}
|
|
if err := validate.StructLang(reqctx.Lang(ctx), in); err != nil {
|
|
return err
|
|
}
|
|
if err := s.requireFriend(ctx, userID, friendID); err != nil {
|
|
return err
|
|
}
|
|
return s.repo.SetRemark(ctx, userID, friendID, strings.TrimSpace(in.Remark))
|
|
}
|
|
|
|
func (s *Service) requireFriend(ctx context.Context, userID, friendID int64) error {
|
|
if friendID <= 0 {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
ok, err := s.AreFriends(ctx, userID, friendID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return errs.ErrNotFound.WithMsg(i18n.T(reqctx.Lang(ctx), i18n.KeyFriendNotFriend, nil))
|
|
}
|
|
return nil
|
|
}
|