@@ -44,7 +44,20 @@ func sortedBookPropertyList(books []Book, getNameID func(Book) nameID, filterNam
4444 }
4545 }
4646 sort .Slice (filteredItems , func (i , j int ) bool {
47- return sortNameID (items [i ], items [j ])
47+ return sortNameID (filteredItems [i ], filteredItems [j ])
48+ })
49+ return filteredItems
50+ }
51+
52+ func sortedBookList (books []Book , filterBook func (Book ) bool , sortBook func (Book , Book ) bool ) []Book {
53+ filteredItems := []Book {}
54+ for _ , book := range books {
55+ if filterBook (book ) {
56+ filteredItems = append (filteredItems , book )
57+ }
58+ }
59+ sort .Slice (filteredItems , func (i , j int ) bool {
60+ return sortBook (filteredItems [i ], filteredItems [j ])
4861 })
4962 return filteredItems
5063}
@@ -111,31 +124,26 @@ func AuthorsHandler(w http.ResponseWriter, r *http.Request) {
111124 io .WriteString (w , pageHTML ("Authors" , listHTML .String ()))
112125 return
113126 }
114-
115- found := false
116127 w .Header ().Set ("Content-Type" , "text/html" )
117128 var booksHTML bytes.Buffer
118129 booksHTML .WriteString (`<div class="books cards">` )
119- aname := ""
120- matched := []Book {}
121- for _ , b := range books {
122- if b .AuthorID == aid {
123- aname = b .Author
124- matched = append (matched , b )
125- found = true
126- }
127- }
128- sort .Slice (matched , func (i , j int ) bool {
129- return matched [i ].Title < matched [j ].Title
130+
131+ matched := sortedBookList (books , func (book Book ) bool {
132+ return book .AuthorID == aid
133+ }, func (a Book , b Book ) bool {
134+ return a .Title < b .Title
130135 })
136+ if len (matched ) == 0 {
137+ w .WriteHeader (http .StatusNotFound )
138+ io .WriteString (w , pageHTML ("Not Found" , "Could not find author with id " + aid ))
139+ return
140+ }
141+ aname := matched [0 ].Author
131142 for _ , b := range matched {
132143 booksHTML .WriteString (bookHTML (& b , true ))
133144 }
145+
134146 booksHTML .WriteString (`</div>` )
135- if found != true {
136- w .WriteHeader (http .StatusNotFound )
137- io .WriteString (w , pageHTML ("Not Found" , "Could not find author with id " + aid ))
138- }
139147 io .WriteString (w , pageHTML (aname , booksHTML .String ()))
140148}
141149
@@ -169,30 +177,26 @@ func SeriesHandler(w http.ResponseWriter, r *http.Request) {
169177 return
170178 }
171179
172- found := false
173180 w .Header ().Set ("Content-Type" , "text/html" )
174181 var booksHTML bytes.Buffer
175182 booksHTML .WriteString (`<div class="books cards">` )
176- sname := ""
177- matched := []Book {}
178- for _ , b := range books {
179- if b .Series .ID == sid {
180- sname = b .Series .Name
181- matched = append (matched , b )
182- found = true
183- }
184- }
185- sort .Slice (matched , func (i , j int ) bool {
186- return matched [i ].Series .Index < matched [j ].Series .Index
183+
184+ matched := sortedBookList (books , func (book Book ) bool {
185+ return book .Series .ID == sid
186+ }, func (a Book , b Book ) bool {
187+ return a .Series .Index < b .Series .Index
187188 })
189+ if len (matched ) == 0 {
190+ w .WriteHeader (http .StatusNotFound )
191+ io .WriteString (w , pageHTML ("Not Found" , "Could not find series with id " + sid ))
192+ return
193+ }
194+ sname := matched [0 ].Series .Name
188195 for _ , b := range matched {
189196 booksHTML .WriteString (bookHTML (& b , true ))
190197 }
198+
191199 booksHTML .WriteString (`</div>` )
192- if found != true {
193- w .WriteHeader (http .StatusNotFound )
194- io .WriteString (w , pageHTML ("Not Found" , "Could not find series with id " + sid ))
195- }
196200 io .WriteString (w , pageHTML (sname , booksHTML .String ()))
197201}
198202
@@ -204,16 +208,16 @@ func BooksHandler(w http.ResponseWriter, r *http.Request) {
204208 w .Header ().Set ("Content-Type" , "text/html" )
205209 var booksHTML bytes.Buffer
206210 booksHTML .WriteString (`<div class="books cards">` )
207- matched := []Book {}
208- for _ , b := range books {
209- matched = append (matched , b )
210- }
211- sort .Slice (matched , func (i , j int ) bool {
212- return matched [i ].ModTime .Unix () > matched [j ].ModTime .Unix ()
211+
212+ matched := sortedBookList (books , func (book Book ) bool {
213+ return true
214+ }, func (a Book , b Book ) bool {
215+ return a .ModTime .Unix () > b .ModTime .Unix ()
213216 })
214217 for _ , b := range matched {
215218 booksHTML .WriteString (bookHTML (& b , true ))
216219 }
220+
217221 booksHTML .WriteString (`</div>` )
218222 io .WriteString (w , pageHTML ("Books" , booksHTML .String ()))
219223 return
0 commit comments