Added more details to books list
Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
parent
f8a6b57c33
commit
2c913f99d5
|
@ -1,7 +1,7 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
type Author struct {
|
type Author struct {
|
||||||
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
||||||
Forename string `xorm:"varchar(250)"`
|
Forename string `xorm:"varchar(250)"`
|
||||||
Lastame string `xorm:"varchar(250) not null"`
|
Lastame string `xorm:"varchar(250) not null"`
|
||||||
Created int64 `xorm:"created"`
|
Created int64 `xorm:"created"`
|
||||||
|
@ -10,4 +10,17 @@ type Author struct {
|
||||||
|
|
||||||
func (Author) TableName() string {
|
func (Author) TableName() string {
|
||||||
return "authors"
|
return "authors"
|
||||||
|
}
|
||||||
|
|
||||||
|
type AuthorBook struct {
|
||||||
|
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
||||||
|
AuthorID int64 `xorm:"int(11)"`
|
||||||
|
BookID int64 `xorm:"int(11)"`
|
||||||
|
|
||||||
|
Created int64 `xorm:"created"`
|
||||||
|
Updated int64 `xorm:"updated"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (AuthorBook) TableName() string {
|
||||||
|
return "authors_books"
|
||||||
}
|
}
|
|
@ -10,6 +10,9 @@ type Book struct {
|
||||||
Publisher int64 `xorm:"int(11)"`
|
Publisher int64 `xorm:"int(11)"`
|
||||||
Created int64 `xorm:"created"`
|
Created int64 `xorm:"created"`
|
||||||
Updated int64 `xorm:"updated"`
|
Updated int64 `xorm:"updated"`
|
||||||
|
|
||||||
|
PublisherFull Publisher `xorm:"-"`
|
||||||
|
Authors []Author `xorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Book) TableName() string{
|
func (Book) TableName() string{
|
||||||
|
|
|
@ -9,5 +9,31 @@ func ListBooks() (books []*Book, err error) {
|
||||||
fmt.Println("Error getting Books", err)
|
fmt.Println("Error getting Books", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
return books, err
|
return books, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ func SetEngine() (err error) {
|
||||||
x.Sync(&User{})
|
x.Sync(&User{})
|
||||||
x.Sync(&Publisher{})
|
x.Sync(&Publisher{})
|
||||||
x.Sync(&Author{})
|
x.Sync(&Author{})
|
||||||
|
x.Sync(&AuthorBook{})
|
||||||
|
|
||||||
x.ShowSQL(true)
|
x.ShowSQL(true)
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue