+++
title = "Implement tag join logic — fetch tags per quote, insert/replace tags on create/update"
priority = 5
status = "todo"
ticket_type = "task"
dependencies = ["a5049d"]
+++
The `quotesdb` API is built with Axum + Tokio, targeting Cloudflare Workers via `workers-rs`. It serves JSON at `/api/*` endpoints and persists data to Cloudflare D1 (production) or a local SQLite file via Turso (development). Source lives in `src/bin/api/`.
Shared types and utilities are in `src/lib.rs` — code placed there must compile for both the host target and `wasm32-unknown-unknown`.
Each quote can have multiple tags stored in the `quote_tags` join table. Tags are not normalised — they are stored as plain strings per quote. On create/update, all tags for the quote are replaced atomically.
Implement tag fetch and upsert logic used by the API handlers:
1. `fetch_tags_for_quote(pool, quote_id) -> Vec` — SELECT from quote_tags
2. `replace_tags_for_quote(pool, quote_id, tags: &[String])` — DELETE existing, INSERT new tags in a transaction
This logic should live in a `db` or `tags` module and be called from the create and update handlers.
- Tag replacement must be atomic (use a transaction).
- Empty `tags` array means "remove all tags" — this is valid.
- Cascade delete on `quote_tags` handles tag cleanup when a quote is deleted — no separate delete-tags step needed.
Use `superpowers:test-driven-development` — write unit tests that verify tag insertion, replacement, and empty-tag cases.
Use `superpowers:verification-before-completion` before closing.
Run in order from the `quotesdb/` directory:
```sh
cargo fmt
cargo check
cargo clippy
cargo test
```
`feat(quotesdb): implement tag join logic — fetch and replace tags per quote`