2.1 KiB
2.1 KiB
Spec: Watchlist CSV Export and Import
Overview
Add functionality to export the user's watchlist to a CSV file and import it back. This allows for easy backup, external editing, and migration of watchlist data.
Requirements
- Export: Generate a CSV file containing
anime_id,status,current_episode, andcurrent_time_seconds. - Import: Upload a CSV file to update or add watchlist entries.
- Data Integrity:
- Do not export
created_atorupdated_at. - On import, overwrite existing entries with the same
anime_id. - Ensure anime exists in the local database before adding to watchlist (fetch from Jikan if necessary).
- Do not export
- UI: Add Export and Import buttons to the watchlist page header.
Architecture
Frontend
- Template: Update
templates/watchlist.gohtml(and partial) to include Export/Import buttons. - Export Implementation:
- Use JavaScript to iterate over the
AllEntriesdata passed to the template. - Create a CSV string.
- Trigger a browser download using a
Bloband a temporary link element.
- Use JavaScript to iterate over the
- Import Implementation:
- Use a hidden
<input type="file" accept=".csv">. - When a file is selected, use
FormDatato upload it to the server viafetch. - Show a simple alert or refresh the page on success.
- Use a hidden
Backend
- Endpoint:
POST /api/watchlist/import - Service: Add
ImportWatchlist(ctx, userID, reader)toapi/watchlist/service.go. - Logic:
- Parse CSV using
encoding/csv. - For each record:
- Validate data types.
- Call
ensureAnimeExists(existing service method). - Use
UpsertWatchListEntryto save/update.
- Process the entire file in a transaction if possible, or gracefully skip malformed rows.
- Parse CSV using
Data Format (CSV)
Headers: anime_id,status,current_episode,current_time_seconds
Example:
123,watching,5,450.5
456,completed,12,0
Success Criteria
- Clicking "Export" downloads a valid CSV file.
- Modifying the CSV and clicking "Import" correctly updates the watchlist in the UI and database.
- New anime added via CSV are correctly fetched from Jikan and added to the database.