Skip to content

Commit

Permalink
project 后台管理支持删除操作
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Jun 20, 2024
1 parent 9720204 commit 0197913
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
61 changes: 61 additions & 0 deletions internal/project/internal/integration/admin_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"testing"
"time"

"gorm.io/gorm"

"github.com/ecodeclub/webook/internal/permission"

"github.com/ecodeclub/webook/internal/interactive"
Expand Down Expand Up @@ -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++ {
Expand Down
5 changes: 5 additions & 0 deletions internal/project/internal/repository/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions internal/project/internal/repository/dao/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions internal/project/internal/service/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions internal/project/internal/web/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0197913

Please sign in to comment.