Skip to content

Latest commit

 

History

History
180 lines (139 loc) · 3.71 KB

README.md

File metadata and controls

180 lines (139 loc) · 3.71 KB

django-todo-api 📝

A simple To Do API built with Django and djangorestframework.

This API utilizes ModelViewSet, which abstracts away the method handlers and provides operations or actions instead. URL routing is handled by DefaultRouter. TRULY MAGIC.

I chose Django specifically because it was incredibly easy to get started (opinionated framework, batteries included), and djangorestframework made it extra easy to create the API.

Table of Contents 📑

Built with 💻

  • Django
  • djangorestframework

Getting Started 🛠

Requirements:

  • Python 3+
  • Django
  • djangorestframework

To run this app on your local computer, please follow the below steps:

Clone repository.

$ git clone https://github.com/janetanne/django-todo-api.git

Create and activate a virtual environment.

$ python -m venv .env
$ source .env/bin/activate

Install the dependencies:

$ pip install -r requirements.txt

Create a secrets.sh file that holds your Django secret key:

export DJANGO_SECRET="INSERTSECRETKEYHERE"

Source your key from secrets.sh into your virtual environment.

$ source secrets.sh

Start the server from the command line:

$ python manage.py runserver

The server may recommend making the data migrations. If so, quit the server, and run this:

$ python manage.py makemigrations

Migrations for 'todo_api':
  todo_api/migrations/0001_initial.py
    - Create model ToDo

$ python manage.py migrate

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, todo_api, sessions
Running migrations:
  Applying todo_api.0001_initial... OK

Navigate to 'http://127.0.0.1:8000/todos' in your browser.

Endpoints 👩🏻‍💻

This was built with REST API best practices in mind.

GET /todos

Returns all todos. Successful response is HTTP 200 status code.

Response:

[
    {
        "id": integer,
        "description": string,
        "completed": boolean,
        "created_at": datetime,
        "updated_at": datetime
    },
]

GET /todos/:id

Returns single todo. Successful response is HTTP 200 status code.

Response:

[
    {
        "id": integer,
        "description": string,
        "completed": boolean,
        "created_at": datetime,
        "updated_at": datetime
    },
]

POST /todos

Creates a new todo and returns it. Completed property defaults to False. Successful response is HTTP 201 status code.

Example request:

{
  "description":"Finish READme",
  "completed":false
}

Response:

{
    "id": 5,
    "description": "Finish READme",
    "completed": false,
    "created_at": "2021-03-24T21:39:28.692402Z",
    "updated_at": "2021-03-24T21:39:28.692439Z"
}

PATCH /todos/:id

Updates a todo and returns it. Can change either the "description" or "completed" properties here; does not need both to update the todo. Successful response is HTTP 200 status code.

Example request for todos/5:

{
  "completed":true
}

Response:

{
    "id": 5,
    "description": "Finish READme",
    "completed": true,
    "created_at": "2021-03-24T21:39:28.692402Z",
    "updated_at": "2021-03-24T21:47:13.522333Z"
}

DELETE /todos/:id

Deletes todo and returns nothing. Successful response is HTTP 204 status code.

Testing 🧪

To run tests, be sure to activate your virtualenv and secrets.sh.

$ source .env/bin/activate
$ source secrets.sh
$ python ./manage.py test

Postman Collection

More to come!