Library/models/author.go

54 lines
1.4 KiB
Go
Raw Permalink Normal View History

package models
2017-11-08 09:55:17 +00:00
// Author holds infos about an author
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-11-08 09:55:17 +00:00
// TableName returns the table name for struct author
func (Author) TableName() string {
return "authors"
}
2017-11-08 09:55:17 +00:00
// AuthorBook holds the relation between an author and a books
type AuthorBook struct {
2017-11-07 15:35:10 +00:00
ID int64 `xorm:"int(11) autoincr not null unique pk"`
AuthorID int64 `xorm:"int(11)"`
2017-11-07 15:35:10 +00:00
BookID int64 `xorm:"int(11)"`
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
func (AuthorBook) TableName() string {
return "authors_books"
}
2017-11-08 09:55:17 +00:00
// GetAuthorByID gets information about an author by its ID
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
}
// GetAuthorsByBook get all authors of a book
2017-11-24 13:36:40 +00:00
func GetAuthorsByBook(book Book) (authors []Author, err error) {
err = x.
Select("authors.*").
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).
Find(&authors)
if err != nil {
return authors, err
}
return authors, err
2017-11-17 10:41:18 +00:00
}