First implementation of fetching collections of book managed by user
This commit is contained in:
@@ -6,9 +6,41 @@ import (
|
||||
"git.artlef.fr/bibliomane/internal/appcontext"
|
||||
"git.artlef.fr/bibliomane/internal/dto"
|
||||
"git.artlef.fr/bibliomane/internal/model"
|
||||
"git.artlef.fr/bibliomane/internal/query"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func CollectionQueryToDto(collectionsQueryResult []query.CollectionsQueryResult) []dto.CollectionItemGet {
|
||||
var collections []dto.CollectionItemGet
|
||||
for _, collectionDb := range collectionsQueryResult {
|
||||
i := findIdInCollection(collections, collectionDb.ID)
|
||||
if i == -1 {
|
||||
collections = append(collections, dto.CollectionItemGet{
|
||||
ID: collectionDb.ID,
|
||||
Name: collectionDb.Name,
|
||||
})
|
||||
//current collection is the last element
|
||||
i = len(collections) - 1
|
||||
}
|
||||
collections[i].Books = append(collections[i].Books, dto.CollectionBookItemGet{
|
||||
ID: collectionDb.BookId,
|
||||
Title: collectionDb.BookTitle,
|
||||
CoverPath: collectionDb.CoverPath,
|
||||
})
|
||||
}
|
||||
return collections
|
||||
}
|
||||
|
||||
// returns the position in collections, -1 if not found
|
||||
func findIdInCollection(collections []dto.CollectionItemGet, collectionId uint) int {
|
||||
for i, collection := range collections {
|
||||
if collection.ID == collectionId {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func FillBookDbFromFields(ac appcontext.AppContext, fields *dto.BookFields, book *model.Book) error {
|
||||
if fields.Title != nil {
|
||||
book.Title = *fields.Title
|
||||
|
||||
Reference in New Issue
Block a user