Skip to content

Commit 62525e4

Browse files
committed
fix: upsert
1 parent 657860c commit 62525e4

7 files changed

Lines changed: 102 additions & 47 deletions

File tree

internal/course/handler.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,15 @@ func (s *CourseServiceImpl) UpsertCustomCourse(ctx context.Context, req *course.
180180
"remark": getStringValue(courseItem.Remark),
181181
}
182182

183-
if err := dbClient.Course.UpdateCustomCourse(ctx, stuId, req.Term, *courseId, updates); err != nil {
183+
rows, err := dbClient.Course.UpdateCustomCourse(ctx, stuId, req.Term, *courseId, updates)
184+
if err != nil {
184185
resp.Base = base.BuildBaseResp(err)
185186
return resp, nil
186187
}
188+
if rows == 0 {
189+
resp.Base = base.BuildBaseResp(errno.CustomCourseNotFoundError)
190+
return resp, nil
191+
}
187192
}
188193

189194
resp.Base = base.BuildSuccessResp()
@@ -203,10 +208,15 @@ func (s *CourseServiceImpl) DeleteCustomCourse(ctx context.Context, req *course.
203208
stuId := metainfoContext.ExtractIDFromLoginData(loginData)
204209
dbClient := s.ClientSet.DBClient
205210

206-
if err := dbClient.Course.DeleteCustomCourse(ctx, stuId, req.Term, req.CourseId); err != nil {
211+
rows, err := dbClient.Course.DeleteCustomCourse(ctx, stuId, req.Term, req.CourseId)
212+
if err != nil {
207213
resp.Base = base.BuildBaseResp(errno.InternalServiceError.WithError(err))
208214
return resp, nil
209215
}
216+
if rows == 0 {
217+
resp.Base = base.BuildBaseResp(errno.CustomCourseNotFoundError)
218+
return resp, nil
219+
}
210220

211221
resp.Base = base.BuildSuccessResp()
212222
return resp, nil

pkg/db/course/delete_custom_course.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222
"github.com/west2-online/fzuhelper-server/pkg/db/model"
2323
)
2424

25-
// DeleteCustomCourse 删除自定义课程(软删除)
26-
func (c *DBCourse) DeleteCustomCourse(ctx context.Context, stuId, term, courseId string) error {
27-
return c.client.WithContext(ctx).
25+
// DeleteCustomCourse 删除自定义课程(软删除),返回受影响的行数
26+
func (c *DBCourse) DeleteCustomCourse(ctx context.Context, stuId, term, courseId string) (int64, error) {
27+
result := c.client.WithContext(ctx).
2828
Where("stu_id = ? AND term = ? AND course_id = ?", stuId, term, courseId).
29-
Delete(&model.UserCustomCourse{}).Error
29+
Delete(&model.UserCustomCourse{})
30+
return result.RowsAffected, result.Error
3031
}

pkg/db/course/delete_custom_course_test.go

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,46 @@ import (
3030

3131
func TestDBCourse_DeleteCustomCourse(t *testing.T) {
3232
type testCase struct {
33-
name string
34-
mockError error
35-
stuId string
36-
term string
37-
courseId string
38-
expectingError bool
33+
name string
34+
mockError error
35+
mockRowsAffected int64
36+
stuId string
37+
term string
38+
courseId string
39+
expectingError bool
40+
expectedRows int64
3941
}
4042

4143
testCases := []testCase{
4244
{
43-
name: "DeleteCustomCourse_Success",
44-
mockError: nil,
45-
stuId: "222200311",
46-
term: "202401",
47-
courseId: "uuid-1",
48-
expectingError: false,
45+
name: "DeleteCustomCourse_Success",
46+
mockError: nil,
47+
mockRowsAffected: 1,
48+
stuId: "222200311",
49+
term: "202401",
50+
courseId: "uuid-1",
51+
expectingError: false,
52+
expectedRows: 1,
4953
},
5054
{
51-
name: "DeleteCustomCourse_DBError",
52-
mockError: fmt.Errorf("db error"),
53-
stuId: "222200311",
54-
term: "202401",
55-
courseId: "uuid-1",
56-
expectingError: true,
55+
name: "DeleteCustomCourse_NotFound",
56+
mockError: nil,
57+
mockRowsAffected: 0,
58+
stuId: "222200311",
59+
term: "202401",
60+
courseId: "not-exist",
61+
expectingError: false,
62+
expectedRows: 0,
63+
},
64+
{
65+
name: "DeleteCustomCourse_DBError",
66+
mockError: fmt.Errorf("db error"),
67+
mockRowsAffected: 0,
68+
stuId: "222200311",
69+
term: "202401",
70+
courseId: "uuid-1",
71+
expectingError: true,
72+
expectedRows: 0,
5773
},
5874
}
5975

@@ -71,20 +87,22 @@ func TestDBCourse_DeleteCustomCourse(t *testing.T) {
7187
return mockGormDB
7288
}).Build()
7389
mockey.Mock((*gorm.DB).Delete).To(func(value interface{}, conds ...interface{}) *gorm.DB {
90+
mockGormDB.RowsAffected = tc.mockRowsAffected
7491
if tc.mockError != nil {
7592
mockGormDB.Error = tc.mockError
7693
return mockGormDB
7794
}
7895
return mockGormDB
7996
}).Build()
8097

81-
err := mockDBCourse.DeleteCustomCourse(context.Background(), tc.stuId, tc.term, tc.courseId)
98+
rows, err := mockDBCourse.DeleteCustomCourse(context.Background(), tc.stuId, tc.term, tc.courseId)
8299

83100
if tc.expectingError {
84101
assert.Error(t, err)
85102
} else {
86103
assert.NoError(t, err)
87104
}
105+
assert.Equal(t, tc.expectedRows, rows)
88106
})
89107
}
90108
}

