Code Monkey home page Code Monkey logo

tasks-rest-api's Introduction

RESTful API for the Task list

A basic RESTful API with token-based authentication in PHP for "Task List" application.

Note: It's a test project. Don't use in for production application.

Technologies and tools:

  • PHP
  • MySQL

Set up

I used the Apache server. However, it should work on any server which supports .htacccess files because it contains routing information.

Note: The php_flag display_errors on should be removed from the .htaccess file before installing to server.

MySQL database

Database name: tasks-db.

Tables:

  • Users (tbl_users)
  • Sessions (tbl_sessions)
  • Tasks (tbl_tasks)

Note: If you want to use different names of database and tables, remember to update source code.

Use Cases

Basic use case of using API for creating account, adding tasks to logged in user and log out.

  1. Create a new account
  2. Log In
  3. Add a new task
  4. Get all tasks or Get all task split by pages
  5. Log Out

Additional features:

  1. Update an existing task
  2. Delete an existing task
  3. Get completed tasks
  4. Get incompleted tasks

Routes

Request requirements

Description of basic requirements for each request.

Create a new user account

Label Value
Method POST
URL /tasks-rest-api/v1/users
Header Content-Type : application/json
Body {
    "fullname":"FULL_NAME",
    "username":"USER_NAME",
    "password":"PASSWORD"
}

The request contains user data: FULL_NAME, USER_NAME and PASSWORD.

Response:

{
    "statusCode": 201,
    "success": true,
    "messages": ["User created"],
    "data": {
        "user_id": "ID",
        "fullname": "FULL_NAME",
        "username": "USER_NAME"
    }
}

Log in (Create a new user session)

Label Value
Method POST
URL /tasks-rest-api/v1/sessions
Header Content-Type : application/json
Body {
    "username":"USER_NAME",
    "password":"PASSWORD"
}

The request contains user data: USER_NAME and PASSWORD.

Response:

{
    "statusCode": 200,
    "success": true,
    "messages": [],
    "data": {
        "user_id": "ID",
        "fullname": "FULL_NAME",
        "username": "USER_NAME"
    }
}

Refresh access and refresh tokens

Label Value
Method PATCH
URL /tasks-rest-api/v1/sessions/SESSION_ID
Header Content-Type : application/json
Authorization : ACCESS_TOKEN
Body {
    "refresh_token":"REFRESH_TOKEN"
}

The request contains user data: SESSION_ID, ACCESS_TOKEN and REFRESH_TOKEN.

Response:

{
    "statusCode": 200,
    "success": true,
    "messages": ["Token refreshed"],
    "data": {
        "session_id": SESSION_ID,
        "access_token": "ACCESS_TOKEN",
        "access_token_expires_in": ACCESS_TOKEN_EXPIRY,
        "refresh_token": "REFRESH_TOKEN",
        "refresh_token_expires_in": REFRESH_TOKEN_EXPIRY
    }
}

Log out (Delete existing session)

Label Value
Method DELETE
URL /tasks-rest-api/v1/sessions/SESSION_ID
Header Authorization : ACCESS_TOKEN

The request contains user data: SESSION_ID and ACCESS_TOKEN.

Response:

{
    "statusCode": 200,
    "success": true,
    "messages": ["Logged out"],
    "data": {
        "session_id": SESSION_ID
    }
}

Get all tasks

Label Value
Method GET
URL /tasks-rest-api/v1/tasks
Header Authorization : ACCESS_TOKEN

The request contains user data: ACCESS_TOKEN.

Response:

{
    "statusCode": 200,
    "success": true,
    "messages": [],
    "data": {
        "row_returned": TASKS_COUNT,
        "tasks": [
            {
                "id": TASK_ID,
                "title": "TASK_TITLE",
                "description": "TASK_DESCRIPTION",
                "deadline": "TASK_DEADLINE",
                "completed": "TASK_COMPLETED"  // 'Y' or 'N'
            },
            {
                "id": TASK_ID,
                "title": "TASK_TITLE",
                "description": "TASK_DESCRIPTION",
                "deadline": "TASK_DEADLINE",
                "completed": "TASK_COMPLETED"  // 'Y' or 'N'
            }
        ]
    }
}

Create a new task

Label Value
Method POST
URL /tasks-rest-api/v1/tasks
Header Content-Type : application/json
Authorization : ACCESS_TOKEN
Body {
    "title":"TASK_TITLE",
    "description":"TASK_DESCRIPTION",    //optional
    "deadline":"TASK_DEADLINE",    //optional, date format: 01/01/2019 01:00
    "completed":"TASK_COMPLETED"    // N - incompleted, Y - completed
}

The request contains user data: ACCESS_TOKEN, TASK_TITLE, TASK_DESCRIPTION, TASK_DEADLINE and TASK_COMPLETED.

Response:

{
    "statusCode": 201,
    "success": true,
    "messages": ["Task created"],
    "data": {
        "rows_returned": 1,
        "tasks": [
            {
                "id": TASK_ID,
                "title": "TASK_TITLE",
                "description": "TASK_DESCRIPTION",
                "deadline": "TASK_DEADLINE",
                "completed": "TASK_COMPLETED"  // 'Y' or 'N'
            }
        ]
    }
}

Get tasks by page number

