|
| 1 | +package message_config |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/globalsign/mgo/bson" |
| 6 | + "github.com/minibear2333/programmer-go/api/common/perr" |
| 7 | + "github.com/minibear2333/programmer-go/api/global" |
| 8 | + "github.com/minibear2333/programmer-go/api/model" |
| 9 | + "github.com/pkg/errors" |
| 10 | + "go.uber.org/zap" |
| 11 | + |
| 12 | + "github.com/minibear2333/programmer-go/api/internal/svc" |
| 13 | + "github.com/minibear2333/programmer-go/api/internal/types" |
| 14 | + |
| 15 | + "github.com/zeromicro/go-zero/core/logx" |
| 16 | +) |
| 17 | + |
| 18 | +type UpdateMessageConfigLogic struct { |
| 19 | + logx.Logger |
| 20 | + ctx context.Context |
| 21 | + svcCtx *svc.ServiceContext |
| 22 | +} |
| 23 | + |
| 24 | +func NewUpdateMessageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) UpdateMessageConfigLogic { |
| 25 | + return UpdateMessageConfigLogic{ |
| 26 | + Logger: logx.WithContext(ctx), |
| 27 | + ctx: ctx, |
| 28 | + svcCtx: svcCtx, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func (l *UpdateMessageConfigLogic) UpdateMessageConfig(req types.MessageConfig) (resp *types.MessageConfig, err error) { |
| 33 | + userID := l.ctx.Value("ID") |
| 34 | + m := model.MessageConfig{ |
| 35 | + Comments: req.Comments, |
| 36 | + Follow: req.Follow, |
| 37 | + GoodAndStar: req.GoodAndStar, |
| 38 | + MailNotice: struct { |
| 39 | + Comments bool `bson:"comments" json:"comments"` |
| 40 | + Follow bool `bson:"follow" json:"follow"` |
| 41 | + GoodAndStar bool `bson:"good_and_star" json:"good_and_star"` |
| 42 | + }{ |
| 43 | + Comments: req.MailNotice.Comments, |
| 44 | + Follow: req.MailNotice.Follow, |
| 45 | + GoodAndStar: req.MailNotice.GoodAndStar, |
| 46 | + }, |
| 47 | + UserID: bson.ObjectIdHex(userID.(string)), |
| 48 | + } |
| 49 | + |
| 50 | + messageConfig, err := global.Mongo.MessageConfigModel.FindOneByUserID(l.ctx, userID.(string)) |
| 51 | + if messageConfig == nil && err == model.ErrNotFound { |
| 52 | + err = global.Mongo.MessageConfigModel.Insert(l.ctx, &m) |
| 53 | + if err != nil { |
| 54 | + global.LOG.Error("创建消息中心配置失败", zap.Error(err)) |
| 55 | + return nil, perr.NewErrCode(perr.MessageConfigGenerateError) |
| 56 | + } |
| 57 | + return &req, nil |
| 58 | + } |
| 59 | + if err != nil { |
| 60 | + global.LOG.Error("通过用户id获取消息中心配置失败", zap.Error(err)) |
| 61 | + return nil, errors.Wrapf(perr.NewErrCode(perr.NotFoundError), "logic.UpdateMessageConfig.FindOneByUserID not found: %s", userID) |
| 62 | + } |
| 63 | + |
| 64 | + messageConfig.Comments = req.Comments |
| 65 | + messageConfig.Follow = req.Follow |
| 66 | + messageConfig.GoodAndStar = req.GoodAndStar |
| 67 | + messageConfig.MailNotice = struct { |
| 68 | + Comments bool `bson:"comments" json:"comments"` |
| 69 | + Follow bool `bson:"follow" json:"follow"` |
| 70 | + GoodAndStar bool `bson:"good_and_star" json:"good_and_star"` |
| 71 | + }{ |
| 72 | + Comments: req.MailNotice.Comments, |
| 73 | + Follow: req.MailNotice.Follow, |
| 74 | + GoodAndStar: req.MailNotice.GoodAndStar, |
| 75 | + } |
| 76 | + |
| 77 | + err = global.Mongo.MessageConfigModel.UpdateByUserID(l.ctx, messageConfig) |
| 78 | + if err != nil { |
| 79 | + global.LOG.Error("更新消息中心配置失败:", zap.Error(err)) |
| 80 | + return nil, errors.Wrapf(perr.NewErrCode(perr.OperateFailError), "logic.UpdateMessageConfig operate fail: %s", userID) |
| 81 | + } |
| 82 | + return &req, nil |
| 83 | +} |
0 commit comments