This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/lib/database/list.go

139 lines
3.3 KiB
Go
Raw Permalink Normal View History

2018-04-09 00:15:58 +00:00
package database
import (
2020-11-30 19:03:31 +00:00
"github.com/go-pg/pg/v10"
2018-04-09 00:15:58 +00:00
)
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 {
2019-11-06 06:53:41 +00:00
ID int `pg:"type:serial"`
ListID string `pg:"type:varchar(16)"`
2018-04-09 00:15:58 +00:00
Title string
2019-11-06 06:53:41 +00:00
Description []string `pg:"description,array"`
UserID int `pg:"type:integer"`
2020-11-30 19:03:31 +00:00
User *User `pg:"rel:has-one"`
Books []Book `pg:"many2many:book_list_entries"`
2018-04-09 00:15:58 +00:00
}
type BookListEntry struct {
2020-11-30 19:03:31 +00:00
ID int `pg:"type:serial"`
BookListID int `pg:"type:integer"`
BookList *BookList `pg:"rel:has-one"`
BookID string `pg:"type:varchar(16)"`
Book *Book `pg:"rel:has-one"`
2018-04-09 00:15:58 +00:00
}
func (db *pgDB) NewBookList(listID, title, username string, description []string) error {
user, err := db.getUser(username)
if err != nil {
return err
}
2020-11-30 19:03:31 +00:00
_, err = db.sql.Model(&BookList{
2018-04-09 00:15:58 +00:00
ListID: listID,
Title: title,
Description: description,
UserID: user.ID,
2020-11-30 19:03:31 +00:00
}).Insert()
return err
2018-04-09 00:15:58 +00:00
}
func (db *pgDB) AddBookToList(listID, bookID string) error {
list, err := db.GetBookList(listID)
if err != nil {
return err
}
2020-11-30 19:03:31 +00:00
_, err = db.sql.Model(&BookListEntry{
2018-04-09 00:15:58 +00:00
BookListID: list.ID,
BookID: bookID,
2020-11-30 19:03:31 +00:00
}).Insert()
return err
2018-04-09 00:15:58 +00:00
}
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).
2019-11-06 06:53:41 +00:00
Relation("Books").
Relation("User").
2018-04-09 00:15:58 +00:00
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).
2019-11-06 06:53:41 +00:00
Relation("Books").
Relation("User").
2018-04-09 00:15:58 +00:00
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).
2019-11-06 06:53:41 +00:00
Relation("BookList").
2018-04-09 00:15:58 +00:00
Where("book_id = ?", bookID).
Select()
if err != nil || len(bookListEntries) == 0 {
2018-04-09 00:15:58 +00:00
return bookLists, err
}
whereQuery := `list_id IN (`
2018-04-09 00:15:58 +00:00
listIDs := make([]interface{}, len(bookListEntries))
for i, entry := range bookListEntries {
whereQuery += "?"
if i < len(bookListEntries)-1 {
whereQuery += ", "
}
listIDs[i] = entry.BookList.ListID
2018-04-09 00:15:58 +00:00
}
whereQuery += ")"
2018-04-09 00:15:58 +00:00
err = db.sql.Model(&bookLists).
2019-11-06 06:53:41 +00:00
Relation("Books").
Relation("User").
2018-04-09 00:15:58 +00:00
Where(whereQuery, listIDs...).
Select()
return bookLists, err
}