Basic CRUD API

Complete CRUD API example with tasks management. Includes all operations (Create, Read, Update, Delete), error handling, validation, and a working moclojer configuration ready to use.

This example demonstrates a complete CRUD (Create, Read, Update, Delete) API for managing tasks. It's production-ready and includes proper error handling, validation, and RESTful conventions.

📋 What You'll Get

  • ✅ Complete CRUD operations

  • ✅ Proper HTTP status codes

  • ✅ Error handling and validation

  • ✅ Pagination support

  • ✅ Search and filtering

  • ✅ Ready-to-use configuration file

🎯 API Overview

Architecture Diagram

graph LR
    A[Client] -->|HTTP Requests| B[moclojer]
    B -->|GET /tasks| C[List Tasks]
    B -->|POST /tasks| D[Create Task]
    B -->|GET /tasks/:id| E[Get Task]
    B -->|PUT /tasks/:id| F[Update Task]
    B -->|DELETE /tasks/:id| G[Delete Task]

Endpoints

Method
Path
Description
Status

GET

/tasks

List all tasks (with pagination)

200

GET

/tasks/:id

Get specific task

200, 404

POST

/tasks

Create new task

201, 400

PUT

/tasks/:id

Update task

200, 404, 400

DELETE

/tasks/:id

Delete task

204, 404

GET

/tasks/search

Search tasks

200

📁 Configuration File

Create tasks-api.yml:

🚀 Usage Examples

Start the Server

1. List All Tasks

With pagination:

2. Get Specific Task

3. Create New Task

4. Update Task

5. Delete Task

6. Search Tasks

7. Test Error Cases

📊 Testing Script

Create test-tasks-api.sh:

Run tests:

🎓 Learning Points

HTTP Status Codes

  • 200 OK - Successful GET, PUT

  • 201 Created - Successful POST (resource created)

  • 204 No Content - Successful DELETE (no response body)

  • 400 Bad Request - Validation errors

  • 404 Not Found - Resource doesn't exist

RESTful Conventions

  • Resource naming: Plural nouns (/tasks, not /task)

  • HTTP methods: Match CRUD operations semantically

  • Status codes: Use appropriate codes for each response

  • Location header: Return URL of created resource

Best Practices Demonstrated

✅ Consistent response format across endpoints ✅ Pagination metadata for list endpoints ✅ Detailed error messages with codes ✅ Timestamps for audit trail ✅ Search and filtering capabilities

🚀 Next Steps


💡 Tip: This example is production-ready! You can use it as a template for your own CRUD APIs.

Last updated

Was this helpful?