Label Value
Method GET
URL /tasks-rest-api/v1/tasks/page/PAGE_NUMBER
Header Authorization : ACCESS_TOKEN

The request contains user data: PAGE_NUMBER and ACCESS_TOKEN.

Response:

{
    "statusCode": 200,
    "success": true,
    "messages": [],
    "data": {
        "row_returned": TASKS_COUNT_ON_PAGE,
        "total_rows": TASKS_COUNT,
        "total_pages": TOTAL_PAGES_COUNT,
        "has_next_page": true|false,
        "has_previous_page": true|false,
        "tasks": [
            {
                "id": TASK_ID,
                "title": "TASK_TITLE",
                "description": "TASK_DESCRIPTION",
                "deadline": "TASK_DEADLINE",
                "completed": "TASK_COMPLETED"  // 'Y' or 'N'
            },
            {
                "id": TASK_ID,
                "title": "TASK_TITLE",
                "description": "TASK_DESCRIPTION",
                "deadline": "TASK_DEADLINE",
                "completed": "TASK_COMPLETED"  // 'Y' or 'N'
            }
        ]
    }
}

Get list of completed tasks

Label Value
Method GET
URL /tasks-rest-api/v1/tasks/complete
Header Authorization : ACCESS_TOKEN

The request contains user data: ACCESS_TOKEN.

Response:

{
    "statusCode": 200,
    "success": true,
    "messages": [],
    "data": {
        "row_returned": TASKS_COUNT,
        "tasks": [
            {
                "id": TASK_ID,
                "title": "TASK_TITLE",
                "description": "TASK_DESCRIPTION",
                "deadline": "TASK_DEADLINE",
                "completed": "TASK_COMPLETED"  // 'Y' or 'N'
            },
            {
                "id": TASK_ID,
                "title": "TASK_TITLE",
                "description": "TASK_DESCRIPTION",
                "deadline": "TASK_DEADLINE",
                "completed": "TASK_COMPLETED"  // 'Y' or 'N'
            }
        ]
    }
}

Get list of incompleted tasks

Label Value
Method GET
URL /tasks-rest-api/v1/tasks/incomplete
Header Authorization : ACCESS_TOKEN

The request contains user data: ACCESS_TOKEN.

Response:

{
    "statusCode": 200,
    "success": true,
    "messages": [],
    "data": {
        "row_returned": TASKS_COUNT,
        "tasks": [
            {
                "id": TASK_ID,
                "title": "TASK_TITLE",
                "description": "TASK_DESCRIPTION",
                "deadline": "TASK_DEADLINE",
                "completed": "TASK_COMPLETED"  // 'Y' or 'N'
            },
            {
                "id": TASK_ID,
                "title": "TASK_TITLE",
                "description": "TASK_DESCRIPTION",
                "deadline": "TASK_DEADLINE",
                "completed": "TASK_COMPLETED"  // 'Y' or 'N'
            }
        ]
    }
}

Get task by ID

Label Value
Method GET
URL /tasks-rest-api/v1/tasks/TASK_ID
Header Authorization : ACCESS_TOKEN

The request contains user data: TASK_ID and ACCESS_TOKEN.

Response:

 "statusCode": 200,
    "success": true,
    "messages": [],
    "data": {
        "rows_returned": 1,
        "tasks": {
            "id": TASK_ID,
            "title": "TASK_TITLE",
            "description": "TASK_DESCRIPTION",
            "deadline": "TASK_DEADLINE",
            "completed": "TASK_COMPLETED"  // 'Y' or 'N'
        }
    }

Update task by ID

Label Value
Method PATCH
URL /tasks-rest-api/v1/tasks/TASK_ID
Header Authorization : ACCESS_TOKEN
Body {
    "title":"TASK_TITLE",
    "description":"TASK_DESCRIPTION",    //optional
    "deadline":"TASK_DEADLINE",    //optional, date format: 01/01/2019 01:00
    "completed":"TASK_COMPLETED"    // N - incompleted, Y - completed
}

The request contains user data: TASK_ID, ACCESS_TOKEN, TASK_TITLE, TASK_DESCRIPTION, TASK_DEADLINE and TASK_COMPLETED..

Response:

{
    "statusCode": 200,
    "success": true,
    "messages": ["Task updated"],
    "data": {
        "rows_returned": 1,
        "tasks": {
            "id": TASK_ID,
            "title": "TASK_TITLE",
            "description": "TASK_DESCRIPTION",
            "deadline": "TASK_DEADLINE",
            "completed": "TASK_COMPLETED"  // 'Y' or 'N'
        }
    }
}

Delete task by ID

Label Value
Method DELETE
URL /tasks-rest-api/v1/tasks/TASK_ID
Header Authorization : ACCESS_TOKEN

The request contains user data: TASK_ID and ACCESS_TOKEN.

Response:

{
    "statusCode": 200,
    "success": true,
    "messages": ["Task deleted"],
    "data": null
}

tasks-rest-api's People

Contributors

alexzhukovich avatar

Stargazers

Andrzej Jóźwiak avatar

Watchers

James Cloos avatar

tasks-rest-api's Issues

Add support for labels

  • Each task can have many labels.
  • Each label provides information about title and colour

Add documentation

  • Describe database structure
  • Describe Use Cases of the API
  • Describe available routes
  • Describe request requirements

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.