Skip to content

Commit 17289de

Browse files
authored
feat(feedback): add GetFeedback and CreateFeedback service (#339)
1 parent e395834 commit 17289de

41 files changed

Lines changed: 11979 additions & 2626 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ OUTPUT_PATH = $(DIR)/output
2727
API_PATH= $(DIR)/cmd/api
2828

2929
# 服务名
30-
SERVICES := api user classroom course launch_screen paper academic version common
30+
SERVICES := api user classroom course launch_screen paper academic version common oa
3131
service = $(word 1, $@)
3232

3333
PREFIX = "[Makefile]"
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2024 The west2-online Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package api
18+
19+
import (
20+
"context"
21+
22+
"github.com/cloudwego/hertz/pkg/app"
23+
24+
api "github.com/west2-online/fzuhelper-server/api/model/api"
25+
"github.com/west2-online/fzuhelper-server/api/pack"
26+
"github.com/west2-online/fzuhelper-server/api/rpc"
27+
oa "github.com/west2-online/fzuhelper-server/kitex_gen/oa"
28+
"github.com/west2-online/fzuhelper-server/pkg/errno"
29+
)
30+
31+
// CreateFeedback .
32+
// @router /api/v1/feedback/create [POST]
33+
func CreateFeedback(ctx context.Context, c *app.RequestContext) {
34+
var req api.CreateFeedbackRequest
35+
if err := c.BindAndValidate(&req); err != nil {
36+
pack.RespError(c, errno.ParamError.WithError(err))
37+
return
38+
}
39+
40+
resp := new(api.CreateFeedbackResponse)
41+
err := rpc.CreateFeedbackRPC(ctx, &oa.CreateFeedbackRequest{
42+
ReportId: req.GetReportID(),
43+
StuId: req.GetStuID(),
44+
Name: req.GetName(),
45+
College: req.GetCollege(),
46+
ContactPhone: req.GetContactPhone(),
47+
ContactQq: req.GetContactQq(),
48+
ContactEmail: req.GetContactEmail(),
49+
NetworkEnv: req.GetNetworkEnv(),
50+
IsOnCampus: req.GetIsOnCampus(),
51+
OsName: req.GetOsName(),
52+
OsVersion: req.GetOsVersion(),
53+
Manufacturer: req.GetManufacturer(),
54+
DeviceModel: req.GetDeviceModel(),
55+
ProblemDesc: req.GetProblemDesc(),
56+
Screenshots: req.GetScreenshots(),
57+
AppVersion: req.GetAppVersion(),
58+
VersionHistory: req.GetVersionHistory(),
59+
NetworkTraces: req.GetNetworkTraces(),
60+
Events: req.GetEvents(),
61+
UserSettings: req.GetUserSettings(),
62+
})
63+
if err != nil {
64+
pack.RespError(c, err)
65+
return
66+
}
67+
pack.RespData(c, resp)
68+
}
69+
70+
// GetFeedback .
71+
// @router /api/v1/feedback/get [POST]
72+
func GetFeedback(ctx context.Context, c *app.RequestContext) {
73+
var req api.GetFeedbackRequest
74+
if err := c.BindAndValidate(&req); err != nil {
75+
pack.RespError(c, errno.ParamError.WithError(err))
76+
return
77+
}
78+
79+
resp, err := rpc.GetFeedbackRPC(ctx, &oa.GetFeedbackRequest{ReportId: req.ReportID})
80+
if err != nil {
81+
pack.RespError(c, err)
82+
return
83+
}
84+
pack.RespData(c, resp)
85+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
Copyright 2024 The west2-online Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package api
18+
19+
import (
20+
"bytes"
21+
"context"
22+
"testing"
23+
24+
"github.com/bytedance/mockey"
25+
"github.com/cloudwego/hertz/pkg/common/config"
26+
"github.com/cloudwego/hertz/pkg/common/ut"
27+
"github.com/cloudwego/hertz/pkg/protocol/consts"
28+
"github.com/cloudwego/hertz/pkg/route"
29+
"github.com/stretchr/testify/assert"
30+
31+
"github.com/west2-online/fzuhelper-server/api/rpc"
32+
oa "github.com/west2-online/fzuhelper-server/kitex_gen/oa"
33+
"github.com/west2-online/fzuhelper-server/pkg/errno"
34+
)
35+
36+
func TestCreateFeedback(t *testing.T) {
37+
type testCase struct {
38+
name string
39+
body string
40+
mockRPCError error
41+
expectingError bool
42+
expectingMsg string
43+
url string
44+
}
45+
46+
okBody := `{
47+
"reportId": 1,
48+
"stuId": "102301000",
49+
"name": "张三",
50+
"college": "计算机与大数据学院",
51+
"contactPhone": "13800000000",
52+
"contactQQ": "10001",
53+
"contactEmail": "a@b.com",
54+
"networkEnv": "wifi",
55+
"isOnCampus": true,
56+
"osName": "Android",
57+
"osVersion": "14",
58+
"manufacturer": "Xiaomi",
59+
"deviceModel": "Mi 14",
60+
"problemDesc": "登录白屏",
61+
"screenshots": "[]",
62+
"appVersion": "1.2.3",
63+
"versionHistory": "[]",
64+
"networkTraces": "[]",
65+
"events": "[]",
66+
"userSettings": "{}"
67+
}`
68+
69+
testCases := []testCase{
70+
{
71+
name: "success",
72+
body: okBody,
73+
mockRPCError: nil,
74+
expectingError: false,
75+
expectingMsg: `{"code":"10000","message":`,
76+
url: "/api/v1/feedback/create",
77+
},
78+
{
79+
name: "invalid json",
80+
body: `{"reportId": 1,`, // 非法 JSON
81+
mockRPCError: nil,
82+
expectingError: true,
83+
expectingMsg: `{"code":"20001","message":`,
84+
url: "/api/v1/feedback/create",
85+
},
86+
{
87+
name: "rpc error",
88+
body: okBody,
89+
mockRPCError: errno.InternalServiceError,
90+
expectingError: true,
91+
expectingMsg: `{"code":"50001","message":`,
92+
url: "/api/v1/feedback/create",
93+
},
94+
}
95+
96+
router := route.NewEngine(&config.Options{})
97+
router.POST("/api/v1/feedback/create", CreateFeedback)
98+
99+
defer mockey.UnPatchAll()
100+
for _, tc := range testCases {
101+
mockey.PatchConvey(tc.name, t, func() {
102+
mockey.Mock(rpc.CreateFeedbackRPC).To(func(ctx context.Context, req *oa.CreateFeedbackRequest) error {
103+
return tc.mockRPCError
104+
}).Build()
105+
106+
result := ut.PerformRequest(router, "POST", tc.url,
107+
&ut.Body{Body: bytes.NewBufferString(tc.body), Len: len(tc.body)},
108+
ut.Header{Key: "Content-Type", Value: "application/json"})
109+
assert.Equal(t, consts.StatusOK, result.Result().StatusCode())
110+
assert.Contains(t, string(result.Result().Body()), tc.expectingMsg)
111+
})
112+
}
113+
}

0 commit comments

Comments
 (0)