Fixed issue where querying empty collection returns an empty book record

This commit is contained in:
2026-04-06 21:08:32 +02:00
parent 2552ba8e94
commit a537c12a3b
5 changed files with 31 additions and 11 deletions

View File

@@ -130,6 +130,7 @@ INSERT INTO user_books(created_at, user_id, book_id, start_read_date, rating) VA
INSERT INTO collections(name, user_id) VALUES ('Littérature française',(SELECT id FROM users WHERE name = 'demo')); INSERT INTO collections(name, user_id) VALUES ('Littérature française',(SELECT id FROM users WHERE name = 'demo'));
INSERT INTO collections(name, user_id) VALUES ('Nouvelles',(SELECT id FROM users WHERE name = 'demo')); INSERT INTO collections(name, user_id) VALUES ('Nouvelles',(SELECT id FROM users WHERE name = 'demo'));
INSERT INTO collections(name, user_id) VALUES ('Non fiction',(SELECT id FROM users WHERE name = 'demo2')); INSERT INTO collections(name, user_id) VALUES ('Non fiction',(SELECT id FROM users WHERE name = 'demo2'));
INSERT INTO collections(name, user_id) VALUES ('Empty',(SELECT id FROM users WHERE name = 'demo'));
INSERT INTO collection_books(collection_id, book_id) VALUES ((SELECT id FROM collections WHERE name = 'Littérature française'), (SELECT id FROM books WHERE title = 'Nord')); INSERT INTO collection_books(collection_id, book_id) VALUES ((SELECT id FROM collections WHERE name = 'Littérature française'), (SELECT id FROM books WHERE title = 'Nord'));
INSERT INTO collection_books(collection_id, book_id) VALUES ((SELECT id FROM collections WHERE name = 'Littérature française'), (SELECT id FROM books WHERE title = 'Gargantua')); INSERT INTO collection_books(collection_id, book_id) VALUES ((SELECT id FROM collections WHERE name = 'Littérature française'), (SELECT id FROM books WHERE title = 'Gargantua'));

View File

@@ -14,7 +14,10 @@ func CollectionQueryToCollectionDto(collectionsQueryResult []query.CollectionsQu
var collection dto.CollectionGet var collection dto.CollectionGet
for _, collectionsDb := range collectionsQueryResult { for _, collectionsDb := range collectionsQueryResult {
collection.Name = collectionsDb.Name collection.Name = collectionsDb.Name
collection.Books = append(collection.Books, collectionDbToBookItem(&collectionsDb)) book := collectionDbToBookItem(&collectionsDb)
if book != nil {
collection.Books = append(collection.Books, *book)
}
collection.UserID = collectionsDb.UserID collection.UserID = collectionsDb.UserID
} }
return collection return collection
@@ -32,16 +35,24 @@ func CollectionQueryToCollectionItemDto(collectionsQueryResult []query.Collectio
//current collection is the last element //current collection is the last element
i = len(collections) - 1 i = len(collections) - 1
} }
collections[i].Books = append(collections[i].Books, collectionDbToBookItem(&collectionDb)) book := collectionDbToBookItem(&collectionDb)
if book != nil {
collections[i].Books = append(collections[i].Books, *book)
}
} }
return collections return collections
} }
func collectionDbToBookItem(collectionDb *query.CollectionsQueryResult) dto.CollectionBookItemGet { func collectionDbToBookItem(collectionDb *query.CollectionsQueryResult) *dto.CollectionBookItemGet {
return dto.CollectionBookItemGet{ if collectionDb.BookId > 0 {
ID: collectionDb.BookId, bookItem := dto.CollectionBookItemGet{
Title: collectionDb.BookTitle, ID: collectionDb.BookId,
CoverPath: collectionDb.CoverPath, Title: collectionDb.BookTitle,
CoverPath: collectionDb.CoverPath,
}
return &bookItem
} else {
return nil
} }
} }

View File

@@ -12,8 +12,8 @@ import (
func TestFetchAllCollections_OK(t *testing.T) { func TestFetchAllCollections_OK(t *testing.T) {
status, res := testFetchCollections(t, "10", "0") status, res := testFetchCollections(t, "10", "0")
assert.Equal(t, http.StatusOK, status) assert.Equal(t, http.StatusOK, status)
assert.Equal(t, int64(2), res.Count) assert.Equal(t, int64(3), res.Count)
assert.Equal(t, 2, len(res.Collections)) assert.Equal(t, 3, len(res.Collections))
} }
func testFetchCollections(t *testing.T, limit string, offset string) (int, dto.CollectionItemsGet) { func testFetchCollections(t *testing.T, limit string, offset string) (int, dto.CollectionItemsGet) {

View File

@@ -25,10 +25,18 @@ func TestGetCollection_Limit(t *testing.T) {
} }
func TestGetCollection_Unauthorized(t *testing.T) { func TestGetCollection_Unauthorized(t *testing.T) {
status, _ := testGetCollection(t, "4", "10", "0") status, _ := testGetCollection(t, "3", "10", "0")
assert.Equal(t, http.StatusUnauthorized, status) assert.Equal(t, http.StatusUnauthorized, status)
} }
func TestGetCollection_Empty(t *testing.T) {
status, collection := testGetCollection(t, "4", "10", "0")
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, "Empty", collection.Name)
assert.Equal(t, 0, len(collection.Books))
assert.Equal(t, int64(0), collection.Count)
}
func testGetCollection(t *testing.T, id string, limit string, offset string) (int, dto.CollectionGet) { func testGetCollection(t *testing.T, id string, limit string, offset string) (int, dto.CollectionGet) {
return testutils.TestFetchModel[dto.CollectionGet](t, "/ws/collection/"+id, limit, offset) return testutils.TestFetchModel[dto.CollectionGet](t, "/ws/collection/"+id, limit, offset)
} }

View File

@@ -226,7 +226,7 @@ func FetchCollectionBooks(db *gorm.DB, collectionId uint, limit int, offset int)
func FetchCollectionBooksCount(db *gorm.DB, collectionId uint) (int64, error) { func FetchCollectionBooksCount(db *gorm.DB, collectionId uint) (int64, error) {
var count int64 var count int64
res := fetchCollectionBooksQuery(db, collectionId).Count(&count) res := fetchCollectionBooksQuery(db, collectionId).Where("book_id IS NOT NULL").Count(&count)
return count, res.Error return count, res.Error
} }