Update to pg v9

This commit is contained in:
Las Zenow 2019-11-06 06:53:41 +00:00
parent 91beca9a55
commit defaa2ae0b
9 changed files with 75 additions and 44 deletions

View file

@ -1,7 +1,7 @@
package database
import (
"github.com/go-pg/pg"
"github.com/go-pg/pg/v9"
)
type BookLister interface {
@ -15,20 +15,20 @@ type BookLister interface {
}
type BookList struct {
ID int `sql:"type:serial"`
ListID string `sql:"type:varchar(16)"`
ID int `pg:"type:serial"`
ListID string `pg:"type:varchar(16)"`
Title string
Description []string `sql:"description" pg:",array"`
UserID int `sql:"type:integer"`
Description []string `pg:"description,array"`
UserID int `pg:"type:integer"`
User *User
Books []Book `pg:"many2many:book_list_entries"`
}
type BookListEntry struct {
ID int `sql:"type:serial"`
BookListID int `sql:"type:integer"`
ID int `pg:"type:serial"`
BookListID int `pg:"type:integer"`
BookList *BookList
BookID string `sql:"type:varchar(16)"`
BookID string `pg:"type:varchar(16)"`
Book *Book
}
@ -80,7 +80,8 @@ func (db *pgDB) UpdateBookList(listID, title string, description []string) error
func (db *pgDB) GetBookList(listID string) (*BookList, error) {
var bookList BookList
err := db.sql.Model(&bookList).
Column("Books", "User").
Relation("Books").
Relation("User").
Where("list_id = ?", listID).
Select()
return &bookList, err
@ -95,7 +96,8 @@ func (db *pgDB) GetListsByUser(username string) ([]BookList, error) {
}
err = db.sql.Model(&bookLists).
Column("Books", "User").
Relation("Books").
Relation("User").
Where("user_id = ?", user.ID).
Select()
return bookLists, err
@ -106,7 +108,7 @@ func (db *pgDB) GetListsByBook(bookID string) ([]BookList, error) {
var bookListEntries []BookListEntry
err := db.sql.Model(&bookListEntries).
Column("BookList").
Relation("BookList").
Where("book_id = ?", bookID).
Select()
if err != nil || len(bookListEntries) == 0 {
@ -125,7 +127,8 @@ func (db *pgDB) GetListsByBook(bookID string) ([]BookList, error) {
whereQuery += ")"
err = db.sql.Model(&bookLists).
Column("Books", "User").
Relation("Books").
Relation("User").
Where(whereQuery, listIDs...).
Select()
return bookLists, err