Passing structs instead of ids
the build was successful Details

This commit is contained in:
konrad 2017-11-24 14:36:40 +01:00 committed by kolaente
parent 79da356557
commit a19650ac3a
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 18 additions and 17 deletions

View File

@ -37,12 +37,12 @@ func GetAuthorByID(id int64) (author Author, exists bool, err error) {
} }
// GetAuthorsByBook get all authors of a book // GetAuthorsByBook get all authors of a book
func GetAuthorsByBook(bookID int64) (authors []Author, err error) { func GetAuthorsByBook(book Book) (authors []Author, err error) {
err = x. err = x.
Select("authors.*"). Select("authors.*").
Table("authors_books"). Table("authors_books").
Join("INNER", "authors", "authors_books.author_id = authors.id"). Join("INNER", "authors", "authors_books.author_id = authors.id").
Where("book_id = ?", bookID). Where("book_id = ?", book.ID).
Find(&authors) Find(&authors)
if err != nil { if err != nil {

View File

@ -40,8 +40,8 @@ func (Quantity) TableName() string {
} }
// GetQuantityByBook returns the current quantity for a book // GetQuantityByBook returns the current quantity for a book
func GetQuantityByBook(bookID int64) (quantity int64, err error) { func GetQuantityByBook(book Book) (quantity int64, err error) {
bq := Quantity{BookID: bookID} bq := Quantity{BookID: book.ID}
has, err := x.Desc("id").Get(&bq) has, err := x.Desc("id").Get(&bq)
if err != nil { if err != nil {
return 0, err return 0, err
@ -68,7 +68,7 @@ func GetBookByID(ID int64) (book Book, exists bool, err error) {
if has { if has {
// Get the books quantity. We can't join it because xorm ignores the Quantity option in struct // Get the books quantity. We can't join it because xorm ignores the Quantity option in struct
book.Quantity, err = GetQuantityByBook(ID) book.Quantity, err = GetQuantityByBook(book)
if err != nil { if err != nil {
fmt.Println("Error getting quantity:", err) fmt.Println("Error getting quantity:", err)
} }
@ -80,7 +80,7 @@ func GetBookByID(ID int64) (book Book, exists bool, err error) {
} }
// Get all authors // Get all authors
book.Authors, err = GetAuthorsByBook(book.ID) book.Authors, err = GetAuthorsByBook(book)
if err != nil { if err != nil {
fmt.Println("Error getting authors:", err) fmt.Println("Error getting authors:", err)
} }

View File

@ -29,7 +29,7 @@ func ListBooks(searchterm string) (books []*Book, err error) {
for i, book := range books { for i, book := range books {
// Get quantities // Get quantities
books[i].Quantity, err = GetQuantityByBook(book.ID) books[i].Quantity, err = GetQuantityByBook(*book)
if err != nil { if err != nil {
fmt.Println("Error getting quantity:", err) fmt.Println("Error getting quantity:", err)
} }
@ -41,7 +41,7 @@ func ListBooks(searchterm string) (books []*Book, err error) {
} }
// Get all authors // Get all authors
books[i].Authors, err = GetAuthorsByBook(book.ID) books[i].Authors, err = GetAuthorsByBook(*book)
if err != nil { if err != nil {
fmt.Println("Error getting authors:", err) fmt.Println("Error getting authors:", err)
} }

View File

@ -29,15 +29,16 @@ func (User) TableName() string {
// GetUserByID gets informations about a user by its ID // GetUserByID gets informations about a user by its ID
func GetUserByID(id int64) (user User, exists bool, err error) { func GetUserByID(id int64) (user User, exists bool, err error) {
exists, err = x.Id(id).Get(&user) /*exists, err = x.Id(id).Get(&user)
return user, exists, err return user, exists, err*/
return GetUser(User{ID: id})
} }
// GetUserByUsername returns a User struct based on its name // GetUser gets a user object
func GetUserByUsername(username string) (user User, exists bool, err error) { func GetUser(user User) (userOut User, exists bool, err error) {
user.Username = username userOut = user
exists, err = x.Get(&user) exists, err = x.Get(&userOut)
return user, exists, err return userOut, exists, err
} }
// CreateUser creates a new user and inserts it into the database // CreateUser creates a new user and inserts it into the database
@ -50,8 +51,8 @@ func CreateUser(user User) (newUser User, err error) {
return User{}, fmt.Errorf("you need to specify at least a username and a password") return User{}, fmt.Errorf("you need to specify at least a username and a password")
} }
// Check if the user already exists // Check if the user already existst
_, exists, err := GetUserByUsername(newUser.Username) _, exists, err := GetUser(User{Name: newUser.Name})
if err != nil { if err != nil {
return User{}, err return User{}, err
} }