Convert Id to ID

This commit is contained in:
Las Zenow 2016-07-30 07:59:30 -04:00
parent f12114c296
commit 52b9882be9
17 changed files with 76 additions and 78 deletions

View file

@ -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

View file

@ -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)

View file

@ -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{}).

View file

@ -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 {

View file

@ -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

View file

@ -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")
}

View file

@ -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
}

View file

@ -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,
}
}

View file

@ -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

View file

@ -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)

View file

@ -4,13 +4,13 @@
{{with .Book}}
<script>
function delBook(){
var div = document.getElementById('delete');
var div = document.getElementByID('delete');
div.innerHTML =
'<div class="alert alert-error fade in"> \
<a class="close" data-dismiss="alert">×</a> \
<h4 class="alert-heading">Do you really want to delete it?</h4> \
<p>Remove a book is permanent, you won\'t be able to get it back</p> \
<a class="btn btn-danger" href="/delete/{{.Id}}/">Remove it</a> \
<a class="btn btn-danger" href="/delete/{{.ID}}/">Remove it</a> \
<a class="btn" href="#" data-dismiss="alert">Cancel</a> \
</div>';
}
@ -25,7 +25,7 @@ function delBook(){
<div class="row">
{{if .Cover}}
<div class="span4">
<img src="/cover/{{.Id}}/big/{{.Title}}.jpg" alt="{{.Title}}" class="pull-right" />
<img src="/cover/{{.ID}}/big/{{.Title}}.jpg" alt="{{.Title}}" class="pull-right" />
</div>
{{end}}
@ -45,15 +45,15 @@ function delBook(){
<div class="span3">
<div class="row">
<div class="btn-group pull-right">
<a href="/download/{{.Id}}/{{.Title}}.epub" class="btn btn-large btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.Id}}" class="btn btn-large btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
<a href="/download/{{.ID}}/{{.Title}}.epub" class="btn btn-large btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.ID}}" class="btn btn-large btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
</div>
{{if eq $role "admin" "moderator"}}
<div class="row"><p></p></div>
<div class="row">
<div class="btn-group pull-right">
<a href="/edit/{{.Id}}" class="btn btn-primary"><i class="icon-pencil"></i> Edit</a>
<a href="/edit/{{.ID}}" class="btn btn-primary"><i class="icon-pencil"></i> Edit</a>
<a href="#" onClick="delBook();" class="btn btn-danger"><i class="icon-trash"></i> Delete</a>
</div>
</div>

View file

@ -4,12 +4,12 @@
<div class="row">
{{if .Cover}}
<div class="span4">
<img src="/cover/{{.Id}}/big/{{.Title}}.jpg" alt="{{.Title}}" class="pull-right" />
<img src="/cover/{{.ID}}/big/{{.Title}}.jpg" alt="{{.Title}}" class="pull-right" />
</div>
{{end}}
<div class="span8">
<form class="form-horizontal" method="POST" action="/save/{{.Id}}">
<form class="form-horizontal" method="POST" action="/save/{{.ID}}">
<fieldset>
<div class="control-group">
<label class="control-label" for="title">Title</label>
@ -68,7 +68,7 @@
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
<a href="/book/{{.Id}}" class="btn">Cancel</a>
<a href="/book/{{.ID}}" class="btn">Cancel</a>
</div>
</fieldset>
</form>

View file

@ -25,8 +25,8 @@
{{range .}}
<li class="span2">
<div class="thumbnail centered" style="border:none;">
<a href="/book/{{.Id}}">
{{if .Cover}}<div class="down"><img class="img-rounded" src="/cover/{{.Id}}/small/{{.Title}}.jpg" alt="{{.Title}}" /></div>{{end}}
<a href="/book/{{.ID}}">
{{if .Cover}}<div class="down"><img class="img-rounded" src="/cover/{{.ID}}/small/{{.Title}}.jpg" alt="{{.Title}}" /></div>{{end}}
<p><strong>{{.Title}}</strong></p>
</a>
</div>
@ -43,8 +43,8 @@
{{range .}}
<li class="span2">
<div class="thumbnail centered" style="border:none;">
<a href="/book/{{.Id}}">
{{if .Cover}}<div class="down"><img class="img-rounded" src="/cover/{{.Id}}/small/{{.Title}}.jpg" alt="{{.Title}}" /></div>{{end}}
<a href="/book/{{.ID}}">
{{if .Cover}}<div class="down"><img class="img-rounded" src="/cover/{{.ID}}/small/{{.Title}}.jpg" alt="{{.Title}}" /></div>{{end}}
<p><strong>{{.Title}}</strong></p>
</a>
</div>
@ -61,8 +61,8 @@
{{range .}}
<li class="span2">
<div class="thumbnail centered" style="border:none;">
<a href="/book/{{.Id}}">
{{if .Cover}}<div class="down"><img class="img-rounded" src="/cover/{{.Id}}/small/{{.Title}}.jpg" alt="{{.Title}}" /></div>{{end}}
<a href="/book/{{.ID}}">
{{if .Cover}}<div class="down"><img class="img-rounded" src="/cover/{{.ID}}/small/{{.Title}}.jpg" alt="{{.Title}}" /></div>{{end}}
<p><strong>{{.Title}}</strong></p>
</a>
</div>

View file

@ -6,8 +6,8 @@
{{if .Books}}
<div class="centered btn-group">
<a href="/store/{{range .Books}}{{.B.Id}}/{{end}}" class="btn btn-large btn-success"><i class="icon-ok"></i> Save All</a>
<a href="/delete/{{range .Books}}{{.B.Id}}/{{end}}" class="btn btn-large btn-danger"><i class="icon-remove"></i> Delete All</a>
<a href="/store/{{range .Books}}{{.B.ID}}/{{end}}" class="btn btn-large btn-success"><i class="icon-ok"></i> Save All</a>
<a href="/delete/{{range .Books}}{{.B.ID}}/{{end}}" class="btn btn-large btn-danger"><i class="icon-remove"></i> Delete All</a>
</div>
{{end}}
<p class="centered">Found {{.Found}} books.<br /></p>
@ -30,7 +30,7 @@
{{with .B}}
<div class="row">
<div class="span1">
<p class="pull-right">{{if .Cover}}<img src="/cover/{{.Id}}/small/{{.Title}}.jpg" alt="{{.Title}}" />{{end}}</p>
<p class="pull-right">{{if .Cover}}<img src="/cover/{{.ID}}/small/{{.Title}}.jpg" alt="{{.Title}}" />{{end}}</p>
</div>
<div class="span9">
<p><a href="/search/?q=title:{{.Title}}"><strong>{{.Title}}</strong></a> <small>({{$titleFound}})</small><br />
@ -45,14 +45,14 @@
</div>
<div class="span2">
<div class="row btn-group pull-right">
<a href="/store/{{.Id}}/" class="btn btn-success"><i class="icon-ok"></i> Save</a>
<a href="/edit/{{.Id}}" class="btn btn-primary"><i class="icon-pencil"></i> Edit</a>
<a href="/delete/{{.Id}}/" class="btn btn-danger"><i class="icon-remove"></i> Delete</a>
<a href="/store/{{.ID}}/" class="btn btn-success"><i class="icon-ok"></i> Save</a>
<a href="/edit/{{.ID}}" class="btn btn-primary"><i class="icon-pencil"></i> Edit</a>
<a href="/delete/{{.ID}}/" class="btn btn-danger"><i class="icon-remove"></i> Delete</a>
</div>
<div class="row"><p></p></div>
<div class="row btn-group pull-right">
<a href="/download/{{.Id}}/{{.Title}}.epub" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.Id}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
<a href="/download/{{.ID}}/{{.Title}}.epub" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
</div>
</div>
@ -72,8 +72,8 @@
</ul>
{{if .Books}}
<div class="centered btn-group">
<a href="/store/{{range .Books}}{{.B.Id}}/{{end}}" class="btn btn-large btn-success"><i class="icon-ok"></i> Save All</a>
<a href="/delete/{{range .Books}}{{.B.Id}}/{{end}}" class="btn btn-large btn-danger"><i class="icon-remove"></i> Delete All</a>
<a href="/store/{{range .Books}}{{.B.ID}}/{{end}}" class="btn btn-large btn-success"><i class="icon-ok"></i> Save All</a>
<a href="/delete/{{range .Books}}{{.B.ID}}/{{end}}" class="btn btn-large btn-danger"><i class="icon-remove"></i> Delete All</a>
</div>
{{end}}

View file

@ -21,22 +21,22 @@
{{range .}}
<div class="row">
<div class="span1">
<p class="pull-right"><a href="/book/{{.Id}}">{{if .Cover}}<img class="img-rounded" src="/cover/{{.Id}}/small/{{.Title}}.jpg" alt="{{.Title}}" />{{end}}</a></p>
<p class="pull-right"><a href="/book/{{.ID}}">{{if .Cover}}<img class="img-rounded" src="/cover/{{.ID}}/small/{{.Title}}.jpg" alt="{{.Title}}" />{{end}}</a></p>
</div>
<div class="span10 well">
<div class="row">
<div class="span7">
<p>
<span class="muted">[{{if .Lang}}{{.Lang}}{{end}}]</span>
<a href="/book/{{.Id}}"><strong>{{.Title}}</strong></a>
<a href="/book/{{.ID}}"><strong>{{.Title}}</strong></a>
<span class="muted">{{if .Publisher}}{{.Publisher}}{{end}}</span><br />
{{range .Authors}}{{.}}, {{end}}
</p>
</div>
<div class="span3">
<div class="btn-group pull-right">
<a href="/download/{{.Id}}/{{.Title}}.epub" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.Id}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
<a href="/download/{{.ID}}/{{.Title}}.epub" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
</div>
</div>

