Skip to content

Commit

Permalink
update: delete subject template group when delete subject
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu327 committed Dec 26, 2023
1 parent cceddf1 commit be4bc4a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
14 changes: 14 additions & 0 deletions pkg/database/dao/mock/subject_template_group.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion pkg/database/dao/subject_template_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type SubjectTemplateGroup struct {

type SubjectTemplateGroupManager interface {
GetTemplateGroupMemberCount(groupPK, templateID int64) (int64, error)
GetMaxExpiredAtBySubjectGroup(subjectPK, groupPK int64, excludeTemplateID int64) (int64, error)
ListPagingTemplateGroupMember(
groupPK, templateID int64,
limit, offset int64,
Expand All @@ -46,7 +47,7 @@ type SubjectTemplateGroupManager interface {
BulkUpdateExpiredAtWithTx(tx *sqlx.Tx, relations []SubjectTemplateGroup) error
BulkUpdateExpiredAtByRelationWithTx(tx *sqlx.Tx, relations []SubjectRelation) error
BulkDeleteWithTx(tx *sqlx.Tx, relations []SubjectTemplateGroup) error
GetMaxExpiredAtBySubjectGroup(subjectPK, groupPK int64, excludeTemplateID int64) (int64, error)
BulkDeleteBySubjectPKs(tx *sqlx.Tx, subjectPKs []int64) error
}

type subjectTemplateGroupManager struct {
Expand Down Expand Up @@ -235,3 +236,9 @@ func (m *subjectTemplateGroupManager) ListThinRelationWithMaxExpiredAtByGroupPK(

return relations, err
}

func (m *subjectTemplateGroupManager) BulkDeleteBySubjectPKs(tx *sqlx.Tx, subjectPKs []int64) error {
sql := `DELETE FROM subject_template_group
WHERE subject_pk in (?)`
return database.SqlxDeleteWithTx(tx, sql, subjectPKs)
}
10 changes: 10 additions & 0 deletions pkg/service/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,16 @@ func (l *groupService) BulkDeleteBySubjectPKsWithTx(tx *sqlx.Tx, subjectPKs []in
err, "manager.BulkDeleteBySubjectPKs subject_pks=`%+v` fail", subjectPKs)
}

// 批量删除subject template group
err = l.subjectTemplateGroupManager.BulkDeleteBySubjectPKs(tx, subjectPKs)
if err != nil {
return errorWrapf(
err,
"subjectTemplateGroupManager.BulkDeleteBySubjectPKsWithTx subjectPKs=`%+v` fail",
subjectPKs,
)
}

// 批量删除用户的subject system group
err = l.subjectSystemGroupManager.DeleteBySubjectPKsWithTx(tx, subjectPKs)
if err != nil {
Expand Down
42 changes: 38 additions & 4 deletions pkg/service/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,49 @@ var _ = Describe("GroupService", func() {
assert.Contains(GinkgoT(), err.Error(), "BulkDeleteBySubjectPKs")
})

It("manager.BulkDeleteBySubjectPKs fail", func() {
mockSubjectService := mock.NewMockSubjectGroupManager(ctl)

mockSubjectService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return(
nil,
).AnyTimes()

mockSubjectTemplateGroupService := mock.NewMockSubjectTemplateGroupManager(ctl)
mockSubjectTemplateGroupService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return(
errors.New("error"),
).AnyTimes()

manager := &groupService{
manager: mockSubjectService,
subjectTemplateGroupManager: mockSubjectTemplateGroupService,
}

err := manager.BulkDeleteBySubjectPKsWithTx(nil, []int64{1, 2})
assert.Error(GinkgoT(), err)
assert.Contains(GinkgoT(), err.Error(), "subjectTemplateGroupManager.BulkDeleteBySubjectPKsWithTx")
})

It("subjectSystemGroupManager.DeleteBySubjectPKsWithTx fail", func() {
mockSubjectService := mock.NewMockSubjectGroupManager(ctl)

mockSubjectService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return(
nil,
).AnyTimes()

mockSubjectTemplateGroupService := mock.NewMockSubjectTemplateGroupManager(ctl)
mockSubjectTemplateGroupService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return(
nil,
).AnyTimes()

mockSubjectSystemGroupService := mock.NewMockSubjectSystemGroupManager(ctl)
mockSubjectSystemGroupService.EXPECT().DeleteBySubjectPKsWithTx(gomock.Any(), []int64{1, 2}).Return(
errors.New("error"),
).AnyTimes()

manager := &groupService{
manager: mockSubjectService,
subjectSystemGroupManager: mockSubjectSystemGroupService,
manager: mockSubjectService,
subjectSystemGroupManager: mockSubjectSystemGroupService,
subjectTemplateGroupManager: mockSubjectTemplateGroupService,
}

err := manager.BulkDeleteBySubjectPKsWithTx(nil, []int64{1, 2})
Expand All @@ -123,14 +151,20 @@ var _ = Describe("GroupService", func() {
nil,
).AnyTimes()

mockSubjectTemplateGroupService := mock.NewMockSubjectTemplateGroupManager(ctl)
mockSubjectTemplateGroupService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return(
nil,
).AnyTimes()

mockSubjectSystemGroupService := mock.NewMockSubjectSystemGroupManager(ctl)
mockSubjectSystemGroupService.EXPECT().DeleteBySubjectPKsWithTx(gomock.Any(), []int64{1, 2}).Return(
nil,
).AnyTimes()

manager := &groupService{
manager: mockSubjectService,
subjectSystemGroupManager: mockSubjectSystemGroupService,
manager: mockSubjectService,
subjectSystemGroupManager: mockSubjectSystemGroupService,
subjectTemplateGroupManager: mockSubjectTemplateGroupService,
}

err := manager.BulkDeleteBySubjectPKsWithTx(nil, []int64{1, 2})
Expand Down

0 comments on commit be4bc4a

Please sign in to comment.