Skip to content

Commit fe60696

Browse files
committedApr 21, 2022
feat(message_config): 消息中心配置查询与更新
re #18
1 parent 4fe6eaa commit fe60696

13 files changed

+298
-1
lines changed
 

‎README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
如果只是想在本地启动本程序便于前端调试或者体验`api`的话,可以继续;如果是需要参与开发,请跳到 [如何贡献](#如何贡献)
1111

12-
先安装 `docker``docker-compose` 安装完毕后直接在该项目的根目录运行
12+
先安装 `docker``docker-compose``docker` 的安装可以参考 [菜鸟教程](https://www.runoob.com/docker/macos-docker-install.html)`docker-compose``mac``win`上如果安装了桌面版本已自带了
13+
14+
15+
安装完毕后直接在该项目的根目录运行
1316

1417
```bash
1518
docker-compose up -d

‎api/api.api

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"goctl-apis/user.api"
66
"goctl-apis/interviews.api"
77
"goctl-apis/interviews_tags.api"
8+
"goctl-apis/message_config.api"
89
)
910

1011
info(

‎api/common/perr/errCode.go

+4
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ const (
2626
AuthorIdError uint32 = 30001
2727
InterviewGenerateError uint32 = 30002
2828
InvalidInterviewTags uint32 = 30003
29+
)
30+
31+
const (
32+
MessageConfigGenerateError uint32 = 40001
2933
)

‎api/common/perr/errMsg.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ func init() {
2020
AuthorIdError: "错误的作者ID",
2121
InterviewGenerateError: "面试题生成失败",
2222
InvalidInterviewTags: "面试题标签格式错误,请输入平台已存在的标签",
23+
24+
MessageConfigGenerateError: "消息中心替换默认配置失败",
2325
}
2426
}
2527

‎api/goctl-apis/message_center.api

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
syntax = "v1"

‎api/goctl-apis/message_config.api

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
syntax = "v1"
2+
3+
type MailNotice {
4+
Comments bool `json:"comments"`
5+
Follow bool `json:"follow"`
6+
GoodAndStar bool `json:"good_and_star"`
7+
}
8+
9+
type MessageConfig {
10+
Comments bool `json:"comments"`
11+
Follow bool `json:"follow"`
12+
GoodAndStar bool `json:"good_and_star"`
13+
MailNotice MailNotice `json:"mail_notice"`
14+
}
15+
16+
@server(
17+
jwt: Auth
18+
group: message_config
19+
prefix: v1/api
20+
)
21+
22+
service pg-backend {
23+
@handler UpdateMessageConfig
24+
put /message_config (MessageConfig) returns (MessageConfig)
25+
26+
@handler GetMessageConfig
27+
get /message_config () returns (MessageConfig)
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package message_config
2+
3+
import (
4+
"github.com/minibear2333/programmer-go/api/common/result"
5+
"net/http"
6+
7+
"github.com/minibear2333/programmer-go/api/internal/logic/message_config"
8+
"github.com/minibear2333/programmer-go/api/internal/svc"
9+
)
10+
11+
func GetMessageConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
12+
return func(w http.ResponseWriter, r *http.Request) {
13+
l := message_config.NewGetMessageConfigLogic(r.Context(), svcCtx)
14+
resp, err := l.GetMessageConfig()
15+
result.HttpResult(r, w, resp, err)
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package message_config
2+
3+
import (
4+
"github.com/minibear2333/programmer-go/api/common/perr"
5+
"github.com/minibear2333/programmer-go/api/common/result"
6+
"github.com/pkg/errors"
7+
"net/http"
8+
9+
"github.com/minibear2333/programmer-go/api/internal/logic/message_config"
10+
"github.com/minibear2333/programmer-go/api/internal/svc"
11+
"github.com/minibear2333/programmer-go/api/internal/types"
12+
"github.com/zeromicro/go-zero/rest/httpx"
13+
)
14+
15+
func UpdateMessageConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
16+
return func(w http.ResponseWriter, r *http.Request) {
17+
var req types.MessageConfig
18+
if err := httpx.Parse(r, &req); err != nil {
19+
err = errors.Wrap(perr.NewErrCode(perr.InvalidParamError), err.Error())
20+
result.HttpResult(r, w, nil, err)
21+
return
22+
}
23+
24+
l := message_config.NewUpdateMessageConfigLogic(r.Context(), svcCtx)
25+
resp, err := l.UpdateMessageConfig(req)
26+
result.HttpResult(r, w, resp, err)
27+
}
28+
}

‎api/internal/handler/routes.go

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package message_config
2+
3+
import (
4+
"context"
5+
"github.com/minibear2333/programmer-go/api/common/perr"
6+
"github.com/minibear2333/programmer-go/api/global"
7+
"github.com/minibear2333/programmer-go/api/model"
8+
"github.com/pkg/errors"
9+
"go.uber.org/zap"
10+
11+
"github.com/minibear2333/programmer-go/api/internal/svc"
12+
"github.com/minibear2333/programmer-go/api/internal/types"
13+
14+
"github.com/zeromicro/go-zero/core/logx"
15+
)
16+
17+
type GetMessageConfigLogic struct {
18+
logx.Logger
19+
ctx context.Context
20+
svcCtx *svc.ServiceContext
21+
}
22+
23+
func NewGetMessageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) GetMessageConfigLogic {
24+
return GetMessageConfigLogic{
25+
Logger: logx.WithContext(ctx),
26+
ctx: ctx,
27+
svcCtx: svcCtx,
28+
}
29+
}
30+
31+
func (l *GetMessageConfigLogic) GetMessageConfig() (resp *types.MessageConfig, err error) {
32+
resp = &types.MessageConfig{
33+
Comments: true,
34+
Follow: true,
35+
GoodAndStar: true,
36+
MailNotice: types.MailNotice{
37+
Comments: true,
38+
Follow: true,
39+
GoodAndStar: true,
40+
},
41+
}
42+
userID := l.ctx.Value("ID")
43+
messageConfig, err := global.Mongo.MessageConfigModel.FindOneByUserID(l.ctx, userID.(string))
44+
if messageConfig == nil && err == model.ErrNotFound {
45+
return resp,nil
46+
}
47+
if err != nil {
48+
global.LOG.Error("通过用户id获取消息中心配置失败", zap.Error(err))
49+
return nil, errors.Wrapf(perr.NewErrCode(perr.NotFoundError), "logic.GetMessageConfig not found: %s", userID)
50+
}
51+
return &types.MessageConfig{
52+
Comments: messageConfig.Comments,
53+
Follow: messageConfig.Follow,
54+
GoodAndStar: messageConfig.GoodAndStar,
55+
MailNotice: types.MailNotice{
56+
Comments: messageConfig.MailNotice.Comments,
57+
Follow: messageConfig.MailNotice.Follow,
58+
GoodAndStar: messageConfig.MailNotice.GoodAndStar,
59+
},
60+
}, nil
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

‎api/internal/types/types.go

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api/model/messageconfigmodel.go

+38
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
type MessageConfigModel interface {
1111
Insert(ctx context.Context, data *MessageConfig) error
1212
FindOne(ctx context.Context, id string) (*MessageConfig, error)
13+
FindOneByUserID(ctx context.Context, userID string) (*MessageConfig, error)
1314
Update(ctx context.Context, data *MessageConfig) error
15+
UpdateByUserID(ctx context.Context, data *MessageConfig) error
1416
Delete(ctx context.Context, id string) error
1517
}
1618

@@ -62,6 +64,30 @@ func (m *defaultMessageConfigModel) FindOne(ctx context.Context, id string) (*Me
6264
}
6365
}
6466

67+
func (m *defaultMessageConfigModel) FindOneByUserID(ctx context.Context, userID string) (*MessageConfig, error) {
68+
if !bson.IsObjectIdHex(userID) {
69+
return nil, ErrInvalidObjectId
70+
}
71+
72+
session, err := m.TakeSession()
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
defer m.PutSession(session)
78+
var data MessageConfig
79+
80+
err = m.GetCollection(session).Find(bson.M{"user_id": bson.ObjectIdHex(userID)}).One(&data)
81+
switch err {
82+
case nil:
83+
return &data, nil
84+
case mongo.ErrNotFound:
85+
return nil, ErrNotFound
86+
default:
87+
return nil, err
88+
}
89+
}
90+
6591
func (m *defaultMessageConfigModel) Update(ctx context.Context, data *MessageConfig) error {
6692
session, err := m.TakeSession()
6793
if err != nil {
@@ -73,6 +99,18 @@ func (m *defaultMessageConfigModel) Update(ctx context.Context, data *MessageCon
7399
return m.GetCollection(session).UpdateId(data.ID, data)
74100
}
75101

102+
func (m *defaultMessageConfigModel) UpdateByUserID(ctx context.Context, data *MessageConfig) error {
103+
session, err := m.TakeSession()
104+
if err != nil {
105+
return err
106+
}
107+
108+
defer m.PutSession(session)
109+
110+
return m.GetCollection(session).Update(bson.M{"user_id": bson.ObjectIdHex(data.UserID.Hex())}, data)
111+
}
112+
113+
76114
func (m *defaultMessageConfigModel) Delete(ctx context.Context, id string) error {
77115
session, err := m.TakeSession()
78116
if err != nil {

0 commit comments

Comments
 (0)
Please sign in to comment.