View file

@ -58,7 +58,7 @@
{{range .Books}}
<entry>
<title>{{html .Title}}</title>
<id>{{$baseurl}}/book/{{.Id}}</id>
<id>{{$baseurl}}/book/{{.ID}}</id>
<updated>{{$updated}}</updated>
{{range .Authors}}
@ -89,11 +89,11 @@
{{end}}
<summary>{{html .Description}}</summary>
<link type="image/jpeg" href="/cover/{{.Id}}/big/cover.jpg" rel="http://opds-spec.org/image"/>
<link type="image/jpg" href="/cover/{{.Id}}/small/thumbnail.jpg" rel="http://opds-spec.org/image/thumbnail" />
<link type="image/jpeg" href="/cover/{{.ID}}/big/cover.jpg" rel="http://opds-spec.org/image"/>
<link type="image/jpg" href="/cover/{{.ID}}/small/thumbnail.jpg" rel="http://opds-spec.org/image/thumbnail" />
<link rel="http://opds-spec.org/acquisition"
href="/download/{{.Id}}/{{urlquery .Title}}.epub"
href="/download/{{.ID}}/{{urlquery .Title}}.epub"
type="application/epub+zip" />
</entry>
{{end}}

View file

@ -16,11 +16,11 @@
<item>
<title>{{.Title}} - {{index .Authors 0}}</title>
<description>{{.Description}}</description>
<link>{{$baseURL}}/book/{{.Id}}</link>
<link>{{$baseURL}}/book/{{.ID}}</link>
{{if .Isbn}}
<guid isPermaLink="false">ISBN: {{.Isbn}}</guid>
{{end}}
<enclosure url="{{$baseURL}}/download/{{.Id}}/{{.Title}}.epub" length="{{.FileSize}}" type="application/epub+zip" />
<enclosure url="{{$baseURL}}/download/{{.ID}}/{{.Title}}.epub" length="{{.FileSize}}" type="application/epub+zip" />
{{range .Authors}}
{{if .}}
<category>{{.}}</category>