Merge pull request #60 from stevedylandev/feat/library-add-quotes 0739a7a0
feat(library): add quotes feature
Steve Simkins · 2026-10-29 14:17 15 file(s) · +1249 −19
apps/library/app.go +66 −8
27 27
}
28 28
29 29
type App struct {
30 -
	DB             *sql.DB
31 -
	Log            *slog.Logger
32 -
	Templates      *template.Template
33 -
	Sessions       *auth.Store
34 -
	AdminPassword  string
35 -
	CookieSecure   bool
36 -
	BaseURL        string
37 -
	DisplayMode    DisplayMode
30 +
	DB            *sql.DB
31 +
	Log           *slog.Logger
32 +
	Templates     *template.Template
33 +
	Sessions      *auth.Store
34 +
	AdminPassword string
35 +
	CookieSecure  bool
36 +
	BaseURL       string
37 +
	DisplayMode   DisplayMode
38 38
}
39 39
40 40
type bookView struct {
41 +
	ID       int64
41 42
	Title    string
42 43
	Authors  string
44 +
	ISBN     string
43 45
	CoverURL string
44 46
	Notes    string
45 47
}
46 48
49 +
type quoteView struct {
50 +
	ID     int64
51 +
	BookID int64
52 +
	Page   string
53 +
	Quote  string
54 +
}
55 +
56 +
type quoteGroup struct {
57 +
	BookID      int64
58 +
	BookTitle   string
59 +
	BookAuthors string
60 +
	Quotes      []quoteView
61 +
}
62 +
47 63
type sectionView struct {
48 64
	Label string
49 65
	Books []bookView
66 82
	Error string
67 83
}
68 84
85 +
type bookPageData struct {
86 +
	BaseURL string
87 +
	Book    bookView
88 +
	Quotes  []quoteView
89 +
}
90 +
91 +
type quotesPageData struct {
92 +
	BaseURL string
93 +
	Groups  []quoteGroup
94 +
}
95 +
96 +
type quotePageData struct {
97 +
	BaseURL string
98 +
	Quote   quoteView
99 +
	Book    bookView
100 +
}
101 +
69 102
type adminBookRow struct {
70 103
	ID       int64
71 104
	Title    string
74 107
	CoverURL string
75 108
	Notes    string
76 109
	Status   string
110 +
}
111 +
112 +
type adminQuoteRow struct {
113 +
	ID          int64
114 +
	BookID      int64
115 +
	Page        string
116 +
	Quote       string
117 +
	BookTitle   string
118 +
	BookAuthors string
119 +
}
120 +
121 +
type bookWithQuotesPageData struct {
122 +
	BaseURL string
123 +
	Success string
124 +
	Error   string
125 +
	Book    adminBookRow
126 +
	Quotes  []adminQuoteRow
127 +
}
128 +
129 +
type adminQuotesPageData struct {
130 +
	BaseURL string
131 +
	Success string
132 +
	Error   string
133 +
	Quotes  []adminQuoteRow
134 +
	Books   []adminBookRow
77 135
}
78 136
79 137
type adminPageData struct {
apps/library/db.go +196 −0
23 23
);
24 24
CREATE INDEX IF NOT EXISTS idx_books_status_added ON books(status, added_at DESC);
25 25
26 +
CREATE TABLE IF NOT EXISTS quotes (
27 +
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
28 +
    book_id    INTEGER NOT NULL REFERENCES books(id) ON DELETE CASCADE,
29 +
    page       TEXT,
30 +
    quote      TEXT NOT NULL,
31 +
    public     INTEGER NOT NULL DEFAULT 1,
32 +
    added_at   INTEGER NOT NULL,
33 +
    updated_at INTEGER NOT NULL
34 +
);
35 +
CREATE INDEX IF NOT EXISTS idx_quotes_book ON quotes(book_id, page);
36 +
26 37
CREATE TABLE IF NOT EXISTS settings (
27 38
    key   TEXT PRIMARY KEY,
28 39
    value TEXT NOT NULL
170 181
	return err
171 182
}
172 183
184 +
func updateBookDetails(db *sql.DB, id int64, title, authors, isbn, coverURL, notes string) error {
185 +
	optional := func(s string) any {
186 +
		if strings.TrimSpace(s) == "" {
187 +
			return nil
188 +
		}
189 +
		return s
190 +
	}
191 +
	_, err := db.Exec(
192 +
		`UPDATE books SET title = ?, authors = ?, isbn = ?, cover_url = ?, notes = ?, updated_at = ? WHERE id = ?`,
193 +
		title, authors, optional(isbn), optional(coverURL), optional(notes), time.Now().UTC().Unix(), id,
194 +
	)
195 +
	return err
196 +
}
197 +
173 198
func deleteBook(db *sql.DB, id int64) error {
174 199
	_, err := db.Exec(`DELETE FROM books WHERE id = ?`, id)
175 200
	return err
250 275
func validStatus(s string) bool {
251 276
	return s == "read" || s == "reading" || s == "want"
252 277
}
278 +
279 +
const quoteCols = `id, book_id, page, quote, public, added_at, updated_at`
280 +
281 +
type Quote struct {
282 +
	ID        int64   `json:"id"`
283 +
	BookID    int64   `json:"book_id"`
284 +
	Page      *string `json:"page,omitempty"`
285 +
	Quote     string  `json:"quote"`
286 +
	Public    bool    `json:"public"`
287 +
	AddedAt   int64   `json:"added_at"`
288 +
	UpdatedAt int64   `json:"updated_at"`
289 +
}
290 +
291 +
type NewQuote struct {
292 +
	BookID int64
293 +
	Page   *string
294 +
	Quote  string
295 +
	Public *bool // nil → true
296 +
}
297 +
298 +
type QuoteWithBook struct {
299 +
	ID          int64   `json:"id"`
300 +
	BookID      int64   `json:"book_id"`
301 +
	Page        *string `json:"page,omitempty"`
302 +
	Quote       string  `json:"quote"`
303 +
	Public      bool    `json:"public"`
304 +
	AddedAt     int64   `json:"added_at"`
305 +
	UpdatedAt   int64   `json:"updated_at"`
306 +
	BookTitle   string  `json:"book_title,omitempty"`
307 +
	BookAuthors string  `json:"book_authors,omitempty"`
308 +
}
309 +
310 +
func scanQuote(s interface{ Scan(...any) error }) (*Quote, error) {
311 +
	var q Quote
312 +
	var page sql.NullString
313 +
	var public bool
314 +
	err := s.Scan(&q.ID, &q.BookID, &page, &q.Quote, &public, &q.AddedAt, &q.UpdatedAt)
315 +
	if errors.Is(err, sql.ErrNoRows) {
316 +
		return nil, nil
317 +
	}
318 +
	if err != nil {
319 +
		return nil, err
320 +
	}
321 +
	q.Public = public
322 +
	if page.Valid {
323 +
		v := page.String
324 +
		q.Page = &v
325 +
	}
326 +
	return &q, nil
327 +
}
328 +
329 +
func insertQuote(db *sql.DB, q NewQuote) (int64, error) {
330 +
	now := time.Now().UTC().Unix()
331 +
	var page any
332 +
	if q.Page != nil && *q.Page != "" {
333 +
		page = *q.Page
334 +
	}
335 +
	public := true
336 +
	if q.Public != nil {
337 +
		public = *q.Public
338 +
	}
339 +
	res, err := db.Exec(
340 +
		`INSERT INTO quotes (book_id, page, quote, public, added_at, updated_at)
341 +
		 VALUES (?, ?, ?, ?, ?, ?)`,
342 +
		q.BookID, page, q.Quote, public, now, now,
343 +
	)
344 +
	if err != nil {
345 +
		return 0, err
346 +
	}
347 +
	return res.LastInsertId()
348 +
}
349 +
350 +
func updateQuote(db *sql.DB, id int64, q NewQuote) error {
351 +
	var page any
352 +
	if q.Page != nil && *q.Page != "" {
353 +
		page = *q.Page
354 +
	}
355 +
	public := true
356 +
	if q.Public != nil {
357 +
		public = *q.Public
358 +
	}
359 +
	_, err := db.Exec(
360 +
		`UPDATE quotes SET book_id = ?, page = ?, quote = ?, public = ?, updated_at = ? WHERE id = ?`,
361 +
		q.BookID, page, q.Quote, public, time.Now().UTC().Unix(), id,
362 +
	)
363 +
	return err
364 +
}
365 +
366 +
func deleteQuote(db *sql.DB, id int64) error {
367 +
	_, err := db.Exec(`DELETE FROM quotes WHERE id = ?`, id)
368 +
	return err
369 +
}
370 +
371 +
func getQuote(db *sql.DB, id int64) (*Quote, error) {
372 +
	return scanQuote(db.QueryRow(`SELECT `+quoteCols+` FROM quotes WHERE id = ?`, id))
373 +
}
374 +
375 +
func listQuotesForBook(db *sql.DB, bookID int64, publicOnly bool) ([]Quote, error) {
376 +
	q := `SELECT ` + quoteCols + ` FROM quotes WHERE book_id = ?`
377 +
	if publicOnly {
378 +
		q += ` AND public = 1`
379 +
	}
380 +
	q += ` ORDER BY page ASC, added_at ASC`
381 +
	rows, err := db.Query(q, bookID)
382 +
	if err != nil {
383 +
		return nil, err
384 +
	}
385 +
	defer rows.Close()
386 +
	var out []Quote
387 +
	for rows.Next() {
388 +
		q, err := scanQuote(rows)
389 +
		if err != nil {
390 +
			return nil, err
391 +
		}
392 +
		out = append(out, *q)
393 +
	}
394 +
	return out, rows.Err()
395 +
}
396 +
397 +
func listQuotes(db *sql.DB, publicOnly bool) ([]QuoteWithBook, error) {
398 +
	q := `SELECT q.id, q.book_id, q.page, q.quote, q.public, q.added_at, q.updated_at, b.title, b.authors
399 +
	      FROM quotes q JOIN books b ON b.id = q.book_id`
400 +
	if publicOnly {
401 +
		q += ` WHERE q.public = 1`
402 +
	}
403 +
	q += ` ORDER BY b.title COLLATE NOCASE ASC, q.page ASC, q.added_at ASC`
404 +
	rows, err := db.Query(q)
405 +
	if err != nil {
406 +
		return nil, err
407 +
	}
408 +
	defer rows.Close()
409 +
	var out []QuoteWithBook
410 +
	for rows.Next() {
411 +
		var qb QuoteWithBook
412 +
		var page sql.NullString
413 +
		var public bool
414 +
		err := rows.Scan(&qb.ID, &qb.BookID, &page, &qb.Quote, &public, &qb.AddedAt, &qb.UpdatedAt, &qb.BookTitle, &qb.BookAuthors)
415 +
		if err != nil {
416 +
			return nil, err
417 +
		}
418 +
		qb.Public = public
419 +
		if page.Valid {
420 +
			v := page.String
421 +
			qb.Page = &v
422 +
		}
423 +
		out = append(out, qb)
424 +
	}
425 +
	return out, rows.Err()
426 +
}
427 +
428 +
func getQuoteWithBook(db *sql.DB, id int64) (*QuoteWithBook, error) {
429 +
	var qb QuoteWithBook
430 +
	var page sql.NullString
431 +
	var public bool
432 +
	err := db.QueryRow(
433 +
		`SELECT q.id, q.book_id, q.page, q.quote, q.public, q.added_at, q.updated_at, b.title, b.authors
434 +
		 FROM quotes q JOIN books b ON b.id = q.book_id WHERE q.id = ?`, id,
435 +
	).Scan(&qb.ID, &qb.BookID, &page, &qb.Quote, &public, &qb.AddedAt, &qb.UpdatedAt, &qb.BookTitle, &qb.BookAuthors)
436 +
	if errors.Is(err, sql.ErrNoRows) {
437 +
		return nil, nil
438 +
	}
439 +
	if err != nil {
440 +
		return nil, err
441 +
	}
442 +
	qb.Public = public
443 +
	if page.Valid {
444 +
		v := page.String
445 +
		qb.Page = &v
446 +
	}
447 +
	return &qb, nil
448 +
}
apps/library/db_test.go +118 −0
73 73
		t.Fatal("isbn char validation failed")
74 74
	}
