feat: add book admin display fields
This commit is contained in:
@@ -4,16 +4,20 @@ import (
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/book"
|
||||
bookReq "github.com/flipped-aurora/gin-vue-admin/server/model/book/request"
|
||||
bookRes "github.com/flipped-aurora/gin-vue-admin/server/model/book/response"
|
||||
commonReq "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type BookChapterService struct{}
|
||||
|
||||
const bookChapterDisplaySelect = "bc.*, b.title AS book_title"
|
||||
|
||||
func (s *BookChapterService) CreateBookChapter(item book.BookChapter) error {
|
||||
if err := validateBookChapter(item); err != nil {
|
||||
return err
|
||||
}
|
||||
return global.GVA_DB.Save(&item).Error
|
||||
return createWithExplicitIsEnabled(&item, item.IsEnabled)
|
||||
}
|
||||
|
||||
func (s *BookChapterService) DeleteBookChapter(item book.BookChapter) error {
|
||||
@@ -31,29 +35,37 @@ func (s *BookChapterService) UpdateBookChapter(item book.BookChapter) error {
|
||||
return global.GVA_DB.Save(&item).Error
|
||||
}
|
||||
|
||||
func (s *BookChapterService) GetBookChapter(id uint) (item book.BookChapter, err error) {
|
||||
err = global.GVA_DB.Where("id = ?", id).First(&item).Error
|
||||
func (s *BookChapterService) GetBookChapter(id uint) (item bookRes.BookChapterDisplay, err error) {
|
||||
err = bookChapterDisplayDB().
|
||||
Select(bookChapterDisplaySelect).
|
||||
Where("bc.id = ?", id).
|
||||
Take(&item).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (s *BookChapterService) GetBookChapterInfoList(info bookReq.BookChapterSearch) (list []book.BookChapter, total int64, err error) {
|
||||
db := global.GVA_DB.Model(&book.BookChapter{})
|
||||
func (s *BookChapterService) GetBookChapterInfoList(info bookReq.BookChapterSearch) (list []bookRes.BookChapterListItem, total int64, err error) {
|
||||
db := bookChapterDisplayDB()
|
||||
if info.BookID != nil {
|
||||
db = db.Where("book_id = ?", *info.BookID)
|
||||
db = db.Where("bc.book_id = ?", *info.BookID)
|
||||
}
|
||||
if info.IsReadable != nil {
|
||||
db = db.Where("is_readable = ?", *info.IsReadable)
|
||||
db = db.Where("bc.is_readable = ?", *info.IsReadable)
|
||||
}
|
||||
if info.IsEnabled != nil {
|
||||
db = db.Where("is_enabled = ?", *info.IsEnabled)
|
||||
db = db.Where("bc.is_enabled = ?", *info.IsEnabled)
|
||||
}
|
||||
if info.Keyword != "" {
|
||||
db = db.Where("title LIKE ?", "%"+info.Keyword+"%")
|
||||
db = db.Where("bc.title LIKE ?", "%"+info.Keyword+"%")
|
||||
}
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = db.Scopes(paginate(info.PageInfo)).Order("book_id asc, chapter_no asc").Find(&list).Error
|
||||
err = db.Select(bookChapterDisplaySelect).Scopes(paginate(info.PageInfo)).Order("bc.book_id asc, bc.chapter_no asc").Scan(&list).Error
|
||||
return
|
||||
}
|
||||
|
||||
func bookChapterDisplayDB() *gorm.DB {
|
||||
return global.GVA_DB.Table("book_chapter AS bc").
|
||||
Joins("LEFT JOIN book AS b ON b.id = bc.book_id")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user