Update to pg v9
This commit is contained in:
parent
91beca9a55
commit
defaa2ae0b
9 changed files with 75 additions and 44 deletions
|
@ -4,26 +4,26 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-pg/pg"
|
||||
"github.com/go-pg/pg/v9"
|
||||
)
|
||||
|
||||
// Book metadata
|
||||
type Book struct {
|
||||
ID string `sql:"type:varchar(16)"`
|
||||
ID string `pg:"type:varchar(16)"`
|
||||
Title string
|
||||
Authors []string `sql:"authors" pg:",array"`
|
||||
Authors []string `pg:"authors,array"`
|
||||
Contributor string
|
||||
Publisher string
|
||||
Description string
|
||||
Tags []string `sql:"tags" pg:",array"`
|
||||
Tags []string `pg:"tags,array"`
|
||||
Date string
|
||||
Lang string `sql:"type:varchar(3)"`
|
||||
Isbn string `sql:"type:varchar(13)"`
|
||||
FileSize int `sql:"type:integer"`
|
||||
Cover bool `sql:",notnull"`
|
||||
Active bool `sql:",notnull"`
|
||||
UploadDate time.Time `sql:"type:timestamp"`
|
||||
Tsv string `sql:"type:tsvector"`
|
||||
Lang string `pg:"type:varchar(3)"`
|
||||
Isbn string `pg:"type:varchar(13)"`
|
||||
FileSize int `pg:"type:integer"`
|
||||
Cover bool `pg:",notnull"`
|
||||
Active bool `pg:",notnull"`
|
||||
UploadDate time.Time `pg:"type:timestamp"`
|
||||
Tsv string `pg:"type:tsvector"`
|
||||
Visit *Visit
|
||||
}
|
||||
|
||||
|
@ -156,7 +156,7 @@ func (db *pgDB) ActiveBook(id string) error {
|
|||
func (db *pgDB) IsBookActive(id string) bool {
|
||||
var active []bool
|
||||
err := db.sql.Model(&Book{}).
|
||||
Column("active").
|
||||
Relation("active").
|
||||
Where("id = ?", id).
|
||||
Select(&active)
|
||||
if err != nil || len(active) != 1 {
|
||||
|
|
|
@ -3,8 +3,8 @@ package database
|
|||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-pg/pg"
|
||||
"github.com/go-pg/pg/orm"
|
||||
"github.com/go-pg/pg/v9"
|
||||
"github.com/go-pg/pg/v9/orm"
|
||||
)
|
||||
|
||||
type DB interface {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -7,8 +7,8 @@ import (
|
|||
|
||||
// New entry in the news table
|
||||
type New struct {
|
||||
ID int `sql:"type:serial"`
|
||||
Date time.Time `sql:"type:timestamp"`
|
||||
ID int `pg:"type:serial"`
|
||||
Date time.Time `pg:"type:timestamp"`
|
||||
Text string
|
||||
}
|
||||
|
||||
|
|
|
@ -7,10 +7,10 @@ import (
|
|||
)
|
||||
|
||||
type Visit struct {
|
||||
ID int `sql:"type:serial"`
|
||||
Downloads int `sql:"type:integer,notnull"`
|
||||
Views int `sql:"type:integer,notnull"`
|
||||
BookID string `sql:"type:varchar(16),unique"`
|
||||
ID int `pg:"type:serial"`
|
||||
Downloads int `pg:"type:integer,notnull"`
|
||||
Views int `pg:"type:integer,notnull"`
|
||||
BookID string `pg:"type:varchar(16),unique"`
|
||||
Book *Book
|
||||
}
|
||||
|
||||
|
|
|
@ -12,15 +12,15 @@ const (
|
|||
)
|
||||
|
||||
type Submission struct {
|
||||
ID int `sql:"type:serial"`
|
||||
SubmissionID string `sql:"type:varchar(16)"`
|
||||
ID int `pg:"type:serial"`
|
||||
SubmissionID string `pg:"type:varchar(16)"`
|
||||
Filename string
|
||||
Status string
|
||||
Comment string
|
||||
LastModified time.Time
|
||||
UserID int `sql:"type:integer,unique"`
|
||||
UserID int `pg:"type:integer,unique"`
|
||||
User *User
|
||||
BookID string `sql:"type:varchar(16),unique"`
|
||||
BookID string `pg:"type:varchar(16),unique"`
|
||||
Book *Book
|
||||
}
|
||||
|
||||
|
@ -78,7 +78,7 @@ func (db *pgDB) GetComment(bookID string) (string, error) {
|
|||
|
||||
func (db *pgDB) GetSubmission(submissionID string) (submission []Submission, err error) {
|
||||
err = db.sql.Model(&submission).
|
||||
Column("Book").
|
||||
Relation("Book").
|
||||
Where("submission_id = ?", submissionID).
|
||||
Select()
|
||||
return
|
||||
|
@ -86,7 +86,8 @@ func (db *pgDB) GetSubmission(submissionID string) (submission []Submission, err
|
|||
|
||||
func (db *pgDB) GetUserSubmissions(userName string) (submission []Submission, err error) {
|
||||
err = db.sql.Model(&submission).
|
||||
Column("Book", "User").
|
||||
Relation("Book").
|
||||
Relation("User").
|
||||
Where("username = ?", userName).
|
||||
Order("last_modified DESC").
|
||||
Select()
|
||||
|
|
|
@ -16,11 +16,11 @@ import (
|
|||
var alphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9_\-\.]+$`).MatchString
|
||||
|
||||
type User struct {
|
||||
ID int `sql:"type:serial"`
|
||||
Username string `sql:"type:varchar(255),unique"`
|
||||
ID int `pg:"type:serial"`
|
||||
Username string `pg:"type:varchar(255),unique"`
|
||||
Password []byte
|
||||
Salt []byte
|
||||
Role string `sql:"type:varchar(255)"`
|
||||
Role string `pg:"type:varchar(255)"`
|
||||
LastLogin time.Time
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue