73 lines
2.2 KiB
Go
73 lines
2.2 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"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type BookCommentService struct{}
|
|
|
|
const bookCommentDisplaySelect = "bc.*, b.title AS book_title, ch.title AS chapter_title"
|
|
|
|
func (s *BookCommentService) CreateBookComment(item book.BookComment) error {
|
|
if err := validateBookComment(item); err != nil {
|
|
return err
|
|
}
|
|
return global.GVA_DB.Create(&item).Error
|
|
}
|
|
|
|
func (s *BookCommentService) DeleteBookComment(item book.BookComment) error {
|
|
return global.GVA_DB.Delete(&item).Error
|
|
}
|
|
|
|
func (s *BookCommentService) DeleteBookCommentByIds(ids commonReq.IdsReq) error {
|
|
return deleteByIDs[book.BookComment](ids.Ids)
|
|
}
|
|
|
|
func (s *BookCommentService) UpdateBookComment(item book.BookComment) error {
|
|
if err := validateBookComment(item); err != nil {
|
|
return err
|
|
}
|
|
return global.GVA_DB.Save(&item).Error
|
|
}
|
|
|
|
func (s *BookCommentService) GetBookComment(id uint) (item bookRes.BookCommentDisplay, err error) {
|
|
err = bookCommentDisplayDB().
|
|
Select(bookCommentDisplaySelect).
|
|
Where("bc.id = ?", id).
|
|
Take(&item).Error
|
|
return
|
|
}
|
|
|
|
func (s *BookCommentService) GetBookCommentInfoList(info bookReq.BookCommentSearch) (list []bookRes.BookCommentListItem, total int64, err error) {
|
|
db := bookCommentDisplayDB()
|
|
if info.MemberUserID != nil {
|
|
db = db.Where("bc.member_user_id = ?", *info.MemberUserID)
|
|
}
|
|
if info.BookID != nil {
|
|
db = db.Where("bc.book_id = ?", *info.BookID)
|
|
}
|
|
if info.ChapterID != nil {
|
|
db = db.Where("bc.chapter_id = ?", *info.ChapterID)
|
|
}
|
|
if info.CommentStatus != "" {
|
|
db = db.Where("bc.comment_status = ?", info.CommentStatus)
|
|
}
|
|
err = db.Count(&total).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = db.Select(bookCommentDisplaySelect).Scopes(paginate(info.PageInfo)).Order("bc.id desc").Scan(&list).Error
|
|
return
|
|
}
|
|
|
|
func bookCommentDisplayDB() *gorm.DB {
|
|
return global.GVA_DB.Table("book_comment AS bc").
|
|
Joins("LEFT JOIN book AS b ON b.id = bc.book_id").
|
|
Joins("LEFT JOIN book_chapter AS ch ON ch.id = bc.chapter_id")
|
|
}
|