2017-10-10 09:20:16 +00:00
|
|
|
package models
|
|
|
|
|
2017-11-08 09:55:17 +00:00
|
|
|
// Author holds infos about an author
|
2017-10-10 09:20:16 +00:00
|
|
|
type Author struct {
|
2017-11-29 15:15:29 +00:00
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
|
|
|
|
Forename string `xorm:"varchar(250)" json:"forename"`
|
|
|
|
Lastname string `xorm:"varchar(250) not null" json:"lastname"`
|
|
|
|
Created int64 `xorm:"created" json:"created"`
|
|
|
|
Updated int64 `xorm:"updated" json:"updated"`
|
2017-10-10 09:20:16 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 09:55:17 +00:00
|
|
|
// TableName returns the table name for struct author
|
2017-10-10 09:20:16 +00:00
|
|
|
func (Author) TableName() string {
|
|
|
|
return "authors"
|
2017-10-10 10:06:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 09:55:17 +00:00
|
|
|
// AuthorBook holds the relation between an author and a books
|
2017-10-10 10:06:34 +00:00
|
|
|
type AuthorBook struct {
|
2017-11-07 15:35:10 +00:00
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
2017-10-10 10:06:34 +00:00
|
|
|
AuthorID int64 `xorm:"int(11)"`
|
2017-11-07 15:35:10 +00:00
|
|
|
BookID int64 `xorm:"int(11)"`
|
2017-10-10 10:06:34 +00:00
|
|
|
|
|
|
|
Created int64 `xorm:"created"`
|
|
|
|
Updated int64 `xorm:"updated"`
|
|
|
|
}
|
|
|
|
|
2017-11-08 09:55:17 +00:00
|
|
|
// TableName returns the name for the relation author <-> book
|
2017-10-10 10:06:34 +00:00
|
|
|
func (AuthorBook) TableName() string {
|
|
|
|
return "authors_books"
|
2017-10-10 16:11:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 09:55:17 +00:00
|
|
|
// GetAuthorByID gets information about an author by its ID
|
2017-10-10 16:11:09 +00:00
|
|
|
func GetAuthorByID(id int64) (author Author, exists bool, err error) {
|
|
|
|
has, err := x.Id(id).Get(&author)
|
|
|
|
|
|
|
|
return author, has, err
|
2017-11-07 15:35:10 +00:00
|
|
|
}
|
2017-11-17 10:40:43 +00:00
|
|
|
|
|
|
|
// GetAuthorsByBook get all authors of a book
|
2017-11-24 13:36:40 +00:00
|
|
|
func GetAuthorsByBook(book Book) (authors []Author, err error) {
|
2017-11-17 10:40:43 +00:00
|
|
|
err = x.
|
2017-11-20 11:49:25 +00:00
|
|
|
Select("authors.*").
|
2017-11-17 10:40:43 +00:00
|
|
|
Table("authors_books").
|
|
|
|
Join("INNER", "authors", "authors_books.author_id = authors.id").
|
2017-11-24 13:36:40 +00:00
|
|
|
Where("book_id = ?", book.ID).
|
2017-11-17 10:40:43 +00:00
|
|
|
Find(&authors)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return authors, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return authors, err
|
2017-11-17 10:41:18 +00:00
|
|
|
}
|