# SCREEN: Reading List

| Field | Value |
|-------|-------|
| Version | 20261023 V1 |
| Description | Server-rendered page that shows the reading list in the order added, with an add form, error display, and per-book remove buttons. |
| Depends On | FEATURE-AddBook.md, FEATURE-RejectEmptyFields.md, FEATURE-ListBooks.md, FEATURE-RemoveBook.md, FEATURE-AppShell.md |
| Provides | GET /, POST /add, POST /remove/{book_id} |
| Consumes | GET /books, POST /books, DELETE /books/{book_id} |
| Route | / |
| Parent | — |
| Main Menu | Reading List (1) |
| Tab Order | — |

## Purpose

The single page of the application. A reader sees their books in the order they were added, adds a book with a title and author, sees a clear error when either is empty, and removes books they no longer want.

## Layout

- Page heading: "Reading List".
- Add form with a `title` text input, an `author` text input, and an "Add" submit button, posting to `/add`.
- Error region directly above the form, rendered only when a submission was rejected. It states which field is missing.
- Ordered list of books, one entry per book showing title and author, in the order added (id ascending).
- Each entry carries a "Remove" button, a form posting to `/remove/{book_id}`.
- An empty list renders a short "No books yet" message instead of the list.

## Routes

| Route | Method | Behavior |
|-------|--------|----------|
| `/` | GET | Renders the page with all books in the order added; status 200. |
| `/add` | POST | Form fields `title` and `author`. Valid input stores the book through the book service and redirects 303 to `/`. Empty or whitespace-only title or author re-renders the page with status 400, a visible error message, and nothing stored. |
| `/remove/{book_id}` | POST | Removes the book through the book service and redirects 303 to `/`. An unknown id also redirects 303 to `/` without changing state. |

The screen calls the same service functions that back `POST /books`, `GET /books`, and `DELETE /books/{book_id}`, so browser and JSON behavior cannot diverge. No client-side JavaScript is used.

## Interactions

1. The reader fills in both fields and submits; the page reloads with the new book at the end of the list.
2. The reader submits with an empty title or author; the page reloads with an error message and the list unchanged. Entered values are preserved in the form.
3. The reader presses "Remove" on a book; the page reloads without that book.

## Database Interface

- None — persistence is owned by FEATURE-AddBook.md.

## Programmatic Acceptance

=== AC screen-index-renders ===
Intent: GET / answers 200 and shows an added book's title and author, and GET /books agrees with it on the same book.
Suite: scoped

import uuid
from app_server.testing import web_client

client = web_client()
token = uuid.uuid4().hex
title = "ac-screen-title-" + token
author = "ac-screen-author-" + token
created = client.post("/books", json={"title": title, "author": author})
assert created.status_code == 201
book_id = created.get_json()["id"]
try:
    page = client.get("/")
    assert page.status_code == 200
    html = page.get_data(as_text=True)
    assert title in html
    assert author in html
    listed = [b["id"] for b in client.get("/books").get_json()]
    assert book_id in listed
finally:
    client.delete(f"/books/{book_id}")
=== END AC screen-index-renders ===

=== AC screen-add-form ===
Intent: POST /add with a title and author redirects 303 and the book then appears on GET / and in GET /books.
Suite: scoped

import uuid
from app_server.testing import web_client

client = web_client()
token = uuid.uuid4().hex
title = "ac-form-title-" + token
author = "ac-form-author-" + token
added_ids = []
try:
    before = {b["id"] for b in client.get("/books").get_json()}
    response = client.post("/add", data={"title": title, "author": author})
    assert response.status_code == 303
    after = client.get("/books").get_json()
    added_ids = [b["id"] for b in after if b["id"] not in before]
    assert len(added_ids) == 1
    stored = [b for b in after if b["id"] == added_ids[0]][0]
    assert stored["title"] == title
    assert stored["author"] == author
    html = client.get("/").get_data(as_text=True)
    assert title in html
finally:
    for book_id in added_ids:
        client.delete(f"/books/{book_id}")
=== END AC screen-add-form ===

=== AC screen-add-rejects-empty ===
Intent: POST /add with an empty title, an empty author, or whitespace-only values answers 400 and stores nothing.
Suite: scoped

import uuid
from app_server.testing import web_client

client = web_client()
token = uuid.uuid4().hex
valid_title = "ac-reject-title-" + token
valid_author = "ac-reject-author-" + token
cases = [
    {"title": "", "author": valid_author},
    {"title": valid_title, "author": ""},
    {"title": "", "author": ""},
    {"title": "   ", "author": valid_author},
    {"title": valid_title, "author": "   "},
]
before = [b["id"] for b in client.get("/books").get_json()]
try:
    for form in cases:
        response = client.post("/add", data=form)
        assert response.status_code == 400
        assert len(response.get_data(as_text=True)) > 0
    after = [b["id"] for b in client.get("/books").get_json()]
    assert after == before
finally:
    current = client.get("/books").get_json()
    for book in current:
        if book["id"] not in before:
            client.delete(f"/books/{book['id']}")
=== END AC screen-add-rejects-empty ===

=== AC screen-remove-form ===
Intent: POST /remove/{book_id} redirects 303 and the book is gone from GET / and GET /books; an unknown id also redirects 303.
Suite: scoped

import uuid
from app_server.testing import web_client

client = web_client()
title = "ac-screen-remove-" + uuid.uuid4().hex
created = client.post("/books", json={"title": title, "author": "ac-screen-author"})
assert created.status_code == 201
book_id = created.get_json()["id"]
try:
    response = client.post(f"/remove/{book_id}")
    assert response.status_code == 303
    listed = [b["id"] for b in client.get("/books").get_json()]
    assert book_id not in listed
    html = client.get("/").get_data(as_text=True)
    assert title not in html
    again = client.post(f"/remove/{book_id}")
    assert again.status_code == 303
finally:
    client.delete(f"/books/{book_id}")
=== END AC screen-remove-form ===

=== AC screen-order-added ===
Intent: GET / presents books in the order they were added.
Suite: scoped

import uuid
from app_server.testing import web_client

client = web_client()
token = uuid.uuid4().hex
titles = [f"ac-screen-order-{token}-{n}" for n in range(3)]
ids = []
try:
    for title in titles:
        resp = client.post("/books", json={"title": title, "author": "ac-screen-author"})
        assert resp.status_code == 201
        ids.append(resp.get_json()["id"])
    html = client.get("/").get_data(as_text=True)
    positions = [html.index(title) for title in titles]
    assert positions == sorted(positions)
finally:
    for book_id in ids:
        client.delete(f"/books/{book_id}")
=== END AC screen-order-added ===

## User Acceptance

- The page is readable: the form, the error message, and the book list are visually distinct.

## Guardrails

- Books are never presented in any order other than the order they were added.
- An empty or whitespace-only title or author is never stored; the reader sees a clear error message.
- A removed book never reappears on the page.
- The page uses no client-side JavaScript.
