Add book list support in the database
This commit is contained in:
parent
f2b393a453
commit
c0a70a18e1
5 changed files with 298 additions and 1 deletions
132
lib/database/list.go
Normal file
132
lib/database/list.go
Normal file
|
@ -0,0 +1,132 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"github.com/go-pg/pg"
|
||||
)
|
||||
|
||||
type BookLister interface {
|
||||
NewBookList(listID, title, username string, description []string) error
|
||||
AddBookToList(listID, bookID string) error
|
||||
DeleteBookFromList(listID, bookID string) error
|
||||
UpdateBookList(listID, title string, description []string) error
|
||||
GetBookList(listID string) (*BookList, error)
|
||||
GetListsByUser(username string) ([]BookList, error)
|
||||
GetListsByBook(bookID string) ([]BookList, error)
|
||||
}
|
||||
|
||||
type BookList struct {
|
||||
ID int `sql:"type:serial"`
|
||||
ListID string `sql:"type:varchar(16)"`
|
||||
Title string
|
||||
Description []string `sql:"description" pg:",array"`
|
||||
UserID int `sql:"type:integer"`
|
||||
User *User
|
||||
Books []Book `pg:"many2many:book_list_entries"`
|
||||
}
|
||||
|
||||
type BookListEntry struct {
|
||||
ID int `sql:"type:serial"`
|
||||
BookListID int `sql:"type:integer"`
|
||||
BookList *BookList
|
||||
BookID string `sql:"type:varchar(16)"`
|
||||
Book *Book
|
||||
}
|
||||
|
||||
func (db *pgDB) NewBookList(listID, title, username string, description []string) error {
|
||||
user, err := db.getUser(username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return db.sql.Insert(&BookList{
|
||||
ListID: listID,
|
||||
Title: title,
|
||||
Description: description,
|
||||
UserID: user.ID,
|
||||
})
|
||||
}
|
||||
|
||||
func (db *pgDB) AddBookToList(listID, bookID string) error {
|
||||
list, err := db.GetBookList(listID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return db.sql.Insert(&BookListEntry{
|
||||
BookListID: list.ID,
|
||||
BookID: bookID,
|
||||
})
|
||||
}
|
||||
|
||||
func (db *pgDB) DeleteBookFromList(listID, bookID string) error {
|
||||
list, err := db.GetBookList(listID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = db.sql.Model(&BookListEntry{}).
|
||||
Where("book_list_id = ? AND book_id = ?", list.ID, bookID).
|
||||
Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *pgDB) UpdateBookList(listID, title string, description []string) error {
|
||||
_, err := db.sql.Model(&BookList{}).
|
||||
Set("title = ?, description = ?", title, pg.Array(description)).
|
||||
Where("list_id = ?", listID).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *pgDB) GetBookList(listID string) (*BookList, error) {
|
||||
var bookList BookList
|
||||
err := db.sql.Model(&bookList).
|
||||
Column("Books").
|
||||
Where("list_id = ?", listID).
|
||||
Select()
|
||||
return &bookList, err
|
||||
}
|
||||
|
||||
func (db *pgDB) GetListsByUser(username string) ([]BookList, error) {
|
||||
var bookLists []BookList
|
||||
|
||||
user, err := db.getUser(username)
|
||||
if err != nil {
|
||||
return bookLists, err
|
||||
}
|
||||
|
||||
err = db.sql.Model(&bookLists).
|
||||
Column("Books").
|
||||
Where("user_id = ?", user.ID).
|
||||
Select()
|
||||
return bookLists, err
|
||||
}
|
||||
|
||||
func (db *pgDB) GetListsByBook(bookID string) ([]BookList, error) {
|
||||
var bookLists []BookList
|
||||
var bookListEntries []BookListEntry
|
||||
|
||||
err := db.sql.Model(&bookListEntries).
|
||||
Where("book_id = ?", bookID).
|
||||
Select()
|
||||
if err != nil {
|
||||
return bookLists, err
|
||||
}
|
||||
|
||||
whereQuery := "id IN ("
|
||||
listIDs := make([]interface{}, len(bookListEntries))
|
||||
for i, entry := range bookListEntries {
|
||||
whereQuery += "?"
|
||||
if i < len(bookListEntries)-1 {
|
||||
whereQuery += ", "
|
||||
} else {
|
||||
whereQuery += ")"
|
||||
}
|
||||
listIDs[i] = entry.ID
|
||||
}
|
||||
|
||||
err = db.sql.Model(&bookLists).
|
||||
Column("Books").
|
||||
Where(whereQuery, listIDs...).
|
||||
Select()
|
||||
return bookLists, err
|
||||
}
|
Reference in a new issue