feat: add book admin display fields
This commit is contained in:
@@ -6,10 +6,13 @@ import (
|
||||
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 BookAuthorRelationService struct{}
|
||||
|
||||
const bookAuthorRelationDisplaySelect = "bar.*, b.title AS book_title, ba.name AS author_name"
|
||||
|
||||
func (s *BookAuthorRelationService) CreateBookAuthorRelation(item book.BookAuthorRelation) error {
|
||||
item = applyBookAuthorRelationDefaults(item)
|
||||
if err := validateBookAuthorRelation(item); err != nil {
|
||||
@@ -33,13 +36,16 @@ func (s *BookAuthorRelationService) UpdateBookAuthorRelation(item book.BookAutho
|
||||
return global.GVA_DB.Save(&item).Error
|
||||
}
|
||||
|
||||
func (s *BookAuthorRelationService) GetBookAuthorRelation(id uint) (item book.BookAuthorRelation, err error) {
|
||||
err = global.GVA_DB.Where("id = ?", id).First(&item).Error
|
||||
func (s *BookAuthorRelationService) GetBookAuthorRelation(id uint) (item bookRes.BookAuthorRelationDisplay, err error) {
|
||||
err = bookAuthorRelationDisplayDB().
|
||||
Select(bookAuthorRelationDisplaySelect).
|
||||
Where("bar.id = ?", id).
|
||||
Take(&item).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (s *BookAuthorRelationService) GetBookAuthorRelationInfoList(info bookReq.BookAuthorRelationSearch) (list []bookRes.BookAuthorRelationListItem, total int64, err error) {
|
||||
db := global.GVA_DB.Table("book_author_relation AS bar")
|
||||
db := bookAuthorRelationDisplayDB()
|
||||
if info.BookID != nil {
|
||||
db = db.Where("bar.book_id = ?", *info.BookID)
|
||||
}
|
||||
@@ -50,10 +56,15 @@ func (s *BookAuthorRelationService) GetBookAuthorRelationInfoList(info bookReq.B
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = db.Select("bar.*, ba.name AS author_name").
|
||||
Joins("LEFT JOIN book_author AS ba ON ba.id = bar.author_id").
|
||||
err = db.Select(bookAuthorRelationDisplaySelect).
|
||||
Scopes(paginate(info.PageInfo)).
|
||||
Order("bar.book_id asc, bar.author_sort asc, bar.id desc").
|
||||
Scan(&list).Error
|
||||
return
|
||||
}
|
||||
|
||||
func bookAuthorRelationDisplayDB() *gorm.DB {
|
||||
return global.GVA_DB.Table("book_author_relation AS bar").
|
||||
Joins("LEFT JOIN book AS b ON b.id = bar.book_id").
|
||||
Joins("LEFT JOIN book_author AS ba ON ba.id = bar.author_id")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user