Convert Id to ID
This commit is contained in:
parent
f12114c296
commit
52b9882be9
17 changed files with 76 additions and 78 deletions
|
@ -5,8 +5,9 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// Book metadata
|
||||
type Book struct {
|
||||
Id string
|
||||
ID string
|
||||
Title string
|
||||
Authors []string `sql:"authors" pg:",array"`
|
||||
Contributor string
|
||||
|
@ -42,6 +43,8 @@ func (db *pgDB) GetBooks(query string, length int, start int) (books []Book, num
|
|||
|
||||
// TODO: func (db *pgDB) GetBooksIter() Iter {
|
||||
|
||||
// GetNewBooks returns a list of books in the incoming queue and the number of books
|
||||
// in the queue
|
||||
func (db *pgDB) GetNewBooks(query string, length int, start int) (books []Book, num int, err error) {
|
||||
return db.getBooks(false, query, length, start)
|
||||
}
|
||||
|
@ -81,7 +84,8 @@ func (db *pgDB) getBooks(active bool, query string, length int, start int) (book
|
|||
return books, num, err
|
||||
}
|
||||
|
||||
func (db *pgDB) GetBookId(id string) (Book, error) {
|
||||
// GetBookID returns a the book with the specified id
|
||||
func (db *pgDB) GetBookID(id string) (Book, error) {
|
||||
var book Book
|
||||
err := db.sql.Model(&book).
|
||||
Where("id = ?", id).
|
||||
|
@ -89,6 +93,7 @@ func (db *pgDB) GetBookId(id string) (Book, error) {
|
|||
return book, err
|
||||
}
|
||||
|
||||
// DeleteBook removes the book with id from the database
|
||||
func (db *pgDB) DeleteBook(id string) error {
|
||||
_, err := db.sql.Model(&Book{}).
|
||||
Where("id = ?", id).
|
||||
|
@ -96,6 +101,7 @@ func (db *pgDB) DeleteBook(id string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// UpdateBook metadata
|
||||
func (db *pgDB) UpdateBook(id string, data map[string]interface{}) error {
|
||||
setCondition := ""
|
||||
params := []interface{}{}
|
||||
|
@ -125,11 +131,7 @@ func (db *pgDB) UpdateBook(id string, data map[string]interface{}) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (db *pgDB) FlagBadQuality(id string, user string) error {
|
||||
// TODO: delete me
|
||||
return nil
|
||||
}
|
||||
|
||||
// ActiveBook activates the book
|
||||
func (db *pgDB) ActiveBook(id string) error {
|
||||
uploadDate := time.Now()
|
||||
_, err := db.sql.Model(&Book{}).
|
||||
|
@ -139,6 +141,7 @@ func (db *pgDB) ActiveBook(id string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// IsBookActive checks if the book is active
|
||||
func (db *pgDB) IsBookActive(id string) bool {
|
||||
var active []bool
|
||||
err := db.sql.Model(&Book{}).
|
||||
|
|
|
@ -3,7 +3,7 @@ package database
|
|||
import "testing"
|
||||
|
||||
var book = Book{
|
||||
Id: "r_m-IOzzIbA6QK5w",
|
||||
ID: "r_m-IOzzIbA6QK5w",
|
||||
Title: "some title",
|
||||
Authors: []string{"Alice", "Bob"},
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ func TestAddAndDeleteBook(t *testing.T) {
|
|||
t.Error("Book title don't match : '", books[0].Title, "' <=> '", book.Title, "'")
|
||||
}
|
||||
|
||||
err = db.DeleteBook(books[0].Id)
|
||||
err = db.DeleteBook(books[0].ID)
|
||||
if err != nil {
|
||||
t.Fatal("db.DeleteBook() return an error: ", err)
|
||||
}
|
||||
|
@ -47,16 +47,16 @@ func TestActiveBook(t *testing.T) {
|
|||
|
||||
testAddBook(t, db)
|
||||
books, _, _ := db.GetNewBooks("", 1, 0)
|
||||
id := books[0].Id
|
||||
id := books[0].ID
|
||||
|
||||
err := db.ActiveBook(id)
|
||||
if err != nil {
|
||||
t.Fatal("db.ActiveBook(", id, ") return an error: ", err)
|
||||
}
|
||||
|
||||
b, err := db.GetBookId(id)
|
||||
b, err := db.GetBookID(id)
|
||||
if err != nil {
|
||||
t.Fatal("db.GetBookId(", id, ") return an error: ", err)
|
||||
t.Fatal("db.GetBookID(", id, ") return an error: ", err)
|
||||
}
|
||||
if !b.Active {
|
||||
t.Error("Book is not activated")
|
||||
|
@ -92,7 +92,7 @@ func TestUpdateBook(t *testing.T) {
|
|||
testAddBook(t, db)
|
||||
|
||||
newTitle := "other title"
|
||||
err := db.UpdateBook(book.Id, map[string]interface{}{
|
||||
err := db.UpdateBook(book.ID, map[string]interface{}{
|
||||
"title": newTitle,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
|
@ -9,10 +9,9 @@ type DB interface {
|
|||
AddBook(book Book) error
|
||||
GetBooks(query string, length int, start int) (books []Book, num int, err error)
|
||||
GetNewBooks(query string, length int, start int) (books []Book, num int, err error)
|
||||
GetBookId(id string) (Book, error)
|
||||
GetBookID(id string) (Book, error)
|
||||
DeleteBook(id string) error
|
||||
UpdateBook(id string, data map[string]interface{}) error
|
||||
FlagBadQuality(id string, user string) error
|
||||
ActiveBook(id string) error
|
||||
IsBookActive(id string) bool
|
||||
AddUser(name string, pass string) error
|
||||
|
|
|
@ -24,8 +24,8 @@ func (db *roDB) GetNewBooks(query string, length int, start int) (books []Book,
|
|||
return db.db.GetNewBooks(query, length, start)
|
||||
}
|
||||
|
||||
func (db *roDB) GetBookId(id string) (Book, error) {
|
||||
return db.db.GetBookId(id)
|
||||
func (db *roDB) GetBookID(id string) (Book, error) {
|
||||
return db.db.GetBookID(id)
|
||||
}
|
||||
|
||||
func (db *roDB) DeleteBook(id string) error {
|
||||
|
@ -36,10 +36,6 @@ func (db *roDB) UpdateBook(id string, data map[string]interface{}) error {
|
|||
return errors.New("RO database")
|
||||
}
|
||||
|
||||
func (db *roDB) FlagBadQuality(id string, user string) error {
|
||||
return errors.New("RO database")
|
||||
}
|
||||
|
||||
func (db *roDB) ActiveBook(id string) error {
|
||||
return errors.New("RO database")
|
||||
}
|
||||
|
|
Reference in a new issue