- Add modernc.org/sqlite (pure Go, no CGO) - Create models package with Post struct - Implement SQLite connection and schema auto-creation - Add CRUD methods to database package - Create post handlers with JSON API - Register API routes: GET/POST/PUT/DELETE /api/v1/posts - Set default DBURL to file:./data.db with WAL mode
2.3 KiB
2.3 KiB
TODO: Blog Posts CRUD Implementation
Completed
- Add SQLite dependency (
modernc.org/sqlite- pure Go, no CGO) - Create models package (
internal/models/models.go)- Post struct with JSON tags
- CreatePostRequest and UpdatePostRequest structs
- Update database package (
internal/database/database.go)- SQLite connection with WAL mode
- Schema auto-creation on startup
- CRUD methods: CreatePost, GetPost, ListPosts, UpdatePost, DeletePost
- Create post handlers (
internal/handlers/posts.go)- HandleListPosts, HandleGetPost, HandleCreatePost, HandleUpdatePost, HandleDeletePost
- Register API routes (
internal/server/routes.go)- GET /api/v1/posts
- POST /api/v1/posts
- GET /api/v1/posts/{id}
- PUT /api/v1/posts/{id}
- DELETE /api/v1/posts/{id}
- Set default DBURL (
internal/config/config.go)- Default:
file:./data.db?_journal_mode=WAL
- Default:
Optional (Future Enhancements)
- Add HTML templates for web UI
templates/posts.html- List all poststemplates/post.html- View single posttemplates/post_form.html- Create/edit form
- Add web routes for HTML views
- GET /posts - List page
- GET /posts/new - Create form
- GET /posts/{id} - View page
- GET /posts/{id}/edit - Edit form
- Add authentication for create/update/delete operations
- Add pagination for list endpoint
- Add unit tests
Testing the API
# Create a post
curl -X POST http://localhost:8080/api/v1/posts \
-H "Content-Type: application/json" \
-d '{"title":"Hello","body":"World","author":"me"}'
# List all posts
curl http://localhost:8080/api/v1/posts
# Get a single post
curl http://localhost:8080/api/v1/posts/1
# Update a post
curl -X PUT http://localhost:8080/api/v1/posts/1 \
-H "Content-Type: application/json" \
-d '{"title":"Updated Title","published":true}'
# Delete a post
curl -X DELETE http://localhost:8080/api/v1/posts/1
Files Modified/Created
| File | Status |
|---|---|
go.mod |
Modified - added modernc.org/sqlite |
internal/models/models.go |
Created |
internal/database/database.go |
Modified - added SQLite and CRUD |
internal/handlers/posts.go |
Created |
internal/server/routes.go |
Modified - added post routes |
internal/config/config.go |
Modified - added default DBURL |