75 75
}
76 +
77 +
func TestQuoteCRUDFilterAndCascade(t *testing.T) {
78 +
	db := openLibraryTestDB(t)
79 +
	defer db.Close()
80 +
81 +
	duneID, err := insertBook(db, NewBook{Title: "Dune", Authors: "Frank Herbert", Status: "read"})
82 +
	if err != nil {
83 +
		t.Fatal(err)
84 +
	}
85 +
	emmaID, err := insertBook(db, NewBook{Title: "Emma", Authors: "Jane Austen", Status: "read"})
86 +
	if err != nil {
87 +
		t.Fatal(err)
88 +
	}
89 +
90 +
	// Create (defaults to public).
91 +
	qid, err := insertQuote(db, NewQuote{BookID: duneID, Page: sp("12"), Quote: "Fear is the mind-killer."})
92 +
	if err != nil {
93 +
		t.Fatal(err)
94 +
	}
95 +
	q, err := getQuote(db, qid)
96 +
	if err != nil || q == nil || q.BookID != duneID || q.Quote != "Fear is the mind-killer." || !q.Public {
97 +
		t.Fatalf("get quote %#v err %v", q, err)
98 +
	}
99 +
	if q.Page == nil || *q.Page != "12" {
100 +
		t.Fatalf("page %#v", q.Page)
101 +
	}
102 +
103 +
	// Edit (all fields incl. book and page clearing).
104 +
	if err := updateQuote(db, qid, NewQuote{BookID: emmaID, Quote: "Changed quote."}); err != nil {
105 +
		t.Fatal(err)
106 +
	}
107 +
	q, _ = getQuote(db, qid)
108 +
	if q == nil || q.BookID != emmaID || q.Quote != "Changed quote." || q.Page != nil {
109 +
		t.Fatalf("updated quote %#v", q)
110 +
	}
111 +
112 +
	// Private quotes are excluded from public listing.
113 +
	private := false
114 +
	privID, err := insertQuote(db, NewQuote{BookID: emmaID, Quote: "secret quote.", Public: &private})
115 +
	if err != nil {
116 +
		t.Fatal(err)
117 +
	}
118 +
	if got, err := listQuotes(db, true); err != nil || len(got) != 1 || !got[0].Public {
119 +
		t.Fatalf("public list %#v err %v", got, err)
120 +
	}
121 +
	all, err := listQuotes(db, false)
122 +
	if err != nil || len(all) != 2 {
123 +
		t.Fatalf("all list %#v err %v", all, err)
124 +
	}
125 +
	// Joined metadata should come along.
126 +
	for _, qb := range all {
127 +
		if qb.BookTitle == "" || qb.BookAuthors == "" {
128 +
			t.Fatalf("quote with book missing join data: %#v", qb)
129 +
		}
130 +
	}
131 +
132 +
	// listQuotesForBook filters by book and respects publicOnly.
133 +
	got, err := listQuotesForBook(db, emmaID, false)
134 +
	if err != nil || len(got) != 2 {
135 +
		t.Fatalf("book quotes %#v err %v", got, err)
136 +
	}
137 +
	pub, err := listQuotesForBook(db, emmaID, true)
138 +
	if err != nil || len(pub) != 1 || pub[0].ID != qid {
139 +
		t.Fatalf("public book quotes %#v err %v", pub, err)
140 +
	}
141 +
142 +
	// Delete.
143 +
	if err := deleteQuote(db, privID); err != nil {
144 +
		t.Fatal(err)
145 +
	}
146 +
	if got, err := getQuote(db, privID); err != nil || got != nil {
147 +
		t.Fatalf("deleted quote %#v err %v", got, err)
148 +
	}
149 +
150 +
	// Deleting a book cascades its quotes.
151 +
	if err := deleteBook(db, emmaID); err != nil {
152 +
		t.Fatal(err)
153 +
	}
154 +
	if got, err := getQuote(db, qid); err != nil || got != nil {
155 +
		t.Fatalf("quote should cascade-delete with book: %#v err %v", got, err)
156 +
	}
157 +
}
158 +
159 +
func TestQuoteOrdering(t *testing.T) {
160 +
	db := openLibraryTestDB(t)
161 +
	defer db.Close()
162 +
163 +
	duneID, err := insertBook(db, NewBook{Title: "Dune", Authors: "Frank Herbert", Status: "read"})
164 +
	if err != nil {
165 +
		t.Fatal(err)
166 +
	}
167 +
	emmaID, err := insertBook(db, NewBook{Title: "Emma", Authors: "Jane Austen", Status: "read"})
168 +
	if err != nil {
169 +
		t.Fatal(err)
170 +
	}
171 +
	if _, err := insertQuote(db, NewQuote{BookID: emmaID, Quote: "Emma quote "}); err != nil {
172 +
		t.Fatal(err)
173 +
	}
174 +
	if _, err := insertQuote(db, NewQuote{BookID: duneID, Page: sp("1"), Quote: "dune p1 "}); err != nil {
175 +
		t.Fatal(err)
176 +
	}
177 +
	if _, err := insertQuote(db, NewQuote{BookID: duneID, Page: sp("2"), Quote: "dune p2 "}); err != nil {
178 +
		t.Fatal(err)
179 +
	}
180 +
181 +
	all, err := listQuotes(db, false)
182 +
	if err != nil || len(all) != 3 {
183 +
		t.Fatalf("list %#v err %v", all, err)
184 +
	}
185 +
	// Books grouped alphabetically: Dune before Emma.
186 +
	if all[0].BookTitle != "Dune" || all[2].BookTitle != "Emma" {
187 +
		t.Fatalf("group order wrong: %#v", all)
188 +
	}
189 +
	// Within a book, page order ascending.
190 +
	if all[0].Page == nil || *all[0].Page != "1" || all[1].Page == nil || *all[1].Page != "2" {
191 +
		t.Fatalf("page order wrong: %#v", all)
192 +
	}
193 +
}
apps/library/handlers_api.go +55 −0
2 2
3 3
import (
4 4
	"net/http"
5 +
	"strconv"
5 6
6 7
	"github.com/stevedylandev/andromeda/pkg/web"
7 8
)
47 48
	}
