58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
package book
|
|
|
|
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"
|
|
)
|
|
|
|
type BookAuthorService struct{}
|
|
|
|
func (s *BookAuthorService) CreateBookAuthor(item book.BookAuthor) error {
|
|
if err := validateBookAuthor(item); err != nil {
|
|
return err
|
|
}
|
|
return global.GVA_DB.Save(&item).Error
|
|
}
|
|
|
|
func (s *BookAuthorService) DeleteBookAuthor(item book.BookAuthor) error {
|
|
return global.GVA_DB.Delete(&item).Error
|
|
}
|
|
|
|
func (s *BookAuthorService) DeleteBookAuthorByIds(ids commonReq.IdsReq) error {
|
|
return deleteByIDs[book.BookAuthor](ids.Ids)
|
|
}
|
|
|
|
func (s *BookAuthorService) UpdateBookAuthor(item book.BookAuthor) error {
|
|
if err := validateBookAuthor(item); err != nil {
|
|
return err
|
|
}
|
|
return global.GVA_DB.Save(&item).Error
|
|
}
|
|
|
|
func (s *BookAuthorService) GetBookAuthor(id uint) (item book.BookAuthor, err error) {
|
|
err = global.GVA_DB.Where("id = ?", id).First(&item).Error
|
|
return
|
|
}
|
|
|
|
func (s *BookAuthorService) GetBookAuthorInfoList(info bookReq.BookAuthorSearch) (list []bookRes.BookAuthorListItem, total int64, err error) {
|
|
db := global.GVA_DB.Model(&book.BookAuthor{})
|
|
if info.Name != "" {
|
|
db = db.Where("name LIKE ?", "%"+info.Name+"%")
|
|
}
|
|
if info.Keyword != "" {
|
|
db = db.Where("name LIKE ?", "%"+info.Keyword+"%")
|
|
}
|
|
if info.IsEnabled != nil {
|
|
db = db.Where("is_enabled = ?", *info.IsEnabled)
|
|
}
|
|
err = db.Count(&total).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = db.Select("book_author.*, book_author.name AS author_name").Scopes(paginate(info.PageInfo)).Order("id desc").Scan(&list).Error
|
|
return
|
|
}
|