Todo Application CP 8

 

Todo Application

Given an

app.js
file and an empty database file
todoApplication.db
.

Create a table with the name

todo
with the following columns,

Todo Table

ColumnType
idINTEGER
todoTEXT
priorityTEXT
statusTEXT

and write APIs to perform operations on the table

todo
,

Note
  • Replace the spaces in URL with 
    %20
    .
  • Possible values for 
    priority
     are 
    HIGH
    MEDIUM
    , and 
    LOW
    .
  • Possible values for 
    status
     are 
    TO DO
    IN PROGRESS
    , and 
    DONE
    .

API 1

Path: 
/todos/

Method: 
GET

  • Scenario 1

    • Sample API 
      /todos/?status=TO%20DO
    • Description:

      Returns a list of all todos whose status is 'TO DO'

    • Response

      [
      {
      id: 1,
      todo: "Watch Movie",
      priority: "LOW",
      status: "TO DO"
      },
      ...
      ]
  • Scenario 2

    • Sample API 
      /todos/?priority=HIGH
    • Description:

      Returns a list of all todos whose priority is 'HIGH'

    • Response

      [
      {
      id: 2,
      todo: "Learn Node JS",
      priority: "HIGH",
      status: "IN PROGRESS"
      },
      ...
      ]
  • Scenario 3

    • Sample API 
      /todos/?priority=HIGH&status=IN%20PROGRESS
    • Description:

      Returns a list of all todos whose priority is 'HIGH' and status is 'IN PROGRESS'

    • Response

      [
      {
      id: 2,
      todo: "Learn Node JS",
      priority: "HIGH",
      status: "IN PROGRESS"
      },
      ...
      ]
  • Scenario 4

    • Sample API 
      /todos/?search_q=Play
    • Description:

      Returns a list of all todos whose todo contains 'Play' text

    • Response

      [
      {
      id: 4,
      todo: "Play volleyball",
      priority: "MEDIUM",
      status: "DONE"
      },
      ...
      ]

API 2

Path: 
/todos/:todoId/

Method: 
GET

Description:

Returns a specific todo based on the todo ID

Response

{
id: 2,
todo: "Learn JavaScript",
priority: "HIGH",
status: "DONE"
}

API 3

Path: 
/todos/

Method: 
POST

Description:

Create a todo in the todo table,

Request

{
"id": 10,
"todo": "Finalize event theme",
"priority": "LOW",
"status": "TO DO"
}

Response

Todo Successfully Added

API 4

Path: 
/todos/:todoId/

Method: 
PUT

Description:

Updates the details of a specific todo based on the todo ID

  • Scenario 1

    • Request 
      { "status": "DONE" }
    • Response

      Status Updated
  • Scenario 2

    • Request 
      { "priority": "HIGH" }
    • Response

      Priority Updated
  • Scenario 3

    • Request 
      { "todo": "Some task" }
    • Response

      Todo Updated

API 5

Path: 
/todos/:todoId/

Method: 
DELETE

Description:

Deletes a todo from the todo table based on the todo ID

Response

Todo Deleted

Use

npm install
to install the packages.

Export the express instance using the default export syntax.

Use Common JS module syntax.


// Import Modules
const express = require("express");
const { open } = require("sqlite");
const sqlite3 = require("sqlite3");
const path = require("path");

// database Path
const databasePath = path.join(__dirname"todoApplication.db");

// Calling Express and accept JSON file
const app = express();
app.use(express.json());

// Database Initialization
let database = null;

const databaseInitialization = async () => {
  try {
    database = await open({
      filenamedatabasePath,
      driver: sqlite3.Database,
    });
    app.listen(3000, () =>
      console.log("Server is running at: http://localhost:3000")
    );
  } catch (error) {
    console.log(error.message);
    process.exit(1);
  }
};
databaseInitialization();

// API 1 Scenario 1
const hasPriorityAndStatusProperties = (requestQuery=> {
  return (
    requestQuery.priority !== undefined && requestQuery.status !== undefined
  );
};

const hasPriorityProperty = (requestQuery=> {
  return requestQuery.priority !== undefined;
};

const hasStatusProperty = (requestQuery=> {
  return requestQuery.status !== undefined;
};

app.get("/todos/"async (requestresponse=> {
  let data = null;
  let getTodosQuery = "";
  const { search_q = ""prioritystatus } = request.query;

  switch (true) {
    case hasPriorityAndStatusProperties(request.query): //if this is true then below query is taken in the code
      getTodosQuery = `
   SELECT
    *
   FROM
    todo 
   WHERE
    todo LIKE '%${search_q}%'
    AND status = '${status}'
    AND priority = '${priority}';`;
      break;
    case hasPriorityProperty(request.query):
      getTodosQuery = `
   SELECT
    *
   FROM
    todo 
   WHERE
    todo LIKE '%${search_q}%'
    AND priority = '${priority}';`;
      break;
    case hasStatusProperty(request.query):
      getTodosQuery = `
   SELECT
    *
   FROM
    todo 
   WHERE
    todo LIKE '%${search_q}%'
    AND status = '${status}';`;
      break;
    default:
      getTodosQuery = `
   SELECT
    *
   FROM
    todo 
   WHERE
    todo LIKE '%${search_q}%';`;
  }

  data = await database.all(getTodosQuery);
  response.send(data);
});

// API 2
app.get("/todos/:todoId/"async (requestresponse=> {
  const { todoId } = request.params;
  const queryPerId = `
    SELECT *
    FROM todo
    WHERE id = '${todoId}';`;
  const getDataAsPerId = await database.get(queryPerId);
  response.send(getDataAsPerId);
});

// API 3
app.post("/todos/"async (requestresponse=> {
  const { idtodoprioritystatus } = request.body;
  const queryToPostDatabase = `
  INSERT INTO todo(id, todo, priority, status)
  VALUES ('${id}', '${todo}', '${priority}', '${status}');`;
  await database.run(queryToPostDatabase);
  response.send("Todo Successfully Added");
});

// API 4
app.put("/todos/:todoId/"async (requestresponse=> {
  const { todoId } = request.params;
  let updateColumn = "";
  const requestBody = request.body;
  switch (true) {
    case requestBody.status !== undefined:
      updateColumn = "Status";
      break;
    case requestBody.priority !== undefined:
      updateColumn = "Priority";
      break;
    case requestBody.todo !== undefined:
      updateColumn = "Todo";
      break;
  }
  const previousTodoQuery = `
    SELECT
      *
    FROM
      todo
    WHERE 
      id = ${todoId};`;
  const previousTodo = await database.get(previousTodoQuery);

  const {
    todo = previousTodo.todo,
    priority = previousTodo.priority,
    status = previousTodo.status,
  } = request.body;

  const updateTodoQuery = `
    UPDATE
      todo
    SET
      todo='${todo}',
      priority='${priority}',
      status='${status}'
    WHERE
      id = ${todoId};`;

  await database.run(updateTodoQuery);
  response.send(`${updateColumn} Updated`);
});

// API 5
app.delete("/todos/:todoId/"async (requestresponse=> {
  const { todoId } = request.params;
  const queryToDelete = `
  DELETE FROM todo
  WHERE id = ${todoId};`;
  await database.run(queryToDelete);
  response.send("Todo Deleted");
});

module.exports = app;


Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form