48 49
	web.WriteJSON(w, http.StatusOK, b)
49 50
}
51 +
52 +
func (a *App) apiListQuotes(w http.ResponseWriter, r *http.Request) {
53 +
	bookID := int64(0)
54 +
	if v := r.URL.Query().Get("book_id"); v != "" {
55 +
		id, err := strconv.ParseInt(v, 10, 64)
56 +
		if err != nil || id <= 0 {
57 +
			web.WriteError(w, http.StatusBadRequest, "invalid book_id")
58 +
			return
59 +
		}
60 +
		bookID = id
61 +
	}
62 +
	if bookID != 0 {
63 +
		quotes, err := listQuotesForBook(a.DB, bookID, true)
64 +
		if err != nil {
65 +
			a.Log.Error("list quotes for book", "err", err)
66 +
			w.WriteHeader(http.StatusInternalServerError)
67 +
			return
68 +
		}
69 +
		if quotes == nil {
70 +
			quotes = []Quote{}
71 +
		}
72 +
		web.WriteJSON(w, http.StatusOK, quotes)
73 +
		return
74 +
	}
75 +
	all, err := listQuotes(a.DB, true)
76 +
	if err != nil {
77 +
		a.Log.Error("list quotes", "err", err)
78 +
		w.WriteHeader(http.StatusInternalServerError)
79 +
		return
80 +
	}
81 +
	if all == nil {
82 +
		all = []QuoteWithBook{}
83 +
	}
84 +
	web.WriteJSON(w, http.StatusOK, all)
85 +
}
86 +
87 +
func (a *App) apiGetQuote(w http.ResponseWriter, r *http.Request) {
88 +
	id, ok := web.PathInt64(r, "id")
89 +
	if !ok {
90 +
		web.WriteError(w, http.StatusBadRequest, "invalid id")
91 +
		return
92 +
	}
93 +
	q, err := getQuote(a.DB, id)
94 +
	if err != nil {
95 +
		a.Log.Error("get quote", "err", err)
96 +
		w.WriteHeader(http.StatusInternalServerError)
97 +
		return
98 +
	}
99 +
	if q == nil || !q.Public {
100 +
		web.WriteError(w, http.StatusNotFound, "not found")
101 +
		return
102 +
	}
103 +
	web.WriteJSON(w, http.StatusOK, q)
104 +
}
apps/library/handlers_web.go +328 −1
2 2
3 3
import (
4 4
	"net/http"
5 +
	"sort"
5 6
	"strconv"
6 7
	"strings"
7 8
19 20
}
20 21
21 22
func bookToView(b Book) bookView {
22 -
	v := bookView{Title: b.Title, Authors: b.Authors}
23 +
	v := bookView{ID: b.ID, Title: b.Title, Authors: b.Authors}
23 24
	if b.CoverURL != nil {
24 25
		v.CoverURL = *b.CoverURL
25 26
	}
26 27
	if b.Notes != nil {
27 28
		v.Notes = *b.Notes
29 +
	}
30 +
	if b.ISBN != nil {
31 +
		v.ISBN = *b.ISBN
28 32
	}
29 33
	return v
34 +
}
35 +
36 +
func quotesToViews(qs []Quote) []quoteView {
37 +
	out := make([]quoteView, 0, len(qs))
38 +
	for _, q := range qs {
39 +
		v := quoteView{ID: q.ID, BookID: q.BookID, Quote: q.Quote}
40 +
		if q.Page != nil {
41 +
			v.Page = *q.Page
42 +
		}
43 +
		out = append(out, v)
44 +
	}
45 +
	return out
46 +
}
47 +
48 +
func quoteToRow(q QuoteWithBook) adminQuoteRow {
49 +
	r := adminQuoteRow{ID: q.ID, BookID: q.BookID, Quote: q.Quote, BookTitle: q.BookTitle, BookAuthors: q.BookAuthors}
50 +
	if q.Page != nil {
51 +
		r.Page = *q.Page
52 +
	}
53 +
	return r
54 +
}
55 +
56 +
func quoteRow(q Quote) adminQuoteRow {
57 +
	r := adminQuoteRow{ID: q.ID, BookID: q.BookID, Quote: q.Quote}
58 +
	if q.Page != nil {
59 +
		r.Page = *q.Page
60 +
	}
61 +
	return r
62 +
}
63 +
64 +
// adminBackTo resolves the redirect target for admin quote form submissions
65 +
// via the hidden "from" field, falling back when absent or not an admin path.
66 +
func adminBackTo(r *http.Request, fallback string) string {
67 +
	from := r.FormValue("from")
68 +
	if strings.HasPrefix(from, "/admin/") {
69 +
		return from
70 +
	}
71 +
	return fallback
30 72
}
31 73
32 74
func bookToRow(b Book) adminBookRow {
261 303
	}
262 304
	web.RedirectWithSuccess(w, r, "/admin", "Book removed")
263 305
}
306 +
307 +
func (a *App) bookPageHandler(w http.ResponseWriter, r *http.Request) {
308 +
	id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
309 +
	if err != nil {
310 +
		http.NotFound(w, r)
311 +
		return
312 +
	}
313 +
	b, err := getBook(a.DB, id)
314 +
	if err != nil {
315 +
		a.Log.Error("get book", "err", err)
316 +
		http.Error(w, "Internal error", http.StatusInternalServerError)
317 +
		return
318 +
	}
319 +
	if b == nil {
320 +
		http.NotFound(w, r)
321 +
		return
322 +
	}
323 +
	quotes, err := listQuotesForBook(a.DB, id, true)
324 +
	if err != nil {
325 +
		a.Log.Error("list quotes for book", "err", err)
326 +
		http.Error(w, "Internal error", http.StatusInternalServerError)
327 +
		return
328 +
	}
329 +
	web.Render(a.Templates, w, "book.html", bookPageData{
330 +
		BaseURL: a.BaseURL,
331 +
		Book:    bookToView(*b),
332 +
		Quotes:  quotesToViews(quotes),
333 +
	}, a.Log)
334 +
}
335 +
336 +
func (a *App) quotesHandler(w http.ResponseWriter, r *http.Request) {
337 +
	all, err := listQuotes(a.DB, true)
338 +
	if err != nil {
339 +
		a.Log.Error("list quotes", "err", err)
340 +
		http.Error(w, "Internal error", http.StatusInternalServerError)
341 +
		return
342 +
	}
343 +
	data := quotesPageData{BaseURL: a.BaseURL}
344 +
	for _, qb := range all {
345 +
		var g *quoteGroup
346 +
		if len(data.Groups) > 0 {
347 +
			g = &data.Groups[len(data.Groups)-1]
348 +
		}
349 +
		if g == nil || g.BookID != qb.BookID {
350 +
			data.Groups = append(data.Groups, quoteGroup{BookID: qb.BookID, BookTitle: qb.BookTitle, BookAuthors: qb.BookAuthors})
351 +
			g = &data.Groups[len(data.Groups)-1]
352 +
		}
353 +
		qv := quoteView{ID: qb.ID, BookID: qb.BookID, Quote: qb.Quote}
354 +
		if qb.Page != nil {
355 +
			qv.Page = *qb.Page
356 +
		}
357 +
		g.Quotes = append(g.Quotes, qv)
358 +
	}
359 +
	web.Render(a.Templates, w, "quotes.html", data, a.Log)
360 +
}
361 +
362 +
func (a *App) quotePageHandler(w http.ResponseWriter, r *http.Request) {
363 +
	id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
364 +
	if err != nil {
365 +
		http.NotFound(w, r)
366 +
		return
367 +
	}
368 +
	q, err := getQuote(a.DB, id)
369 +
	if err != nil {
370 +
		a.Log.Error("get quote", "err", err)
371 +
		http.Error(w, "Internal error", http.StatusInternalServerError)
372 +
		return
373 +
	}
374 +
	if q == nil || !q.Public {
375 +
		http.NotFound(w, r)
376 +
		return
377 +
	}
378 +
	b, err := getBook(a.DB, q.BookID)
379 +
	if err != nil {
380 +
		a.Log.Error("get book", "err", err)
381 +
		http.Error(w, "Internal error", http.StatusInternalServerError)
382 +
		return
383 +
	}
384 +
	if b == nil {
385 +
		http.NotFound(w, r)
386 +
		return
387 +
	}
388 +
	data := quotePageData{BaseURL: a.BaseURL, Book: bookToView(*b)}
389 +
	qv := quoteView{ID: q.ID, BookID: q.BookID, Quote: q.Quote}
390 +
	if q.Page != nil {
391 +
		qv.Page = *q.Page
392 +
	}
393 +
	data.Quote = qv
394 +
	web.Render(a.Templates, w, "quote.html", data, a.Log)
395 +
}
396 +
397 +
func (a *App) adminEditBookHandler(w http.ResponseWriter, r *http.Request) {
398 +
	id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
399 +
	if err != nil {
400 +
		web.RedirectWithError(w, r, "/admin", "Bad id")
401 +
		return
402 +
	}
403 +
	b, err := getBook(a.DB, id)
404 +
	if err != nil {
405 +
		a.Log.Error("get book", "err", err)
406 +
		web.RedirectWithError(w, r, "/admin", "Failed to load book")
407 +
		return
408 +
	}
409 +
	if b == nil {
410 +
		web.RedirectWithError(w, r, "/admin", "Book not found")
411 +
		return
412 +
	}
413 +
	quotes, err := listQuotesForBook(a.DB, id, false)
414 +
	if err != nil {
415 +
		a.Log.Error("list quotes for book", "err", err)
416 +
		web.RedirectWithError(w, r, "/admin", "Failed to load quotes")
417 +
		return
418 +
	}
419 +
	rows := make([]adminQuoteRow, 0, len(quotes))
420 +
	for _, q := range quotes {
421 +
		rows = append(rows, quoteRow(q))
422 +
	}
423 +
	web.Render(a.Templates, w, "admin_book.html", bookWithQuotesPageData{
424 +
		BaseURL: a.BaseURL,
425 +
		Success: r.URL.Query().Get("success"),
426 +
		Error:   r.URL.Query().Get("error"),
427 +
		Book:    bookToRow(*b),
428 +
		Quotes:  rows,
429 +
	}, a.Log)
430 +
}
431 +
432 +
func (a *App) adminUpdateBook(w http.ResponseWriter, r *http.Request) {
433 +
	id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
434 +
	back := "/admin/books/" + strconv.FormatInt(id, 10)
435 +
	if err != nil {
436 +
		web.RedirectWithError(w, r, "/admin", "Bad id")
437 +
		return
438 +
	}
439 +
	if err := r.ParseForm(); err != nil {
440 +
		web.RedirectWithError(w, r, back, "Bad request")
441 +
		return
442 +
	}
443 +
	title := strings.TrimSpace(r.FormValue("title"))
444 +
	authors := strings.TrimSpace(r.FormValue("authors"))
445 +
	if title == "" || authors == "" {
446 +
		web.RedirectWithError(w, r, back, "Title and authors are required")
447 +
		return
448 +
	}
449 +
	if err := updateBookDetails(a.DB, id, title, authors, r.FormValue("isbn"), r.FormValue("cover_url"), r.FormValue("notes")); err != nil {
450 +
		a.Log.Error("update book details", "err", err)
451 +
		web.RedirectWithError(w, r, back, "Failed to save book")
452 +
		return
453 +
	}
454 +
	web.RedirectWithSuccess(w, r, back, "Book updated")
455 +
}
456 +
457 +
func (a *App) adminQuotesHandler(w http.ResponseWriter, r *http.Request) {
458 +
	all, err := listQuotes(a.DB, false)
459 +
	if err != nil {
460 +
		a.Log.Error("list quotes", "err", err)
461 +
		web.RedirectWithError(w, r, "/admin", "Failed to load quotes")
462 +
		return
463 +
	}
464 +
	rows := make([]adminQuoteRow, 0, len(all))
465 +
	for _, q := range all {
466 +
		rows = append(rows, quoteToRow(q))
467 +
	}
468 +
	books, err := listBooks(a.DB, "")
469 +
	if err != nil {
470 +
		a.Log.Error("list books", "err", err)
471 +
		web.RedirectWithError(w, r, "/admin", "Failed to load books")
472 +
		return
473 +
	}
474 +
	sort.Slice(books, func(i, j int) bool {
475 +
		return strings.ToLower(books[i].Title) < strings.ToLower(books[j].Title)
476 +
	})
477 +
	bookRows := make([]adminBookRow, 0, len(books))
478 +
	for _, b := range books {
479 +
		bookRows = append(bookRows, bookToRow(b))
480 +
	}
481 +
	web.Render(a.Templates, w, "admin_quotes.html", adminQuotesPageData{
482 +
		BaseURL: a.BaseURL,
483 +
		Success: r.URL.Query().Get("success"),
484 +
		Error:   r.URL.Query().Get("error"),
485 +
		Quotes:  rows,
486 +
		Books:   bookRows,
487 +
	}, a.Log)
488 +
}
489 +
490 +
// validQuoteForm validates and extracts quote fields shared by add and edit.
491 +
func validQuoteForm(r *http.Request) (NewQuote, string) {
492 +
	bookID, err := strconv.ParseInt(r.FormValue("book_id"), 10, 64)
493 +
	if err != nil || bookID <= 0 {
494 +
		return NewQuote{}, "Bad book"
495 +
	}
496 +
	quote := strings.TrimSpace(r.FormValue("quote"))
497 +
	if quote == "" {
498 +
		return NewQuote{}, "Quote is required"
499 +
	}
500 +
	page := strings.TrimSpace(r.FormValue("page"))
501 +
	nq := NewQuote{BookID: bookID, Quote: quote}
502 +
	if page != "" {
503 +
		nq.Page = &page
504 +
	}
505 +
	return nq, ""
506 +
}
507 +
508 +
func (a *App) adminAddQuote(w http.ResponseWriter, r *http.Request) {
509 +
	if err := r.ParseForm(); err != nil {
510 +
		web.RedirectWithError(w, r, "/admin/quotes", "Bad request")
511 +
		return
512 +
	}
513 +
	back := adminBackTo(r, "/admin/quotes")
514 +
	nq, msg := validQuoteForm(r)
515 +
	if msg != "" {
516 +
		web.RedirectWithError(w, r, back, msg)
517 +
		return
518 +
	}
519 +
	b, err := getBook(a.DB, nq.BookID)
520 +
	if err != nil {
521 +
		a.Log.Error("get book", "err", err)
522 +
		web.RedirectWithError(w, r, back, "Failed to load book")
523 +
		return
524 +
	}
525 +
	if b == nil {
526 +
		web.RedirectWithError(w, r, back, "Book not found")
527 +
		return
528 +
	}
529 +
	if _, err := insertQuote(a.DB, nq); err != nil {
530 +
		a.Log.Error("insert quote", "err", err)
531 +
		web.RedirectWithError(w, r, back, "Failed to add quote")
532 +
		return
533 +
	}
534 +
	web.RedirectWithSuccess(w, r, back, "Quote added")
535 +
}
536 +
537 +
func (a *App) adminUpdateQuote(w http.ResponseWriter, r *http.Request) {
538 +
	id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
539 +
	if err != nil {
540 +
		web.RedirectWithError(w, r, "/admin/quotes", "Bad id")
541 +
		return
542 +
	}
543 +
	if q, gerr := getQuote(a.DB, id); gerr != nil {
544 +
		a.Log.Error("get quote", "err", gerr)
545 +
		web.RedirectWithError(w, r, "/admin/quotes", "Failed to load quote")
546 +
		return
547 +
	} else if q == nil {
548 +
		web.RedirectWithError(w, r, "/admin/quotes", "Quote not found")
549 +
		return
550 +
	}
551 +
	if err := r.ParseForm(); err != nil {
552 +
		web.RedirectWithError(w, r, "/admin/quotes", "Bad request")
553 +
		return
554 +
	}
555 +
	back := adminBackTo(r, "/admin/quotes")
556 +
	nq, msg := validQuoteForm(r)
557 +
	if msg != "" {
558 +
		web.RedirectWithError(w, r, back, msg)
559 +
		return
560 +
	}
561 +
	b, err := getBook(a.DB, nq.BookID)
562 +
	if err != nil {
563 +
		a.Log.Error("get book", "err", err)
564 +
		web.RedirectWithError(w, r, back, "Failed to load book")
565 +
		return
566 +
	}
567 +
	if b == nil {
568 +
		web.RedirectWithError(w, r, back, "Book not found")
569 +
		return
570 +
	}
571 +
	if err := updateQuote(a.DB, id, nq); err != nil {
572 +
		a.Log.Error("update quote", "err", err)
573 +
		web.RedirectWithError(w, r, back, "Failed to save quote")
574 +
		return
575 +
	}
576 +
	web.RedirectWithSuccess(w, r, back, "Quote updated")
577 +
}
578 +
579 +
func (a *App) adminDeleteQuote(w http.ResponseWriter, r *http.Request) {
580 +
	if err := r.ParseForm(); err != nil {
581 +
		web.RedirectWithError(w, r, "/admin/quotes", "Bad request")
582 +
		return
583 +
	}
584 +
	back := adminBackTo(r, "/admin/quotes")
585 +
	id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
586 +
	if err == nil {
587 +
		_ = deleteQuote(a.DB, id)
588 +
	}
589 +
	web.RedirectWithSuccess(w, r, back, "Quote deleted")
590 +
}
apps/library/main.go +8 −8
32 32
33 33
	tmpl := template.Must(template.ParseFS(appFS, "templates/*.html"))
34 34
	app := &App{
35 -
		DB:             db,
36 -
		Log:            logger,
37 -
		Templates:      tmpl,
38 -
		Sessions:       sessions,
39 -
		AdminPassword:  os.Getenv("ADMIN_PASSWORD"),
40 -
		CookieSecure:   sessions.CookieSecure,
41 -
		BaseURL:        config.Getenv("BASE_URL", "http://localhost:3000"),
42 -
		DisplayMode:    parseDisplayMode(config.Getenv("LIBRARY_DISPLAY_MODE", "inline")),
35 +
		DB:            db,
36 +
		Log:           logger,
37 +
		Templates:     tmpl,
38 +
		Sessions:      sessions,
39 +
		AdminPassword: os.Getenv("ADMIN_PASSWORD"),
40 +
		CookieSecure:  sessions.CookieSecure,
41 +
		BaseURL:       config.Getenv("BASE_URL", "http://localhost:3000"),
42 +
		DisplayMode:   parseDisplayMode(config.Getenv("LIBRARY_DISPLAY_MODE", "inline")),
43 43
	}
44 44
45 45
	addr := config.Getenv("HOST", "0.0.0.0") + ":" + config.Getenv("PORT", "3000")
apps/library/routes.go +12 −0
28 28
	mux.HandleFunc("POST /admin/books/{id}/status", requireSession(a.adminUpdateStatus))
