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

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Jun 26, 2017
1 parent a38891c commit 493281a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
15 changes: 15 additions & 0 deletions html.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ func bookHTML(b *Book, isCard bool) string {
return html.String()
}

func bookListPageHTML(books []Book, title string, notfoundtext string) (html string, notfound bool) {
if len(books) == 0 {
return pageHTML("Not Found", notfoundtext), true
}

var booksHTML bytes.Buffer
booksHTML.WriteString(`<div class="books cards">`)
for _, b := range books {
booksHTML.WriteString(bookHTML(&b, true))
}
booksHTML.WriteString(`</div>`)

return pageHTML(title, booksHTML.String()), false
}

func itemCardHTML(title string, description string, link string) string {
var html bytes.Buffer
html.WriteString(`<a class="item card" href="` + link + `">`)
Expand Down
55 changes: 27 additions & 28 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,27 @@ func AuthorsHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, pageHTML("Authors", listHTML.String()))
return
}

w.Header().Set("Content-Type", "text/html")
var booksHTML bytes.Buffer
booksHTML.WriteString(`<div class="books cards">`)

matched := sortedBookList(books, func(book Book) bool {
return book.AuthorID == aid
}, func(a Book, b Book) bool {
return a.Title < b.Title
})
if len(matched) == 0 {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, pageHTML("Not Found", "Could not find author with id "+aid))
return

aname := ""
if len(matched) != 0 {
aname = matched[0].Author
}
aname := matched[0].Author
for _, b := range matched {
booksHTML.WriteString(bookHTML(&b, true))

html, notfound := bookListPageHTML(matched, aname, "Author not found")

if notfound {
w.WriteHeader(http.StatusNotFound)
}

booksHTML.WriteString(`</div>`)
io.WriteString(w, pageHTML(aname, booksHTML.String()))
io.WriteString(w, html)
}

// SeriesHandler handles the series page
Expand Down Expand Up @@ -178,26 +178,25 @@ func SeriesHandler(w http.ResponseWriter, r *http.Request) {
}

w.Header().Set("Content-Type", "text/html")
var booksHTML bytes.Buffer
booksHTML.WriteString(`<div class="books cards">`)

matched := sortedBookList(books, func(book Book) bool {
return book.Series.ID == sid
}, func(a Book, b Book) bool {
return a.Series.Index < b.Series.Index
})
if len(matched) == 0 {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, pageHTML("Not Found", "Could not find series with id "+sid))
return

sname := ""
if len(matched) != 0 {
sname = matched[0].Series.Name
}
sname := matched[0].Series.Name
for _, b := range matched {
booksHTML.WriteString(bookHTML(&b, true))

html, notfound := bookListPageHTML(matched, sname, "Series not found")

if notfound {
w.WriteHeader(http.StatusNotFound)
}

booksHTML.WriteString(`</div>`)
io.WriteString(w, pageHTML(sname, booksHTML.String()))
io.WriteString(w, html)
}

// BooksHandler handles the books page
Expand All @@ -206,20 +205,20 @@ func BooksHandler(w http.ResponseWriter, r *http.Request) {

if bid == "books" {
w.Header().Set("Content-Type", "text/html")
var booksHTML bytes.Buffer
booksHTML.WriteString(`<div class="books cards">`)

matched := sortedBookList(books, func(book Book) bool {
return true
}, func(a Book, b Book) bool {
return a.ModTime.Unix() > b.ModTime.Unix()
})
for _, b := range matched {
booksHTML.WriteString(bookHTML(&b, true))

html, notfound := bookListPageHTML(matched, "Books", "There are no books in your library.")

if notfound {
w.WriteHeader(http.StatusNotFound)
}

booksHTML.WriteString(`</div>`)
io.WriteString(w, pageHTML("Books", booksHTML.String()))
io.WriteString(w, html)
return
}

Expand Down

0 comments on commit 493281a

Please sign in to comment.