From 9016443409f2a1c588561321f8b08091178c87f1 Mon Sep 17 00:00:00 2001 From: konrad Date: Fri, 17 Nov 2017 11:40:43 +0100 Subject: [PATCH] Moved getting authors from a book to a seperate function + Implemented getting quantity in book list --- models/author.go | 15 +++++++++++++++ models/book.go | 15 ++------------- models/books_list.go | 22 +++++++++------------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/models/author.go b/models/author.go index 7b49417..a0074d7 100644 --- a/models/author.go +++ b/models/author.go @@ -35,3 +35,18 @@ func GetAuthorByID(id int64) (author Author, exists bool, err error) { return author, has, err } + +// GetAuthorsByBook get all authors of a book +func GetAuthorsByBook(bookID int64) (authors []Author, err error) { + err = x. + Table("authors_books"). + Join("INNER", "authors", "authors_books.author_id = authors.id"). + Where("book_id = ?", bookID). + Find(&authors) + + if err != nil { + return authors, err + } + + return authors, err +} \ No newline at end of file diff --git a/models/book.go b/models/book.go index 8d95ec5..e428170 100644 --- a/models/book.go +++ b/models/book.go @@ -74,27 +74,16 @@ func GetBookByID(ID int64) (book Book, exists bool, err error) { } // Get publisher. We can't join it because xorm ignores the PublisherID option in struct - publisher := Publisher{ID: book.Publisher} - _, err = x.Get(&publisher) + book.PublisherFull, _, err = GetPublisherByID(book.Publisher) if err != nil { fmt.Println("Error getting publisher:", err) } - book.PublisherFull = publisher // Get all authors - var authors []Author - err = x. - Table("authors_books"). - Select("authors.*"). - Join("INNER", "authors", "authors_books.author_id = authors.id"). - Where("book_id = ?", book.ID). - Find(&authors) - + book.Authors, err = GetAuthorsByBook(book.ID) if err != nil { fmt.Println("Error getting authors:", err) } - - book.Authors = authors } return book, has, err diff --git a/models/books_list.go b/models/books_list.go index 27ac2be..eaa8b18 100644 --- a/models/books_list.go +++ b/models/books_list.go @@ -25,30 +25,26 @@ func ListBooks(searchterm string) (books []*Book, err error) { } } - // Get all authors and publishers + // Get all authors, publishers and quantities for i, book := range books { + // Get quantities + books[i].Quantity, err = GetQuantityByBook(book.ID) + if err != nil { + fmt.Println("Error getting quantity:", err) + } + // Get publisher - publisher := Publisher{ID: book.Publisher} - _, err := x.Get(&publisher) + books[i].PublisherFull, _, err = GetPublisherByID(book.Publisher) if err != nil { fmt.Println("Error getting publisher:", err) } - books[i].PublisherFull = publisher // Get all authors - var authors []Author - err = x. - Table("authors_books"). - Join("INNER", "authors", "authors_books.author_id = authors.id"). - Where("book_id = ?", book.ID). - Find(&authors) - + books[i].Authors, err = GetAuthorsByBook(book.ID) if err != nil { fmt.Println("Error getting authors:", err) } - - books[i].Authors = authors } return books, err