29 29
	mux.HandleFunc("POST /admin/books/{id}/notes", requireSession(a.adminUpdateNotes))
30 30
	mux.HandleFunc("POST /admin/books/{id}/delete", requireSession(a.adminDeleteBook))
31 +
	mux.HandleFunc("GET /admin/books/{id}", requireSession(a.adminEditBookHandler))
32 +
	mux.HandleFunc("POST /admin/books/{id}/edit", requireSession(a.adminUpdateBook))
33 +
	mux.HandleFunc("GET /admin/quotes", requireSession(a.adminQuotesHandler))
34 +
	mux.HandleFunc("POST /admin/quotes/add", requireSession(a.adminAddQuote))
35 +
	mux.HandleFunc("POST /admin/quotes/{id}/edit", requireSession(a.adminUpdateQuote))
36 +
	mux.HandleFunc("POST /admin/quotes/{id}/delete", requireSession(a.adminDeleteQuote))
37 +
38 +
	mux.HandleFunc("GET /book/{id}", a.bookPageHandler)
39 +
	mux.HandleFunc("GET /quotes", a.quotesHandler)
40 +
	mux.HandleFunc("GET /quotes/{id}", a.quotePageHandler)
31 41
32 42
	mux.HandleFunc("GET /api/books", a.apiListBooks)
33 43
	mux.HandleFunc("GET /api/books/{id}", a.apiGetBook)
44 +
	mux.HandleFunc("GET /api/quotes", a.apiListQuotes)
45 +
	mux.HandleFunc("GET /api/quotes/{id}", a.apiGetQuote)
34 46
35 47
	return mux
36 48
}
apps/library/static/styles.css +128 −0
96 96
  padding: 2rem;
97 97
}
98 98
99 +
/* Book & quote pages */
100 +
101 +
.book-card.detail {
102 +
  border-bottom: none;
103 +
  padding-bottom: 0;
104 +
}
105 +
106 +
.book-card-link {
107 +
  display: block;
108 +
  color: inherit;
109 +
  text-decoration: none;
110 +
}
111 +
112 +
.quote-group {
113 +
  display: flex;
114 +
  flex-direction: column;
115 +
  gap: 0.25rem;
116 +
  margin-bottom: 2rem;
117 +
}
118 +
119 +
.quote-group-head {
120 +
  display: flex;
121 +
  flex-direction: column;
122 +
  gap: 0.2rem;
123 +
  padding-bottom: 0.5rem;
124 +
  border-bottom: 1px solid #333;
125 +
}
126 +
127 +
.quote-group-head .book-title {
128 +
  margin: 0;
129 +
}
130 +
131 +
.quote-group-head a,
132 +
.quote-source a {
133 +
  color: inherit;
134 +
  text-decoration: underline;
135 +
  text-underline-offset: 3px;
136 +
}
137 +
138 +
.quotes-list {
139 +
  display: flex;
140 +
  flex-direction: column;
141 +
}
142 +
143 +
.quote-item {
144 +
  display: flex;
145 +
  flex-direction: column;
146 +
  gap: 0.25rem;
147 +
  padding: 0.75rem 0;
148 +
  border-bottom: 1px solid #333;
149 +
  color: inherit;
150 +
  text-decoration: none;
151 +
}
152 +
153 +
.quote-item:last-child {
154 +
  border-bottom: none;
155 +
}
156 +
157 +
.quote-item:hover .quote-text {
158 +
  opacity: 0.75;
159 +
}
160 +
161 +
.quote-text {
162 +
  margin: 0;
163 +
  font-size: 15px;
164 +
  line-height: 1.5;
165 +
}
166 +
167 +
.quote-page {
168 +
  font-size: 12px;
169 +
}
170 +
171 +
.quote-page-title {
172 +
  font-size: 15px;
173 +
  line-height: 1.6;
174 +
  margin: 2rem 0 1rem;
175 +
}
176 +
177 +
.quote-page-meta {
178 +
  font-size: 13px;
179 +
  opacity: 0.6;
180 +
}
181 +
182 +
/* Admin quotes */
183 +
184 +
.quote-admin-row {
185 +
  display: flex;
186 +
  flex-direction: column;
187 +
  gap: 0.5rem;
188 +
  border: 1px solid #333;
189 +
  padding: 0.75rem;
190 +
}
191 +
192 +
.quote-admin-row .meta {
193 +
  font-size: 12px;
194 +
  opacity: 0.5;
195 +
  margin: 0;
196 +
}
197 +
198 +
.quote-admin-row .meta a {
199 +
  color: inherit;
200 +
  text-decoration: underline;
201 +
  text-underline-offset: 3px;
202 +
}
203 +
204 +
.quote-admin-row .labels-form {
205 +
  display: flex;
206 +
  flex-direction: column;
207 +
  gap: 0.5rem;
208 +
}
209 +
210 +
.quote-admin-row textarea {
211 +
  width: 100%;
212 +
  font-family: inherit;
213 +
  font-size: 13px;
214 +
  line-height: 1.4;
215 +
  background: #121113;
216 +
  color: #ffffff;
217 +
  border: 1px solid #333;
218 +
  padding: 0.3rem 0.4rem;
219 +
  resize: vertical;
220 +
}
221 +
222 +
99 223
/* Admin */
100 224
101 225
.admin-form {
241 365
  font-size: 14px;
242 366
  margin: 0;
243 367
}
368 +
369 +
textarea {
370 +
  min-height: 200px !important;
371 +
}
apps/library/templates/admin.html +2 −1
16 16
    <div class="header">
17 17
      <a href="/" class="logo"><h1>LIBRARY</h1></a>
18 18
      <nav class="links">
19 +
        <a href="/admin/quotes">quotes</a>
19 20
        <a href="/admin/logout">logout</a>
20 21
      </nav>
21 22
    </div>
125 126
          <div class="book-cover placeholder"></div>
126 127
          {{end}}
127 128
          <div class="book-info">
128 -
            <h3 class="book-title">{{.Title}}</h3>
129 +
            <h3 class="book-title"><a href="/admin/books/{{.ID}}">{{.Title}}</a></h3>
129 130
            <p class="book-authors">{{.Authors}}</p>
130 131
            {{if .ISBN}}<p class="book-meta">ISBN: {{.ISBN}}</p>{{end}}
131 132
            <form method="POST" action="/admin/books/{{.ID}}/status" class="inline">
apps/library/templates/admin_book.html (added) +115 −0
1 +
<!doctype html>
2 +
<html lang="en">
3 +
  <head>
4 +
    <meta charset="UTF-8" />
5 +
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6 +
    <meta name="theme-color" content="#121113" />
7 +
    <link rel="stylesheet" href="/assets/darkmatter.css" />
8 +
    <link rel="stylesheet" href="/static/styles.css" />
9 +
    <link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png" />
10 +
    <link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png" />
11 +
    <link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png" />
12 +
    <link rel="manifest" href="/static/site.webmanifest" />
13 +
    <title>Library | Admin</title>
14 +
  </head>
15 +
  <body>
16 +
    <div class="header">
17 +
      <a href="/" class="logo"><h1>LIBRARY</h1></a>
18 +
      <nav class="links">
19 +
        <a href="/admin/quotes">quotes</a>
20 +
        <a href="/admin/logout">logout</a>
21 +
      </nav>
22 +
    </div>
23 +
24 +
    {{if .Success}}<p class="success">{{.Success}}</p>{{end}}
