Skip to content

Instantly share code, notes, and snippets.

@zgulde
Last active April 23, 2017 23:01
Show Gist options
  • Save zgulde/911db02092101cb3404e to your computer and use it in GitHub Desktop.
Save zgulde/911db02092101cb3404e to your computer and use it in GitHub Desktop.

JSON Todo List Exercise

Quick Reference

GET

Parameters Description
none returns all todos
complete=? returns all todos that are completed or not
order_by=?[direction=asc|desc] sort items by a property of the todo, optionally in ascending or descending order
id=? returns the todo with the given id

POST

Parameters Description
content=?[&priority=?][&due_date=?] create a new todo
id=?&action=update&content=?[&priority=?][&due_date=?] update a todo
id=?&action=delete delete a todo
id=?&action=complete[&state=true|false] mark a todo as completed or remove the completed status

See more detailed instructions below

The Todo Object

Each todo is represented as an object that has several properties

property type description
content string title or content of the todo
id number this will be unique for each item
created_at Date automatically generated date
priority number must be an integer >= 0
due_date Date optional
completed Date date completed, defaults to null

GET Requests

Used for retrieving data

  • a get with no parameters will return all the todos
  • sending the parameter complete with a value of true or false will give all todo items that are completed or not, respectively
  • sending the parameter id with a value of a todo item's id will return just that one todo item
  • sending the parameter order_by with a value of one of the todo item's properties will sort the results by that property
    • you can also send a direction parameter for the direction of the sort that takes values asc or desc
    • defaults to ascending order if no direction is passed

POST Requests

Used for creating and modifying data

Creating a Record

Sending only content, priority, and due_date parameters will create a new todo item, content is required, priority will default to 0, and due_date will default to null if nothing is passed.

The server's response will be the object that was newly created or an error message if something went wrong.

Modifying a Record

To modify a record you must pass two parameters:

  • id, the id of the record to be modified
  • action one of update, delete, or complete
    • update: will also take three additional parameters, content, which is required, priority and due_date are optional. All properties will be updated accordingly.
    • delete: as the name implies will delete the todo item
    • complete: optionally takes a state parameter with values true or false
      • if no state parameter is passed, or if state is set to true, the completed property of the todo will be set to the time when the server receives your ajax request
      • if state is set to false, the complete property of the todo will be set to null

The server's response will be the object that was modified, or an error message if something went wrong.

Examples

Update a todo's information

$.post('/todo-json.php',{
    "id": 3,
    "action": "update",
    "content": "updated todo item"
});

Get all completed todos and sort them by descending priority

$.ajax({
    "url": "/todo-json.php",
    "type": "GET",
    "data": {
        "complete": "true",
        "order_by": "priority",
        "direction": "desc"
    }
});
@rrapstine
Copy link

Great documentation! It's missing the $GET parameters for sorting and setting the sort direction though.

$.get('/todo-json.php', {
  order_by: "priority",
  direction: "asc"   // Sort in ascending order
  direction: "desc"  // Sort in descending order
});

@zgulde
Copy link
Author

zgulde commented Feb 19, 2016

Thanks Rick, it's now updated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment