Convert Id to ID
This commit is contained in:
parent
f12114c296
commit
52b9882be9
17 changed files with 76 additions and 78 deletions
|
@ -29,7 +29,7 @@ func deleteHandler(h handler) {
|
|||
if id == "" {
|
||||
continue
|
||||
}
|
||||
book, err := h.db.GetBookId(id)
|
||||
book, err := h.db.GetBookID(id)
|
||||
if err != nil {
|
||||
h.sess.Notify("Book not found!", "The book with id '"+id+"' is not there", "error")
|
||||
continue
|
||||
|
@ -59,7 +59,7 @@ func editHandler(h handler) {
|
|||
notFound(h)
|
||||
return
|
||||
}
|
||||
book, err := h.db.GetBookId(id)
|
||||
book, err := h.db.GetBookID(id)
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
|
@ -199,7 +199,7 @@ func storeHandler(h handler) {
|
|||
if id == "" {
|
||||
continue
|
||||
}
|
||||
book, err := h.db.GetBookId(id)
|
||||
book, err := h.db.GetBookID(id)
|
||||
if err != nil {
|
||||
h.sess.Notify("Book not found!", "The book with id '"+id+"' is not there", "error")
|
||||
continue
|
||||
|
|
|
@ -32,7 +32,7 @@ const (
|
|||
|
||||
func coverHandler(h handler) {
|
||||
vars := mux.Vars(h.r)
|
||||
book, err := h.db.GetBookId(vars["id"])
|
||||
book, err := h.db.GetBookID(vars["id"])
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
|
@ -49,7 +49,7 @@ func coverHandler(h handler) {
|
|||
if vars["size"] == "small" {
|
||||
file = coverSmallFile
|
||||
}
|
||||
f, err := h.store.Get(book.Id, file)
|
||||
f, err := h.store.Get(book.ID, file)
|
||||
if err != nil {
|
||||
log.Error("Error while opening image: ", err)
|
||||
notFound(h)
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -183,7 +183,7 @@ func openReadEpub(h handler) (*epubgo.Epub, database.Book) {
|
|||
if id == "" {
|
||||
return nil, book
|
||||
}
|
||||
book, err := h.db.GetBookId(id)
|
||||
book, err := h.db.GetBookID(id)
|
||||
if err != nil {
|
||||
return nil, book
|
||||
}
|
||||
|
@ -193,7 +193,7 @@ func openReadEpub(h handler) (*epubgo.Epub, database.Book) {
|
|||
}
|
||||
}
|
||||
|
||||
f, err := h.store.Get(book.Id, epubFile)
|
||||
f, err := h.store.Get(book.ID, epubFile)
|
||||
if err != nil {
|
||||
return nil, book
|
||||
}
|
||||
|
@ -228,7 +228,7 @@ func contentHandler(h handler) {
|
|||
}
|
||||
|
||||
func openEpubFile(h handler, id string, file string) error {
|
||||
book, err := h.db.GetBookId(id)
|
||||
book, err := h.db.GetBookID(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -202,11 +202,11 @@ func bookJsonRaw(book database.Book) map[string]interface{} {
|
|||
cover := ""
|
||||
coverSmall := ""
|
||||
if book.Cover {
|
||||
cover = "/cover/" + book.Id + "/big/" + book.Title + ".jpg"
|
||||
coverSmall = "/cover/" + book.Id + "/small/" + book.Title + ".jpg"
|
||||
cover = "/cover/" + book.ID + "/big/" + book.Title + ".jpg"
|
||||
coverSmall = "/cover/" + book.ID + "/small/" + book.Title + ".jpg"
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"id": book.Id,
|
||||
"id": book.ID,
|
||||
"title": book.Title,
|
||||
"authors": book.Authors,
|
||||
"contributor": book.Contributor,
|
||||
|
@ -219,7 +219,7 @@ func bookJsonRaw(book database.Book) map[string]interface{} {
|
|||
"size": book.FileSize,
|
||||
"cover": cover,
|
||||
"cover_small": coverSmall,
|
||||
"download": "/download/" + book.Id + "/" + book.Title + ".epub",
|
||||
"read": "/read/" + book.Id,
|
||||
"download": "/download/" + book.ID + "/" + book.Title + ".epub",
|
||||
"read": "/read/" + book.ID,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ func bookHandler(h handler) {
|
|||
id := mux.Vars(h.r)["id"]
|
||||
var data bookData
|
||||
data.S = GetStatus(h)
|
||||
book, err := h.db.GetBookId(id)
|
||||
book, err := h.db.GetBookID(id)
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
|
@ -78,7 +78,7 @@ func bookHandler(h handler) {
|
|||
|
||||
func downloadHandler(h handler) {
|
||||
id := mux.Vars(h.r)["id"]
|
||||
book, err := h.db.GetBookId(id)
|
||||
book, err := h.db.GetBookID(id)
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
|
@ -91,7 +91,7 @@ func downloadHandler(h handler) {
|
|||
}
|
||||
}
|
||||
|
||||
f, err := h.store.Get(book.Id, epubFile)
|
||||
f, err := h.store.Get(book.ID, epubFile)
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
|
|
|
@ -48,22 +48,22 @@ func processFile(req uploadRequest, db database.DB, store storage.Store) {
|
|||
defer epub.Close()
|
||||
|
||||
book := parser.EpubMetadata(epub)
|
||||
book.Id = genId()
|
||||
book.ID = genID()
|
||||
|
||||
req.file.Seek(0, 0)
|
||||
size, err := store.Store(book.Id, req.file, epubFile)
|
||||
size, err := store.Store(book.ID, req.file, epubFile)
|
||||
if err != nil {
|
||||
log.Error("Error storing book (", book.Id, "): ", err)
|
||||
log.Error("Error storing book (", book.ID, "): ", err)
|
||||
return
|
||||
}
|
||||
|
||||
book.FileSize = int(size)
|
||||
book.Cover = GetCover(epub, book.Id, store)
|
||||
book.Cover = GetCover(epub, book.ID, store)
|
||||
|
||||
err = db.AddBook(book)
|
||||
log.Error(":", book.Lang, ":")
|
||||
if err != nil {
|
||||
log.Error("Error storing metadata (", book.Id, "): ", err)
|
||||
log.Error("Error storing metadata (", book.ID, "): ", err)
|
||||
return
|
||||
}
|
||||
log.Info("File uploaded: ", req.filename)
|
||||
|
@ -119,7 +119,7 @@ func openMultipartEpub(file multipart.File) (*epubgo.Epub, error) {
|
|||
return epubgo.Load(reader, int64(len(buff)))
|
||||
}
|
||||
|
||||
func genId() string {
|
||||
func genID() string {
|
||||
b := make([]byte, 12)
|
||||
rand.Read(b)
|
||||
return base64.URLEncoding.EncodeToString(b)
|
||||
|
|
Reference in a new issue