25 +
    {{if .Error}}<p class="error">{{.Error}}</p>{{end}}
26 +
27 +
    <article class="book-card admin">
28 +
      {{if .Book.CoverURL}}
29 +
      <img class="book-cover" src="{{.Book.CoverURL}}" alt="" loading="lazy" />
30 +
      {{else}}
31 +
      <div class="book-cover placeholder"></div>
32 +
      {{end}}
33 +
      <div class="book-info">
34 +
        <h2 class="book-title">{{.Book.Title}}</h2>
35 +
        <p class="book-authors">{{.Book.Authors}}</p>
36 +
        {{if .Book.ISBN}}<p class="book-meta">ISBN: {{.Book.ISBN}}</p>{{end}}
37 +
      </div>
38 +
    </article>
39 +
40 +
    <section class="admin-form">
41 +
      <h3>Edit book</h3>
42 +
      <form method="POST" action="/admin/books/{{.Book.ID}}/edit" class="labels-form">
43 +
        <input type="hidden" name="from" value="/admin/books/{{.Book.ID}}" />
44 +
        <label>
45 +
          <span>Title</span>
46 +
          <input type="text" name="title" value="{{.Book.Title}}" required />
47 +
        </label>
48 +
        <label>
49 +
          <span>Authors</span>
50 +
          <input type="text" name="authors" value="{{.Book.Authors}}" required />
51 +
        </label>
52 +
        <label>
53 +
          <span>ISBN</span>
54 +
          <input type="text" name="isbn" value="{{.Book.ISBN}}" />
55 +
        </label>
56 +
        <label>
57 +
          <span>Cover URL</span>
58 +
          <input type="text" name="cover_url" value="{{.Book.CoverURL}}" />
59 +
        </label>
60 +
        <label>
61 +
          <span>Notes</span>
62 +
          <textarea name="notes" rows="2">{{.Book.Notes}}</textarea>
63 +
        </label>
64 +
        <button type="submit">Save book</button>
65 +
      </form>
66 +
    </section>
67 +
68 +
    <section class="admin-form">
69 +
      <h3>Add quote</h3>
70 +
      <form method="POST" action="/admin/quotes/add" class="labels-form">
71 +
        <input type="hidden" name="book_id" value="{{.Book.ID}}" />
72 +
        <input type="hidden" name="from" value="/admin/books/{{.Book.ID}}" />
73 +
        <label>
74 +
          <span>Page</span>
75 +
          <input type="text" name="page" placeholder="optional" />
76 +
        </label>
77 +
        <label>
78 +
          <span>Quote</span>
79 +
          <textarea name="quote" rows="4" required></textarea>
80 +
        </label>
81 +
        <button type="submit">Add quote</button>
82 +
      </form>
83 +
    </section>
84 +
85 +
    <section class="admin-subs">
86 +
      <h3>Quotes ({{len .Quotes}})</h3>
87 +
      {{if not .Quotes}}
88 +
      <p class="hint">No quotes from this book yet.</p>
89 +
      {{else}}
90 +
      {{range .Quotes}}
91 +
      <div class="quote-admin-row">
92 +
        <p class="meta"><a href="/quotes/{{.ID}}">view</a></p>
93 +
        <form method="POST" action="/admin/quotes/{{.ID}}/edit" class="labels-form">
94 +
          <input type="hidden" name="book_id" value="{{.BookID}}" />
95 +
          <input type="hidden" name="from" value="/admin/books/{{$.Book.ID}}" />
96 +
          <label>
97 +
            <span>Page</span>
98 +
            <input type="text" name="page" value="{{.Page}}" />
99 +
          </label>
100 +
          <label>
101 +
            <span>Quote</span>
102 +
            <textarea name="quote" rows="3" required>{{.Quote}}</textarea>
103 +
          </label>
104 +
          <button type="submit">Save</button>
105 +
        </form>
106 +
        <form method="POST" action="/admin/quotes/{{.ID}}/delete" class="inline">
107 +
          <input type="hidden" name="from" value="/admin/books/{{$.Book.ID}}" />
108 +
          <button type="submit" class="danger">Delete</button>
109 +
        </form>
110 +
      </div>
111 +
      {{end}}
112 +
      {{end}}
113 +
    </section>
114 +
  </body>
115 +
</html>
apps/library/templates/admin_quotes.html (added) +93 −0
1 +
<!doctype html>
2 +
<html lang="en">
3 +
  <head>
4 +
    <meta charset="UTF-8" />
5 +
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6 +
    <meta name="theme-color" content="#121113" />
7 +
    <link rel="stylesheet" href="/assets/darkmatter.css" />
8 +
    <link rel="stylesheet" href="/static/styles.css" />
9 +
    <link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png" />
10 +
    <link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png" />
11 +
    <link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png" />
12 +
    <link rel="manifest" href="/static/site.webmanifest" />
13 +
    <title>Library | Quotes Admin</title>
14 +
  </head>
15 +
  <body>
16 +
    <div class="header">
17 +
      <a href="/" class="logo"><h1>LIBRARY</h1></a>
18 +
      <nav class="links">
19 +
        <a href="/admin">admin</a>
20 +
        <a href="/admin/logout">logout</a>
21 +
      </nav>
22 +
    </div>
23 +
24 +
    {{if .Success}}<p class="success">{{.Success}}</p>{{end}}
25 +
    {{if .Error}}<p class="error">{{.Error}}</p>{{end}}
26 +
27 +
    <section class="admin-form">
28 +
      <h3>Add quote</h3>
29 +
      {{if not .Books}}
30 +
      <p class="hint">No books yet. Add a book before adding quotes.</p>
31 +
      {{else}}
32 +
      <form method="POST" action="/admin/quotes/add" class="labels-form">
33 +
        <input type="hidden" name="from" value="/admin/quotes" />
34 +
        <label>
35 +
          <span>Book</span>
36 +
          <select name="book_id" required>
37 +
            {{range .Books}}
38 +
            <option value="{{.ID}}">{{.Title}}{{if .Authors}} — {{.Authors}}{{end}}</option>
39 +
            {{end}}
40 +
          </select>
41 +
        </label>
42 +
        <label>
43 +
          <span>Page</span>
44 +
          <input type="text" name="page" placeholder="optional" />
45 +
        </label>
46 +
        <label>
47 +
          <span>Quote</span>
48 +
          <textarea name="quote" rows="2" required></textarea>
49 +
        </label>
50 +
        <button type="submit">Add quote</button>
51 +
      </form>
52 +
      {{end}}
53 +
    </section>
54 +
55 +
    <section class="admin-subs">
56 +
      <h3>All quotes ({{len .Quotes}})</h3>
57 +
      {{if not .Quotes}}
58 +
      <p class="hint">No quotes yet.</p>
59 +
      {{else}}
60 +
      {{range .Quotes}}
61 +
      {{$q := .}}
62 +
      <div class="quote-admin-row">
63 +
        <p class="meta"><a href="/quotes/{{$q.ID}}">view</a> · {{if $q.BookTitle}}{{$q.BookTitle}}{{else}}book #{{$q.BookID}}{{end}}</p>
64 +
        <form method="POST" action="/admin/quotes/{{$q.ID}}/edit" class="labels-form">
65 +
          <input type="hidden" name="from" value="/admin/quotes" />
66 +
          <label>
67 +
            <span>Book</span>
68 +
            <select name="book_id">
69 +
              {{range $.Books}}{{$b := .}}
