139 lines
3.2 KiB
Go
139 lines
3.2 KiB
Go
package book
|
|
|
|
import (
|
|
"context"
|
|
"server/internal/dao"
|
|
"server/internal/model"
|
|
"server/internal/model/do"
|
|
"server/internal/service"
|
|
"server/utility/ecode"
|
|
)
|
|
|
|
type sBook struct{}
|
|
|
|
func New() service.IBook {
|
|
return &sBook{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterBook(New())
|
|
}
|
|
|
|
// List retrieves a paginated list of books
|
|
func (s *sBook) List(ctx context.Context, in *model.BookListIn) (out *model.BookListOut, err error) {
|
|
out = &model.BookListOut{}
|
|
m := dao.Books.Ctx(ctx)
|
|
if in.Title != "" {
|
|
m = m.Where(dao.Books.Columns().Title+" like ?", "%"+in.Title+"%")
|
|
}
|
|
if in.CategoryId != 0 {
|
|
m = m.Where(dao.Books.Columns().CategoryId, in.CategoryId)
|
|
}
|
|
if in.AuthorId != 0 {
|
|
m = m.Where(dao.Books.Columns().AuthorId, in.AuthorId)
|
|
}
|
|
if in.Status != 0 {
|
|
m = m.Where(dao.Books.Columns().Status, in.Status)
|
|
}
|
|
if in.IsRecommended != 0 {
|
|
m = m.Where(dao.Books.Columns().IsRecommended, in.IsRecommended)
|
|
}
|
|
if err = m.Page(in.Page, in.Size).ScanAndCount(&out.List, &out.Total, false); err != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (s *sBook) Create(ctx context.Context, in *model.BookAddIn) (out *model.BookCRUDOut, err error) {
|
|
exist, err := dao.Books.Ctx(ctx).
|
|
Where(dao.Books.Columns().Title, in.Title).
|
|
Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("book_query_failed")
|
|
}
|
|
if exist {
|
|
return nil, ecode.Params.Sub("book_exists")
|
|
}
|
|
|
|
if _, err := dao.Books.Ctx(ctx).Data(do.Books{
|
|
AuthorId: in.AuthorId,
|
|
CategoryId: in.CategoryId,
|
|
Title: in.Title,
|
|
CoverUrl: in.CoverUrl,
|
|
Description: in.Description,
|
|
Status: in.Status,
|
|
Tags: in.Tags,
|
|
IsRecommended: in.IsRecommended,
|
|
}).Insert(); err != nil {
|
|
return nil, ecode.Fail.Sub("book_create_failed")
|
|
}
|
|
|
|
return &model.BookCRUDOut{
|
|
Success: true,
|
|
}, nil
|
|
}
|
|
|
|
func (s *sBook) Update(ctx context.Context, in *model.BookEditIn) (out *model.BookCRUDOut, err error) {
|
|
exist, err := dao.Books.Ctx(ctx).
|
|
WherePri(in.Id).
|
|
Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("book_query_failed")
|
|
}
|
|
if !exist {
|
|
return nil, ecode.NotFound.Sub("book_not_found")
|
|
}
|
|
|
|
exist, err = dao.Books.Ctx(ctx).
|
|
Where(dao.Books.Columns().Title, in.Title).
|
|
Where("id != ?", in.Id).
|
|
Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("book_query_failed")
|
|
}
|
|
if exist {
|
|
return nil, ecode.Params.Sub("book_exists")
|
|
}
|
|
|
|
_, err = dao.Books.Ctx(ctx).
|
|
WherePri(in.Id).
|
|
Data(do.Books{
|
|
AuthorId: in.AuthorId,
|
|
CategoryId: in.CategoryId,
|
|
Title: in.Title,
|
|
CoverUrl: in.CoverUrl,
|
|
Description: in.Description,
|
|
Status: in.Status,
|
|
Tags: in.Tags,
|
|
IsRecommended: in.IsRecommended,
|
|
}).Update()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("book_update_failed")
|
|
}
|
|
|
|
return &model.BookCRUDOut{
|
|
Success: true,
|
|
}, nil
|
|
}
|
|
|
|
func (s *sBook) Delete(ctx context.Context, in *model.BookDelIn) (out *model.BookCRUDOut, err error) {
|
|
exist, err := dao.Books.Ctx(ctx).
|
|
WherePri(in.Id).
|
|
Exist()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("book_query_failed")
|
|
}
|
|
if !exist {
|
|
return nil, ecode.NotFound.Sub("book_not_found")
|
|
}
|
|
|
|
_, err = dao.Books.Ctx(ctx).WherePri(in.Id).Delete()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("book_delete_failed")
|
|
}
|
|
|
|
return &model.BookCRUDOut{
|
|
Success: true,
|
|
}, nil
|
|
}
|