初始化项目框架,完成部分接口开发
This commit is contained in:
98
internal/logic/chapter/chapter.go
Normal file
98
internal/logic/chapter/chapter.go
Normal file
@ -0,0 +1,98 @@
|
||||
package chapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
)
|
||||
|
||||
type sChapter struct{}
|
||||
|
||||
func New() service.IChapter {
|
||||
return &sChapter{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterChapter(New())
|
||||
}
|
||||
|
||||
// List retrieves a paginated list of chapters
|
||||
func (s *sChapter) List(ctx context.Context, in *model.ChapterListIn) (out *model.ChapterListOut, err error) {
|
||||
out = &model.ChapterListOut{}
|
||||
m := dao.Chapters.Ctx(ctx)
|
||||
if in.BookId != 0 {
|
||||
m = m.Where(dao.Chapters.Columns().BookId, in.BookId)
|
||||
}
|
||||
if in.Title != "" {
|
||||
m = m.Where(dao.Chapters.Columns().Title+" like ?", "%"+in.Title+"%")
|
||||
}
|
||||
if in.IsLocked != 0 {
|
||||
m = m.Where(dao.Chapters.Columns().IsLocked, in.IsLocked)
|
||||
}
|
||||
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sChapter) Create(ctx context.Context, in *model.ChapterAddIn) (out *model.ChapterCRUDOut, err error) {
|
||||
if _, err := dao.Chapters.Ctx(ctx).Data(do.Chapters{
|
||||
BookId: in.BookId,
|
||||
Title: in.Title,
|
||||
Content: in.Content,
|
||||
WordCount: in.WordCount,
|
||||
Sort: in.Sort,
|
||||
IsLocked: in.IsLocked,
|
||||
RequiredScore: in.RequiredScore,
|
||||
}).Insert(); err != nil {
|
||||
return nil, ecode.Fail.Sub("chapter_create_failed")
|
||||
}
|
||||
return &model.ChapterCRUDOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sChapter) Update(ctx context.Context, in *model.ChapterEditIn) (out *model.ChapterCRUDOut, err error) {
|
||||
exist, err := dao.Chapters.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("chapter_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("chapter_not_found")
|
||||
}
|
||||
_, err = dao.Chapters.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Data(do.Chapters{
|
||||
BookId: in.BookId,
|
||||
Title: in.Title,
|
||||
Content: in.Content,
|
||||
WordCount: in.WordCount,
|
||||
Sort: in.Sort,
|
||||
IsLocked: in.IsLocked,
|
||||
RequiredScore: in.RequiredScore,
|
||||
}).Update()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("chapter_update_failed")
|
||||
}
|
||||
return &model.ChapterCRUDOut{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *sChapter) Delete(ctx context.Context, in *model.ChapterDelIn) (out *model.ChapterCRUDOut, err error) {
|
||||
exist, err := dao.Chapters.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("chapter_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("chapter_not_found")
|
||||
}
|
||||
_, err = dao.Chapters.Ctx(ctx).WherePri(in.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("chapter_delete_failed")
|
||||
}
|
||||
return &model.ChapterCRUDOut{Success: true}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user