diff --git a/models/author.go b/models/author.go index e6086e7..8d0602e 100644 --- a/models/author.go +++ b/models/author.go @@ -37,12 +37,12 @@ func GetAuthorByID(id int64) (author Author, exists bool, err error) { } // 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. Select("authors.*"). Table("authors_books"). Join("INNER", "authors", "authors_books.author_id = authors.id"). - Where("book_id = ?", bookID). + Where("book_id = ?", book.ID). Find(&authors) if err != nil { diff --git a/models/book.go b/models/book.go index e428170..62a48dc 100644 --- a/models/book.go +++ b/models/book.go @@ -40,8 +40,8 @@ func (Quantity) TableName() string { } // GetQuantityByBook returns the current quantity for a book -func GetQuantityByBook(bookID int64) (quantity int64, err error) { - bq := Quantity{BookID: bookID} +func GetQuantityByBook(book Book) (quantity int64, err error) { + bq := Quantity{BookID: book.ID} has, err := x.Desc("id").Get(&bq) if err != nil { return 0, err @@ -68,7 +68,7 @@ func GetBookByID(ID int64) (book Book, exists bool, err error) { if has { // 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 { fmt.Println("Error getting quantity:", err) } @@ -80,7 +80,7 @@ func GetBookByID(ID int64) (book Book, exists bool, err error) { } // Get all authors - book.Authors, err = GetAuthorsByBook(book.ID) + book.Authors, err = GetAuthorsByBook(book) if err != nil { fmt.Println("Error getting authors:", err) } diff --git a/models/books_list.go b/models/books_list.go index eaa8b18..77c2714 100644 --- a/models/books_list.go +++ b/models/books_list.go @@ -29,7 +29,7 @@ func ListBooks(searchterm string) (books []*Book, err error) { for i, book := range books { // Get quantities - books[i].Quantity, err = GetQuantityByBook(book.ID) + books[i].Quantity, err = GetQuantityByBook(*book) if err != nil { fmt.Println("Error getting quantity:", err) } @@ -41,7 +41,7 @@ func ListBooks(searchterm string) (books []*Book, err error) { } // Get all authors - books[i].Authors, err = GetAuthorsByBook(book.ID) + books[i].Authors, err = GetAuthorsByBook(*book) if err != nil { fmt.Println("Error getting authors:", err) } diff --git a/models/user.go b/models/user.go index 8334b50..3142a52 100644 --- a/models/user.go +++ b/models/user.go @@ -29,15 +29,16 @@ func (User) TableName() string { // GetUserByID gets informations about a user by its ID func GetUserByID(id int64) (user User, exists bool, err error) { - exists, err = x.Id(id).Get(&user) - return user, exists, err + /*exists, err = x.Id(id).Get(&user) + return user, exists, err*/ + return GetUser(User{ID: id}) } -// GetUserByUsername returns a User struct based on its name -func GetUserByUsername(username string) (user User, exists bool, err error) { - user.Username = username - exists, err = x.Get(&user) - return user, exists, err +// GetUser gets a user object +func GetUser(user User) (userOut User, exists bool, err error) { + userOut = user + exists, err = x.Get(&userOut) + return userOut, exists, err } // 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") } - // Check if the user already exists - _, exists, err := GetUserByUsername(newUser.Username) + // Check if the user already existst + _, exists, err := GetUser(User{Name: newUser.Name}) if err != nil { return User{}, err }