2017-10-09 16:20:16 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2017-10-10 15:14:51 +00:00
|
|
|
type BookPublisher struct {
|
|
|
|
Book `xorm:"extends"`
|
|
|
|
Publisher `xorm:"extends"`
|
|
|
|
}
|
|
|
|
|
2017-10-10 19:40:14 +00:00
|
|
|
func ListBooks(searchterm string) (books []*Book, err error) {
|
2017-10-09 16:20:16 +00:00
|
|
|
|
2017-10-10 19:40:14 +00:00
|
|
|
if searchterm == "" {
|
|
|
|
err = x.Table("books").
|
|
|
|
//Join("INNER", "publishers", "books.publisher = publishers.id").
|
|
|
|
Find(&books)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error getting Books", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = x.Where("title LIKE ?", "%" + searchterm + "%").Find(&books)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error getting Books", err)
|
|
|
|
}
|
2017-10-09 16:20:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 19:40:14 +00:00
|
|
|
|
2017-10-10 10:06:34 +00:00
|
|
|
// Get all authors and publishers
|
|
|
|
for i, book := range books {
|
|
|
|
|
|
|
|
// Get publisher
|
|
|
|
publisher := Publisher{ID: book.Publisher}
|
|
|
|
_, err := x.Get(&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)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error getting authors:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
books[i].Authors = authors
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:29:09 +00:00
|
|
|
return books, err
|
2017-10-09 16:20:16 +00:00
|
|
|
}
|