书籍模块
This commit is contained in:
@@ -4,6 +4,7 @@ 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"
|
||||
)
|
||||
|
||||
@@ -36,7 +37,7 @@ func (s *BookAuthorService) GetBookAuthor(id uint) (item book.BookAuthor, err er
|
||||
return
|
||||
}
|
||||
|
||||
func (s *BookAuthorService) GetBookAuthorInfoList(info bookReq.BookAuthorSearch) (list []book.BookAuthor, total int64, err error) {
|
||||
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+"%")
|
||||
@@ -51,6 +52,6 @@ func (s *BookAuthorService) GetBookAuthorInfoList(info bookReq.BookAuthorSearch)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = db.Scopes(paginate(info.PageInfo)).Order("id desc").Find(&list).Error
|
||||
err = db.Select("book_author.*, book_author.name AS author_name").Scopes(paginate(info.PageInfo)).Order("id desc").Scan(&list).Error
|
||||
return
|
||||
}
|
||||
|
||||
70
server/service/book/book_author_list_test.go
Normal file
70
server/service/book/book_author_list_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package book
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
commonReq "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func setupBookAuthorListTestDB(t *testing.T) {
|
||||
t.Helper()
|
||||
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&book.BookAuthor{}, &book.BookAuthorRelation{}))
|
||||
|
||||
global.GVA_DB = db
|
||||
t.Cleanup(func() {
|
||||
global.GVA_DB = nil
|
||||
})
|
||||
}
|
||||
|
||||
func TestBookAuthorService_GetBookAuthorInfoListReturnsAuthorName(t *testing.T) {
|
||||
setupBookAuthorListTestDB(t)
|
||||
|
||||
require.NoError(t, global.GVA_DB.Create(&book.BookAuthor{
|
||||
Name: "鲁迅",
|
||||
AuthorStatus: "enabled",
|
||||
}).Error)
|
||||
|
||||
list, total, err := (&BookAuthorService{}).GetBookAuthorInfoList(bookReq.BookAuthorSearch{
|
||||
PageInfo: commonReq.PageInfo{Page: 1, PageSize: 10},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 1, total)
|
||||
require.Len(t, list, 1)
|
||||
require.Equal(t, "鲁迅", list[0].Name)
|
||||
require.Equal(t, "鲁迅", list[0].AuthorName)
|
||||
}
|
||||
|
||||
func TestBookAuthorRelationService_GetBookAuthorRelationInfoListReturnsAuthorName(t *testing.T) {
|
||||
setupBookAuthorListTestDB(t)
|
||||
|
||||
author := book.BookAuthor{Name: "沈从文", AuthorStatus: "enabled"}
|
||||
require.NoError(t, global.GVA_DB.Create(&author).Error)
|
||||
require.NoError(t, global.GVA_DB.Create(&book.BookAuthorRelation{
|
||||
BookID: 11,
|
||||
AuthorID: author.ID,
|
||||
AuthorSort: 1,
|
||||
}).Error)
|
||||
|
||||
list, total, err := (&BookAuthorRelationService{}).GetBookAuthorRelationInfoList(bookReq.BookAuthorRelationSearch{
|
||||
PageInfo: commonReq.PageInfo{Page: 1, PageSize: 10},
|
||||
BookID: uintPtr(11),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 1, total)
|
||||
require.Len(t, list, 1)
|
||||
require.Equal(t, author.ID, list[0].AuthorID)
|
||||
require.Equal(t, "沈从文", list[0].AuthorName)
|
||||
}
|
||||
|
||||
func uintPtr(v uint) *uint {
|
||||
return &v
|
||||
}
|
||||
@@ -4,6 +4,7 @@ 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"
|
||||
)
|
||||
|
||||
@@ -37,18 +38,22 @@ func (s *BookAuthorRelationService) GetBookAuthorRelation(id uint) (item book.Bo
|
||||
return
|
||||
}
|
||||
|
||||
func (s *BookAuthorRelationService) GetBookAuthorRelationInfoList(info bookReq.BookAuthorRelationSearch) (list []book.BookAuthorRelation, total int64, err error) {
|
||||
db := global.GVA_DB.Model(&book.BookAuthorRelation{})
|
||||
func (s *BookAuthorRelationService) GetBookAuthorRelationInfoList(info bookReq.BookAuthorRelationSearch) (list []bookRes.BookAuthorRelationListItem, total int64, err error) {
|
||||
db := global.GVA_DB.Table("book_author_relation AS bar")
|
||||
if info.BookID != nil {
|
||||
db = db.Where("book_id = ?", *info.BookID)
|
||||
db = db.Where("bar.book_id = ?", *info.BookID)
|
||||
}
|
||||
if info.AuthorID != nil {
|
||||
db = db.Where("author_id = ?", *info.AuthorID)
|
||||
db = db.Where("bar.author_id = ?", *info.AuthorID)
|
||||
}
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = db.Scopes(paginate(info.PageInfo)).Order("book_id asc, author_sort asc, id desc").Find(&list).Error
|
||||
err = db.Select("bar.*, ba.name AS author_name").
|
||||
Joins("LEFT JOIN book_author AS ba ON ba.id = bar.author_id").
|
||||
Scopes(paginate(info.PageInfo)).
|
||||
Order("bar.book_id asc, bar.author_sort asc, bar.id desc").
|
||||
Scan(&list).Error
|
||||
return
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/book"
|
||||
commonModel "github.com/flipped-aurora/gin-vue-admin/server/model/common"
|
||||
)
|
||||
|
||||
func validateBook(item book.Book) error {
|
||||
@@ -59,8 +60,8 @@ var validBookPublishStatuses = map[string]bool{
|
||||
}
|
||||
|
||||
var validBookAuthorStatuses = map[string]bool{
|
||||
book.BookAuthorStatusEnabled: true,
|
||||
book.BookAuthorStatusDisabled: true,
|
||||
commonModel.CommonEnabledStatusEnabled: true,
|
||||
commonModel.CommonEnabledStatusDisabled: true,
|
||||
}
|
||||
|
||||
var validBookCommentStatuses = map[string]bool{
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/book"
|
||||
commonModel "github.com/flipped-aurora/gin-vue-admin/server/model/common"
|
||||
)
|
||||
|
||||
func TestValidateBookRejectsOutOfRangeAggregates(t *testing.T) {
|
||||
@@ -51,6 +52,15 @@ func TestValidateBookAuthorRejectsInvalidStatus(t *testing.T) {
|
||||
assertValidationErrorContains(t, err, "authorStatus")
|
||||
}
|
||||
|
||||
func TestValidateBookAuthorAcceptsCommonEnabledStatus(t *testing.T) {
|
||||
if err := validateBookAuthor(book.BookAuthor{AuthorStatus: commonModel.CommonEnabledStatusEnabled}); err != nil {
|
||||
t.Fatalf("validateBookAuthor enabled error = %v", err)
|
||||
}
|
||||
if err := validateBookAuthor(book.BookAuthor{AuthorStatus: commonModel.CommonEnabledStatusDisabled}); err != nil {
|
||||
t.Fatalf("validateBookAuthor disabled error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyBookAuthorRelationDefaults(t *testing.T) {
|
||||
item := applyBookAuthorRelationDefaults(book.BookAuthorRelation{})
|
||||
if item.AuthorSort != 1 {
|
||||
|
||||
Reference in New Issue
Block a user