70 +
              <option value="{{$b.ID}}"{{if eq $q.BookID $b.ID}} selected{{end}}>{{$b.Title}}{{if $b.Authors}} — {{$b.Authors}}{{end}}</option>
71 +
              {{end}}
72 +
            </select>
73 +
          </label>
74 +
          <label>
75 +
            <span>Page</span>
76 +
            <input type="text" name="page" value="{{$q.Page}}" />
77 +
          </label>
78 +
          <label>
79 +
            <span>Quote</span>
80 +
            <textarea name="quote" rows="2" required>{{$q.Quote}}</textarea>
81 +
          </label>
82 +
          <button type="submit">Save</button>
83 +
        </form>
84 +
        <form method="POST" action="/admin/quotes/{{$q.ID}}/delete" class="inline">
85 +
          <input type="hidden" name="from" value="/admin/quotes" />
86 +
          <button type="submit" class="danger">Delete</button>
87 +
        </form>
88 +
      </div>
89 +
      {{end}}
90 +
      {{end}}
91 +
    </section>
92 +
  </body>
93 +
</html>
apps/library/templates/book.html (added) +52 −0
1 +
<!doctype html>
2 +
<html lang="en">
3 +
  <head>
4 +
    <meta charset="UTF-8" />
5 +
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6 +
    <meta name="theme-color" content="#121113" />
7 +
    <link rel="stylesheet" href="/assets/darkmatter.css" />
8 +
    <link rel="stylesheet" href="/static/styles.css" />
9 +
    <link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png" />
10 +
    <link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png" />
11 +
    <link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png" />
12 +
    <link rel="manifest" href="/static/site.webmanifest" />
13 +
    <title>Library | {{.Book.Title}}</title>
14 +
  </head>
15 +
  <body>
16 +
    <div class="header">
17 +
      <a href="/" class="logo"><h1>LIBRARY</h1></a>
18 +
      <nav class="links">
19 +
        <a href="/quotes">quotes</a>
20 +
      </nav>
21 +
    </div>
22 +
23 +
    <article class="book-card detail">
24 +
      {{if .Book.CoverURL}}
25 +
      <img class="book-cover" src="{{.Book.CoverURL}}" alt="" />
26 +
      {{else}}
27 +
      <div class="book-cover placeholder"></div>
28 +
      {{end}}
29 +
      <div class="book-info">
30 +
        <h2 class="book-title">{{.Book.Title}}</h2>
31 +
        <p class="book-authors">{{.Book.Authors}}</p>
32 +
        {{if .Book.ISBN}}<p class="book-meta">ISBN: {{.Book.ISBN}}</p>{{end}}
33 +
      </div>
34 +
    </article>
35 +
36 +
    <section class="section">
37 +
      <h3 class="section-label">Quotes</h3>
38 +
      {{if not .Quotes}}
39 +
      <p class="no-books">No quotes from this book yet.</p>
40 +
      {{else}}
41 +
      <div class="quotes-list">
42 +
        {{range .Quotes}}
43 +
        <a class="quote-item" href="/quotes/{{.ID}}">
44 +
          <blockquote class="quote-text">{{.Quote}}</blockquote>
45 +
          {{if .Page}}<p class="quote-page">p. {{.Page}}</p>{{end}}
46 +
        </a>
47 +
        {{end}}
48 +
      </div>
49 +
      {{end}}
50 +
    </section>
51 +
  </body>
52 +
</html>
apps/library/templates/index.html +3 −1
32 32
        <a href="/?category={{.Slug}}"{{if .Active}} class="active"{{end}}>{{.Label}}</a>
33 33
        {{end}}
34 34
        {{end}}
35 -
        <a href="/admin">add</a>
35 +
        <a href="/quotes">quotes</a>
36 36
      </nav>
37 37
    </div>
38 38
49 49
      {{else}}
50 50
      <div class="books-list">
51 51
        {{range .Books}}
52 +
        <a class="book-card-link" href="/book/{{.ID}}">
52 53
        <article class="book-card">
53 54
          {{if .CoverURL}}
54 55
          <img class="book-cover" src="{{.CoverURL}}" alt="" loading="lazy" />
63 64
            {{end}}
64 65
          </div>
65 66
        </article>
67 +
        </a>
66 68
        {{end}}
67 69
      </div>
68 70
      {{end}}
apps/library/templates/quote.html (added) +29 −0
1 +
<!doctype html>
2 +
<html lang="en">
3 +
  <head>
4 +
    <meta charset="UTF-8" />
5 +
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6 +
    <meta name="theme-color" content="#121113" />
7 +
    <link rel="stylesheet" href="/assets/darkmatter.css" />
8 +
    <link rel="stylesheet" href="/static/styles.css" />
9 +
    <link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png" />
10 +
    <link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png" />
11 +
    <link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png" />
12 +
    <link rel="manifest" href="/static/site.webmanifest" />
13 +
    <title>Library | Quote</title>
14 +
  </head>
15 +
  <body>
16 +
    <div class="header">
17 +
      <a href="/" class="logo"><h1>LIBRARY</h1></a>
18 +
      <nav class="links">
19 +
        <a href="/quotes">quotes</a>
20 +
      </nav>
21 +
    </div>
22 +
23 +
    <article class="quote-page">
24 +
      <blockquote class="quote-page-title">{{.Quote.Quote}}</blockquote>
25 +
      {{if .Quote.Page}}<p class="quote-page-meta">p. {{.Quote.Page}}</p>{{end}}
26 +
      <p class="quote-page-meta">from <a href="/book/{{.Book.ID}}">{{.Book.Title}}</a> by {{.Book.Authors}}</p>
27 +
    </article>
28 +
  </body>
29 +
</html>
apps/library/templates/quotes.html (added) +44 −0
1 +
<!doctype html>
2 +
<html lang="en">
3 +
  <head>
4 +
    <meta charset="UTF-8" />
5 +
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6 +
    <meta name="theme-color" content="#121113" />
7 +
    <link rel="stylesheet" href="/assets/darkmatter.css" />
8 +
    <link rel="stylesheet" href="/static/styles.css" />
9 +
    <link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png" />
10 +
    <link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png" />
11 +
    <link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png" />
12 +
    <link rel="manifest" href="/static/site.webmanifest" />
13 +
    <title>Library | Quotes</title>
14 +
  </head>
15 +
  <body>
16 +
    <div class="header">
17 +
      <a href="/" class="logo"><h1>LIBRARY</h1></a>
18 +
      <nav class="links">
19 +
        <a href="/" class="active">library</a>
20 +
      </nav>
21 +
    </div>
22 +
23 +
    {{if not .Groups}}
24 +
    <p class="no-books">No quotes yet.</p>
25 +
    {{else}}
26 +
    {{range .Groups}}
27 +
    <section class="quote-group">
28 +
      <div class="quote-group-head">
29 +
        <h2 class="book-title"><a href="/book/{{.BookID}}">{{.BookTitle}}</a></h2>
30 +
        <p class="book-authors">{{.BookAuthors}}</p>
31 +
      </div>
32 +
      <div class="quotes-list">
33 +
        {{range .Quotes}}
34 +
        <a class="quote-item" href="/quotes/{{.ID}}">
35 +
          <blockquote class="quote-text">{{.Quote}}</blockquote>
36 +
          {{if .Page}}<p class="quote-page">p. {{.Page}}</p>{{end}}
37 +
        </a>
38 +
        {{end}}
39 +
      </div>
40 +
    </section>
41 +
    {{end}}
42 +
    {{end}}
43 +
  </body>
44 +
</html>