pkg/db/course/update_custom_course.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ import (
2222
"github.com/west2-online/fzuhelper-server/pkg/db/model"
2323
)
2424

25-
// UpdateCustomCourse 更新自定义课程
26-
func (c *DBCourse) UpdateCustomCourse(ctx context.Context, stuId, term, courseId string, updates map[string]interface{}) error {
27-
return c.client.WithContext(ctx).
25+
// UpdateCustomCourse 更新自定义课程,返回受影响的行数
26+
func (c *DBCourse) UpdateCustomCourse(ctx context.Context, stuId, term, courseId string, updates map[string]interface{}) (int64, error) {
27+
result := c.client.WithContext(ctx).
2828
Model(&model.UserCustomCourse{}).
2929
Where("stu_id = ? AND term = ? AND course_id = ?", stuId, term, courseId).
30-
Updates(updates).Error
30+
Updates(updates)
31+
return result.RowsAffected, result.Error
3132
}

pkg/db/course/update_custom_course_test.go

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,59 @@ import (
3030

3131
func TestDBCourse_UpdateCustomCourse(t *testing.T) {
3232
type testCase struct {
33-
name string
34-
mockError error
35-
stuId string
36-
term string
37-
courseId string
38-
updates map[string]interface{}
39-
expectingError bool
33+
name string
34+
mockError error
35+
mockRowsAffected int64
36+
stuId string
37+
term string
38+
courseId string
39+
updates map[string]interface{}
40+
expectingError bool
41+
expectedRows int64
4042
}
4143

4244
testCases := []testCase{
4345
{
44-
name: "UpdateCustomCourse_Success",
45-
mockError: nil,
46-
stuId: "222200311",
47-
term: "202401",
48-
courseId: "uuid-1",
46+
name: "UpdateCustomCourse_Success",
47+
mockError: nil,
48+
mockRowsAffected: 1,
49+
stuId: "222200311",
50+
term: "202401",
51+
courseId: "uuid-1",
4952
updates: map[string]interface{}{
5053
"name": "自习(更新)",
5154
"location": "图书馆3楼",
5255
"start_class": 3,
5356
"end_class": 4,
5457
},
5558
expectingError: false,
59+
expectedRows: 1,
5660
},
5761
{
58-
name: "UpdateCustomCourse_DBError",
59-
mockError: fmt.Errorf("db error"),
60-
stuId: "222200311",
61-
term: "202401",
62-
courseId: "uuid-1",
62+
name: "UpdateCustomCourse_NotFound",
63+
mockError: nil,
64+
mockRowsAffected: 0,
65+
stuId: "222200311",
66+
term: "202401",
67+
courseId: "not-exist",
68+
updates: map[string]interface{}{
69+
"name": "自习(更新)",
70+
},
71+
expectingError: false,
72+
expectedRows: 0,
73+
},
74+
{
75+
name: "UpdateCustomCourse_DBError",
76+
mockError: fmt.Errorf("db error"),
77+
mockRowsAffected: 0,
78+
stuId: "222200311",
79+
term: "202401",
80+
courseId: "uuid-1",
6381
updates: map[string]interface{}{
6482
"name": "自习(更新)",
6583
},
6684
expectingError: true,
85+
expectedRows: 0,
6786
},
6887
}
6988

@@ -84,20 +103,22 @@ func TestDBCourse_UpdateCustomCourse(t *testing.T) {
84103
return mockGormDB
85104
}).Build()
86105
mockey.Mock((*gorm.DB).Updates).To(func(values interface{}) *gorm.DB {
106+
mockGormDB.RowsAffected = tc.mockRowsAffected
87107
if tc.mockError != nil {
88108
mockGormDB.Error = tc.mockError
89109
return mockGormDB
90110
}
91111
return mockGormDB
92112
}).Build()
93113

94-
err := mockDBCourse.UpdateCustomCourse(context.Background(), tc.stuId, tc.term, tc.courseId, tc.updates)
114+
rows, err := mockDBCourse.UpdateCustomCourse(context.Background(), tc.stuId, tc.term, tc.courseId, tc.updates)
95115

96116
if tc.expectingError {
97117
assert.Error(t, err)
98118
} else {
99119
assert.NoError(t, err)
100120
}
121+
assert.Equal(t, tc.expectedRows, rows)
101122
})
102123
}
103124
}

pkg/errno/code.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const (
5555
BizFileUploadErrorCode = 40006 // 文件上传错误(service 层)
5656
BizJwchCookieExceptionCode = 40007 // jwch cookie异常
5757
BizJwchEvaluationNotFoundCode = 40008 // jwch 未进行评测
58+
BizCustomCourseNotExistCode = 40009 // 自定义课程不存在
5859

5960
InternalServiceErrorCode = 50001 // 未知服务错误
6061
InternalDatabaseErrorCode = 50002 // 数据库错误

pkg/errno/default.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,7 @@ var (
4848

4949
// jwch
5050
EvaluationNotFoundError = NewErrNo(BizJwchEvaluationNotFoundCode, "请先对任课教师进行评价") // jwch 未进行评测
51+
52+
// course
53+
CustomCourseNotFoundError = NewErrNo(BizCustomCourseNotExistCode, "自定义课程不存在")
5154
)

0 commit comments

Comments
 (0)