From 019791348ba06f3171117eea523600720c4a8ad0 Mon Sep 17 00:00:00 2001 From: Deng Ming Date: Thu, 20 Jun 2024 20:11:01 +0800 Subject: [PATCH] =?UTF-8?q?project=20=E5=90=8E=E5=8F=B0=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integration/admin_project_test.go | 61 +++++++++++++++++++ internal/project/internal/repository/admin.go | 5 ++ .../project/internal/repository/dao/admin.go | 6 ++ internal/project/internal/service/admin.go | 5 ++ internal/project/internal/web/admin.go | 9 +++ 5 files changed, 86 insertions(+) diff --git a/internal/project/internal/integration/admin_project_test.go b/internal/project/internal/integration/admin_project_test.go index 428d8680..4837d8ed 100644 --- a/internal/project/internal/integration/admin_project_test.go +++ b/internal/project/internal/integration/admin_project_test.go @@ -23,6 +23,8 @@ import ( "testing" "time" + "gorm.io/gorm" + "github.com/ecodeclub/webook/internal/permission" "github.com/ecodeclub/webook/internal/interactive" @@ -557,6 +559,65 @@ func (s *AdminProjectTestSuite) TestProjectPublish() { } } +func (s *AdminProjectTestSuite) TestProjectDelete() { + testCases := []struct { + name string + + before func(t *testing.T) + after func(t *testing.T) + + req web.IdReq + wantCode int + wantResp test.Result[any] + }{ + { + name: "删除成功", + before: func(t *testing.T) { + err := s.db.Create(&dao.Project{Id: 11}).Error + require.NoError(t, err) + }, + after: func(t *testing.T) { + var prj dao.Project + err := s.db.Where("id = ?", 11).First(&prj).Error + require.Equal(t, gorm.ErrRecordNotFound, err) + }, + req: web.IdReq{ + Id: 11, + }, + wantCode: 200, + wantResp: test.Result[any]{Msg: "OK"}, + }, + { + name: "id不存在", + before: func(t *testing.T) { + }, + after: func(t *testing.T) { + }, + req: web.IdReq{ + Id: 12, + }, + wantCode: 200, + wantResp: test.Result[any]{Msg: "OK"}, + }, + } + + for _, tc := range testCases { + tc := tc + s.T().Run(tc.name, func(t *testing.T) { + tc.before(t) + req, err := http.NewRequest(http.MethodPost, + "/project/delete", iox.NewJSONReader(tc.req)) + req.Header.Set("content-type", "application/json") + require.NoError(t, err) + recorder := test.NewJSONResponseRecorder[any]() + s.server.ServeHTTP(recorder, req) + require.Equal(t, tc.wantCode, recorder.Code) + assert.Equal(t, tc.wantResp, recorder.MustScan()) + tc.after(t) + }) + } +} + func (s *AdminProjectTestSuite) TestProjectList() { prjs := make([]dao.Project, 0, 10) for i := 0; i < 10; i++ { diff --git a/internal/project/internal/repository/admin.go b/internal/project/internal/repository/admin.go index 7f9cff79..856703ea 100644 --- a/internal/project/internal/repository/admin.go +++ b/internal/project/internal/repository/admin.go @@ -51,6 +51,7 @@ type ProjectAdminRepository interface { ComboSave(ctx context.Context, pid int64, c domain.Combo) (int64, error) ComboDetail(ctx context.Context, cid int64) (domain.Combo, error) ComboSync(ctx context.Context, pid int64, c domain.Combo) (int64, error) + Delete(ctx context.Context, id int64) error } var _ ProjectAdminRepository = (*projectAdminRepository)(nil) @@ -59,6 +60,10 @@ type projectAdminRepository struct { dao dao.ProjectAdminDAO } +func (repo *projectAdminRepository) Delete(ctx context.Context, id int64) error { + return repo.dao.Delete(ctx, id) +} + func (repo *projectAdminRepository) ComboSync(ctx context.Context, pid int64, c domain.Combo) (int64, error) { entity := repo.comboToEntity(c) entity.Pid = pid diff --git a/internal/project/internal/repository/dao/admin.go b/internal/project/internal/repository/dao/admin.go index c0260a83..285c4baa 100644 --- a/internal/project/internal/repository/dao/admin.go +++ b/internal/project/internal/repository/dao/admin.go @@ -54,6 +54,7 @@ type ProjectAdminDAO interface { ComboById(ctx context.Context, cid int64) (ProjectCombo, error) ComboSync(ctx context.Context, c ProjectCombo) (int64, error) Combos(ctx context.Context, pid int64) ([]ProjectCombo, error) + Delete(ctx context.Context, id int64) error } var _ ProjectAdminDAO = &GORMProjectAdminDAO{} @@ -63,6 +64,11 @@ type GORMProjectAdminDAO struct { prjUpdateColumns []string } +func (dao *GORMProjectAdminDAO) Delete(ctx context.Context, id int64) error { + // 只需要删除 project 本体就可以了。别的数据也无法查询到了 + return dao.db.WithContext(ctx).Model(&Project{}).Delete("id = ?", id).Error +} + func (dao *GORMProjectAdminDAO) Combos(ctx context.Context, pid int64) ([]ProjectCombo, error) { var res []ProjectCombo err := dao.db.WithContext(ctx).Where("pid = ?", pid).Find(&res).Error diff --git a/internal/project/internal/service/admin.go b/internal/project/internal/service/admin.go index affed27e..8d2f7eb3 100644 --- a/internal/project/internal/service/admin.go +++ b/internal/project/internal/service/admin.go @@ -54,6 +54,7 @@ type ProjectAdminService interface { ComboSave(ctx context.Context, pid int64, c domain.Combo) (int64, error) ComboDetail(ctx context.Context, cid int64) (domain.Combo, error) ComboPublish(ctx context.Context, pid int64, c domain.Combo) (int64, error) + Delete(ctx context.Context, id int64) error } var _ ProjectAdminService = (*projectAdminService)(nil) @@ -65,6 +66,10 @@ type projectAdminService struct { logger *elog.Component } +func (svc *projectAdminService) Delete(ctx context.Context, id int64) error { + return svc.adminRepo.Delete(ctx, id) +} + func (svc *projectAdminService) ComboPublish(ctx context.Context, pid int64, c domain.Combo) (int64, error) { c.Status = domain.ComboStatusPublished id, err := svc.adminRepo.ComboSync(ctx, pid, c) diff --git a/internal/project/internal/web/admin.go b/internal/project/internal/web/admin.go index 761e9044..ca5dc251 100644 --- a/internal/project/internal/web/admin.go +++ b/internal/project/internal/web/admin.go @@ -34,6 +34,7 @@ func (h *AdminHandler) PrivateRoutes(server *gin.Engine) { g.POST("/detail", ginx.B[IdReq](h.Detail)) g.POST("/save", ginx.B[Project](h.Save)) g.POST("/publish", ginx.B[Project](h.Publish)) + g.POST("/delete", ginx.B[IdReq](h.Delete)) g.POST("/difficulty/save", ginx.B(h.DifficultySave)) g.POST("/difficulty/detail", ginx.B(h.DifficultyDetail)) @@ -275,6 +276,14 @@ func (h *AdminHandler) ComboPublish(ctx *ginx.Context, req ComboSaveReq) (ginx.R }, nil } +func (h *AdminHandler) Delete(ctx *ginx.Context, req IdReq) (ginx.Result, error) { + err := h.svc.Delete(ctx, req.Id) + if err != nil { + return systemErrorResult, err + } + return ginx.Result{Msg: "OK"}, nil +} + func NewAdminHandler(svc service.ProjectAdminService) *AdminHandler { return &AdminHandler{ svc: svc,