Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Refactored book author
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Sep 2, 2017
1 parent 1f867ec commit eaef72a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
29 changes: 19 additions & 10 deletions book.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@ import (
"golang.org/x/tools/godoc/vfs/zipfs"
)

// NameID represents a name and an id
type NameID struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
}

// Series represents a book series
type Series struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
NameID
Index float64 `json:"index,omitempty"`
}

// Author represents a book author
type Author struct {
NameID
}

// Book represents a book
type Book struct {
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author,omitempty"`
AuthorID string `json:"authorid"`
Author Author `json:"author"`
Publisher string `json:"publisher,omitempty"`
Description string `json:"description,omitempty"`
Series Series `json:"series,omitempty"`
Expand Down Expand Up @@ -73,12 +82,12 @@ func NewBookFromFile(path, coverpath string) (bk *Book, err error) {
m, err := GetPDFMeta(path)
if err == nil {
book.Title = m.Title
book.Author = m.Author
book.Author.Name = m.Author
}

id := sha1.New()
io.WriteString(id, book.Author)
book.AuthorID = hex.EncodeToString(id.Sum(nil))[:10]
io.WriteString(id, book.Author.Name)
book.Author.ID = hex.EncodeToString(id.Sum(nil))[:10]
io.WriteString(id, book.Series.Name)
io.WriteString(id, book.Title)
book.ID = hex.EncodeToString(id.Sum(nil))[:10]
Expand Down Expand Up @@ -125,7 +134,7 @@ func NewBookFromFile(path, coverpath string) (bk *Book, err error) {
break
}
for _, e := range opf.FindElements("//creator") {
book.Author = e.Text()
book.Author.Name = e.Text()
break
}
for _, e := range opf.FindElements("//publisher") {
Expand All @@ -152,8 +161,8 @@ func NewBookFromFile(path, coverpath string) (bk *Book, err error) {
}

id := sha1.New()
io.WriteString(id, book.Author)
book.AuthorID = hex.EncodeToString(id.Sum(nil))[:10]
io.WriteString(id, book.Author.Name)
book.Author.ID = hex.EncodeToString(id.Sum(nil))[:10]
io.WriteString(id, book.Series.Name)
io.WriteString(id, book.Title)
book.ID = hex.EncodeToString(id.Sum(nil))[:10]
Expand Down
6 changes: 3 additions & 3 deletions html.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ func bookHTML(b *Book, isInfo bool) string {
html.WriteString(`<a class="title" href="/books/` + b.ID + `">`)
html.WriteString(b.Title)
html.WriteString(`</a>`)
if b.Author != "" {
html.WriteString(`<a class="author" href="/authors/` + b.AuthorID + `">`)
html.WriteString(b.Author)
if b.Author.Name != "" {
html.WriteString(`<a class="author" href="/authors/` + b.Author.ID + `">`)
html.WriteString(b.Author.Name)
html.WriteString(`</a>`)
}
if b.Series.Name != "" {
Expand Down
41 changes: 15 additions & 26 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,24 @@ func (s *Server) initRouter() {
s.router.ServeFiles("/covers/*filepath", http.Dir(s.CoverDir))
}

type nameID struct {
Name string
ID string
}

func (s *Server) sortedBookPropertyList(getNameID func(Book) nameID, filterNameID func(nameID) bool, sortNameID func(nameID, nameID) bool) []nameID {
func (s *Server) sortedBookPropertyList(getNameID func(Book) NameID, filterNameID func(NameID) bool, sortNameID func(NameID, NameID) bool) []NameID {
s.booksLock.RLock()
defer s.booksLock.RUnlock()

doneItems := map[string]bool{}
items := []nameID{}
items := []NameID{}
for _, b := range *s.Books {
nid := getNameID(b)
if doneItems[nid.ID] {
continue
}
doneItems[nid.ID] = true
items = append(items, nameID{
items = append(items, NameID{
Name: nid.Name,
ID: nid.ID,
})
}
filteredItems := []nameID{}
filteredItems := []NameID{}
for _, ni := range items {
if filterNameID(ni) {
filteredItems = append(filteredItems, ni)
Expand Down Expand Up @@ -311,14 +306,11 @@ func (s *Server) handleAuthorList(w http.ResponseWriter, r *http.Request, _ http
w.Header().Set("Content-Type", "text/html")
var listHTML bytes.Buffer

authors := s.sortedBookPropertyList(func(b Book) nameID {
return nameID{
Name: b.Author,
ID: b.AuthorID,
}
}, func(ni nameID) bool {
authors := s.sortedBookPropertyList(func(b Book) NameID {
return b.Author.NameID
}, func(ni NameID) bool {
return ni.Name != ""
}, func(a nameID, b nameID) bool {
}, func(a NameID, b NameID) bool {
return a.Name < b.Name
})
listHTML.WriteString(`<div class="items view cards">`)
Expand All @@ -339,14 +331,14 @@ func (s *Server) handleAuthor(w http.ResponseWriter, r *http.Request, p httprout
w.Header().Set("Content-Type", "text/html")

matched := s.sortedBookList(func(book Book) bool {
return book.AuthorID == aid
return book.Author.ID == aid
}, func(a Book, b Book) bool {
return a.Title < b.Title
})

aname := ""
if len(matched) != 0 {
aname = matched[0].Author
aname = matched[0].Author.Name
}

html, notfound := bookListPageHTML(matched, aname, "Author not found", false)
Expand All @@ -365,14 +357,11 @@ func (s *Server) handleSeriesList(w http.ResponseWriter, r *http.Request, _ http
w.Header().Set("Content-Type", "text/html")
var listHTML bytes.Buffer

series := s.sortedBookPropertyList(func(b Book) nameID {
return nameID{
Name: b.Series.Name,
ID: b.Series.ID,
}
}, func(ni nameID) bool {
series := s.sortedBookPropertyList(func(b Book) NameID {
return b.Series.NameID
}, func(ni NameID) bool {
return ni.Name != ""
}, func(a nameID, b nameID) bool {
}, func(a NameID, b NameID) bool {
return a.Name < b.Name
})
listHTML.WriteString(`<div class="items view cards">`)
Expand Down Expand Up @@ -467,7 +456,7 @@ func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request, _ httprout
matched := false
for _, b := range *s.Books {
matches := false
matches = matches || strings.Contains(strings.ToLower(b.Author), ql)
matches = matches || strings.Contains(strings.ToLower(b.Author.Name), ql)
matches = matches || strings.Contains(strings.ToLower(b.Title), ql)
matches = matches || strings.Contains(strings.ToLower(b.Series.Name), ql)

Expand Down

0 comments on commit eaef72a

Please sign in to comment.