chore: update book enabled status wiring

This commit is contained in:
2026-04-27 11:41:35 +08:00
parent 93bde0a6b6
commit 67c33d06be
25 changed files with 336 additions and 122 deletions

View File

@@ -14,7 +14,7 @@ func (s *BookAuthorService) CreateBookAuthor(item book.BookAuthor) error {
if err := validateBookAuthor(item); err != nil {
return err
}
return global.GVA_DB.Create(&item).Error
return global.GVA_DB.Save(&item).Error
}
func (s *BookAuthorService) DeleteBookAuthor(item book.BookAuthor) error {
@@ -45,8 +45,8 @@ func (s *BookAuthorService) GetBookAuthorInfoList(info bookReq.BookAuthorSearch)
if info.Keyword != "" {
db = db.Where("name LIKE ?", "%"+info.Keyword+"%")
}
if info.AuthorStatus != "" {
db = db.Where("author_status = ?", info.AuthorStatus)
if info.IsEnabled != nil {
db = db.Where("is_enabled = ?", *info.IsEnabled)
}
err = db.Count(&total).Error
if err != nil {

View File

@@ -29,8 +29,8 @@ func TestBookAuthorService_GetBookAuthorInfoListReturnsAuthorName(t *testing.T)
setupBookAuthorListTestDB(t)
require.NoError(t, global.GVA_DB.Create(&book.BookAuthor{
Name: "鲁迅",
AuthorStatus: "enabled",
Name: "鲁迅",
IsEnabled: true,
}).Error)
list, total, err := (&BookAuthorService{}).GetBookAuthorInfoList(bookReq.BookAuthorSearch{
@@ -46,7 +46,7 @@ func TestBookAuthorService_GetBookAuthorInfoListReturnsAuthorName(t *testing.T)
func TestBookAuthorRelationService_GetBookAuthorRelationInfoListReturnsAuthorName(t *testing.T) {
setupBookAuthorListTestDB(t)
author := book.BookAuthor{Name: "沈从文", AuthorStatus: "enabled"}
author := book.BookAuthor{Name: "沈从文", IsEnabled: true}
require.NoError(t, global.GVA_DB.Create(&author).Error)
require.NoError(t, global.GVA_DB.Create(&book.BookAuthorRelation{
BookID: 11,

View File

@@ -13,7 +13,7 @@ func (s *BookChapterService) CreateBookChapter(item book.BookChapter) error {
if err := validateBookChapter(item); err != nil {
return err
}
return global.GVA_DB.Create(&item).Error
return global.GVA_DB.Save(&item).Error
}
func (s *BookChapterService) DeleteBookChapter(item book.BookChapter) error {

View File

@@ -10,7 +10,7 @@ import (
type BookSeriesService struct{}
func (s *BookSeriesService) CreateBookSeries(item book.BookSeries) error {
return global.GVA_DB.Create(&item).Error
return global.GVA_DB.Save(&item).Error
}
func (s *BookSeriesService) DeleteBookSeries(item book.BookSeries) error {

View File

@@ -0,0 +1,50 @@
package book
import (
"testing"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/book"
"github.com/glebarez/sqlite"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)
func setupIsEnabledCreateTestDB(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.BookSeries{}, &book.BookChapter{}))
global.GVA_DB = db
t.Cleanup(func() {
global.GVA_DB = nil
})
}
func TestCreateIsEnabledFalsePersistsForEnabledModels(t *testing.T) {
setupIsEnabledCreateTestDB(t)
require.NoError(t, (&BookAuthorService{}).CreateBookAuthor(book.BookAuthor{Name: "禁用作者", IsEnabled: false}))
require.NoError(t, (&BookSeriesService{}).CreateBookSeries(book.BookSeries{Name: "禁用系列", IsEnabled: false}))
require.NoError(t, (&BookChapterService{}).CreateBookChapter(book.BookChapter{
BookID: 1,
Title: "禁用章节",
ChapterNo: 1,
ContentFileUrl: "chapters/disabled.txt",
IsEnabled: false,
}))
var author book.BookAuthor
require.NoError(t, global.GVA_DB.Where("name = ?", "禁用作者").First(&author).Error)
require.False(t, author.IsEnabled)
var series book.BookSeries
require.NoError(t, global.GVA_DB.Where("name = ?", "禁用系列").First(&series).Error)
require.False(t, series.IsEnabled)
var chapter book.BookChapter
require.NoError(t, global.GVA_DB.Where("title = ?", "禁用章节").First(&chapter).Error)
require.False(t, chapter.IsEnabled)
}

View File

@@ -4,7 +4,6 @@ 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,20 +58,12 @@ var validBookPublishStatuses = map[string]bool{
book.BookPublishStatusOnShelf: true,
}
var validBookAuthorStatuses = map[string]bool{
commonModel.CommonEnabledStatusEnabled: true,
commonModel.CommonEnabledStatusDisabled: true,
}
var validBookCommentStatuses = map[string]bool{
book.BookCommentStatusNormal: true,
book.BookCommentStatusHidden: true,
}
func validateBookAuthor(item book.BookAuthor) error {
if item.AuthorStatus != "" && !validBookAuthorStatuses[item.AuthorStatus] {
return errors.New("authorStatus不是有效值")
}
return nil
}

View File

@@ -1,11 +1,11 @@
package book
import (
"reflect"
"strings"
"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) {
@@ -47,17 +47,21 @@ func TestValidateBookRejectsInvalidFixedDictionaryValues(t *testing.T) {
}
}
func TestValidateBookAuthorRejectsInvalidStatus(t *testing.T) {
err := validateBookAuthor(book.BookAuthor{AuthorStatus: "bad"})
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)
func TestBookAuthorUsesIsEnabledBooleanContract(t *testing.T) {
authorType := reflect.TypeOf(book.BookAuthor{})
legacyFieldName := "Author" + "Status"
if _, ok := authorType.FieldByName(legacyFieldName); ok {
t.Fatal("BookAuthor still exposes legacy status field, want IsEnabled boolean only")
}
if err := validateBookAuthor(book.BookAuthor{AuthorStatus: commonModel.CommonEnabledStatusDisabled}); err != nil {
t.Fatalf("validateBookAuthor disabled error = %v", err)
field, ok := authorType.FieldByName("IsEnabled")
if !ok {
t.Fatal("BookAuthor missing IsEnabled field")
}
if field.Type.Kind() != reflect.Bool {
t.Fatalf("BookAuthor.IsEnabled kind = %s, want bool", field.Type.Kind())
}
if got := field.Tag.Get("json"); got != "isEnabled" {
t.Fatalf("BookAuthor.IsEnabled json tag = %q, want isEnabled", got)
}
}