完善功能
This commit is contained in:
@ -7,6 +7,8 @@ import (
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
type sCategory struct {
|
||||
@ -24,8 +26,8 @@ func init() {
|
||||
func (s *sCategory) List(ctx context.Context, in *model.CategoryListIn) (out *model.CategoryListOut, err error) {
|
||||
out = &model.CategoryListOut{}
|
||||
m := dao.Categories.Ctx(ctx)
|
||||
if in.Type != 0 {
|
||||
m = m.Where(dao.Categories.Columns().Type, in.Type)
|
||||
if in.Channel != 0 {
|
||||
m = m.Where(dao.Categories.Columns().Channel, in.Channel)
|
||||
}
|
||||
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
|
||||
return
|
||||
@ -34,9 +36,6 @@ func (s *sCategory) List(ctx context.Context, in *model.CategoryListIn) (out *mo
|
||||
}
|
||||
|
||||
func (s *sCategory) Create(ctx context.Context, in *model.CategoryAddIn) (out *model.CategoryCRUDOut, err error) {
|
||||
if in.Type != 1 && in.Type != 2 {
|
||||
return nil, ecode.Params.Sub("category_type_invalid")
|
||||
}
|
||||
exist, err := dao.Categories.Ctx(ctx).
|
||||
Where(dao.Categories.Columns().Name, in.Name).
|
||||
Exist()
|
||||
@ -48,8 +47,8 @@ func (s *sCategory) Create(ctx context.Context, in *model.CategoryAddIn) (out *m
|
||||
}
|
||||
|
||||
if _, err := dao.Categories.Ctx(ctx).Data(do.Categories{
|
||||
Name: in.Name,
|
||||
Type: in.Type,
|
||||
Name: in.Name,
|
||||
Channel: in.Channel,
|
||||
}).Insert(); err != nil {
|
||||
return nil, ecode.Fail.Sub("category_create_failed")
|
||||
}
|
||||
@ -60,9 +59,6 @@ func (s *sCategory) Create(ctx context.Context, in *model.CategoryAddIn) (out *m
|
||||
}
|
||||
|
||||
func (s *sCategory) Update(ctx context.Context, in *model.CategoryEditIn) (out *model.CategoryCRUDOut, err error) {
|
||||
if in.Type != 1 && in.Type != 2 {
|
||||
return nil, ecode.Params.Sub("category_type_invalid")
|
||||
}
|
||||
exist, err := dao.Categories.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
@ -88,8 +84,8 @@ func (s *sCategory) Update(ctx context.Context, in *model.CategoryEditIn) (out *
|
||||
_, err = dao.Categories.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Data(do.Categories{
|
||||
Name: in.Name,
|
||||
Type: in.Type,
|
||||
Name: in.Name,
|
||||
Channel: in.Channel,
|
||||
}).Update()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("category_update_failed")
|
||||
@ -111,10 +107,31 @@ func (s *sCategory) Delete(ctx context.Context, in *model.CategoryDelIn) (out *m
|
||||
return nil, ecode.NotFound.Sub("category_not_found")
|
||||
}
|
||||
|
||||
// Soft delete category
|
||||
_, err = dao.Categories.Ctx(ctx).WherePri(in.Id).Delete()
|
||||
// 开启事务,检查是否有书籍使用了这个分类
|
||||
err = dao.Categories.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
// 检查是否有书籍使用了这个分类
|
||||
bookCount, err := dao.Books.Ctx(ctx).TX(tx).
|
||||
Where(dao.Books.Columns().CategoryId, in.Id).
|
||||
Count()
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("book_query_failed")
|
||||
}
|
||||
|
||||
if bookCount > 0 {
|
||||
return ecode.Fail.Sub("category_in_use")
|
||||
}
|
||||
|
||||
// 删除分类
|
||||
_, err = dao.Categories.Ctx(ctx).TX(tx).WherePri(in.Id).Delete()
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("category_delete_failed")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("category_delete_failed")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &model.CategoryCRUDOut{
|
||||
|
||||
Reference in New Issue
Block a user