Skip to content

Instantly share code, notes, and snippets.

@misostack
Last active October 20, 2020 17:50
Show Gist options
  • Select an option

  • Save misostack/3d8817d37abb2000effc66db0782b798 to your computer and use it in GitHub Desktop.

Select an option

Save misostack/3d8817d37abb2000effc66db0782b798 to your computer and use it in GitHub Desktop.
REST API Standard Development

Top Down Approach

  1. Routes
  2. Controllers & Actions
  3. DTO : Validations
  4. Response: CRUD, Errors
  5. Resolver
  6. Model/Entity
  7. Schema vs ActiveRecord vs DataMapper
  8. Search & Pagination
  9. Native query & Optimizing
  10. Caching
  11. DB Replication vs Sharding
  12. Multiple Database

General topics

  1. Blueprint routes
GET /resources - LIST - 200
GET /resources/:id - RETRIEVE A RESOURCE - 200/404
POST /resources - CREATE - 201
PATCH /resources/:id - UPDATE - 200/404
DELETE /resources/:id - DELETE - 204/404

Standard JSON Response

  1. Success
curl -X POST http://localhost:1337/admins -H "Content-Type: application/json"  --data '{"username": "sonnm","password":"13456", "email":"sonnm@yopmail.com", "firstName": "Son", "lastName": "Nguyen"}' -v

curl -X PATCH http://localhost:1337/admins/b46f5680-3dd5-4623-a7fd-f383a4cb2680 -H "Content-Type: application/json"  --data '{"username": "sonnm","password":"123456"}' -v

curl -X DELETE http://localhost:1337/admins/3e1e0713-bf9d-4af2-8b23-931c0af3a6c2 -H "Content-Type: application/json" -v
// Create
{
  "message": "Success",
  "status": 201,
  "data": {
    "createdAt": 1603213704644,
    "updatedAt": 1603213704644,
    "id": "b46f5680-3dd5-4623-a7fd-f383a4cb2680",
    "username": "sonnm",
    "email": "sonnm@yopmail.com",
    "status": "active",
    "firstName": "Son",
    "lastName": "Nguyen"
  }
}

// Update

{
  "message": "Success",
  "status": 200,
  "data": {
    "createdAt": 1603213704644,
    "updatedAt": 1603213884594,
    "id": "b46f5680-3dd5-4623-a7fd-f383a4cb2680",
    "username": "sonnm",
    "email": "sonnm@yopmail.com",
    "status": "active",
    "firstName": "Son",
    "lastName": "Nguyen"
  }
}
// Delete

{
  "message": "Success",
  "status": 200,
  "data": {}
}
  1. Validation Error
// 400 - Bad Request
{
  "message": "Bad Request",
  "status": 400,
  "errors": [
    {
      "field": "username",
      "message": "Username is duplicated"
    },
    {
      "field": "email",
      "message": "Email is duplicated"
    }
  ]
}

// 404 - Not found
{
  "message": "Not found",
  "status": 404
}

// 500 - Internal Server Error
{
  "message": "Internal Server Error",
  "status": 500
}

References

  1. https://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/
  2. https://symfonycasts.com/screencast/rest/patch
  3. https://restfulapi.net/http-methods/#:~:text=A%20successful%20response%20of%20DELETE,DELETE%20operations%20are%20idempotent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment