Bootcamp
Search…
2.POCE.6: To-Do List - JSON DB

Introduction

Create a command line to-do list app using helper functions from our jsonFileStorage module and a local JSON file data.json as our database.

Base

  1. 1.
  2. 2.
    Initialise data.json with the following contents.
    {
    "items": []
    }
  3. 3.
    Build the to-do app to support show and add commands as described below.

Show Items

Show all to-do items in the list.

Sample Command

node index.js show

Sample Output

- wash the car
- cook dinner
- buy bananas

Add Item

Add an item to the list.

Sample Command

node index.js add "send flowers"

Sample Output

I have added "send flowers" to your to-do list.

Comfortable

Complete Item

  1. 1.
    Update the app to support a complete command that marks a to-do item complete.
    1. 1.
      The user should be able to specify the number of a given to-do item to mark it as complete.
  2. 2.
    Update the output of the show command to be a numbered list, and to have a Done section below with all the completed to-dos.
    1. 1.
      The number of each to-do can correspond to its index in the to-do array.

Sample Show Command Output

To-Do:
1. wash the car
2. cook dinner
3. buy bananas
​
Done:

Sample Command

node index.js complete 2

Sample Complete Command Output

I have marked item 2, "cook dinner" as complete.

Sample Subsequent Show Command Output

To-Do:
1. wash the car
2. buy bananas
​
Done:
1. cook dinner

More Comfortable

Add remove and editOneElement to jsonFileStorage

  1. 1.
    Add remove and editOneElement functions to jsonFileStorage to support removing and editing existing to-do items (and array elements in general for other apps).
  2. 2.
    The following are sample ways to design the functions in jsonFileStorage.
/**
* Remove an element from an array in JSON
* @param {string} filename
* @param {string} key - The name of the key of the array we wish to edit
* @param {number} index - The index of the array we wish to delete from
* @param {function} callback - The callback function to call after removing
* @returns undefined
*/
remove(
'data.json',
'items',
2,
(dataWithoutItem) => {
// ...
}
);
​
/**
* Edit the properties of an element in an array in JSON
* @param {string} filename
* @param {string} key - The name of the key of the array we wish to edit
* @param {number} index - The index of the array we wish to edit
* @param {object} payload - The attributes we wish to add to the element
* @param {function} callback - The callback function to call after editing
* @returns undefined
*/
editOneElement(
'data.json',
'items',
2,
{ text: 'buy soap and toothpaste' },
(dataWithEditedItem) => {
// ...
}
);

Remove Item

Remove the item that corresponds to the given number.

Sample Show Command Output

To-Do:
1. wash the car
2. cook dinner
3. buy bananas
​
Done:

Sample Remove Command

node index.js remove 2

Sample Remove Command Output

I have removed Item 2, "cook dinner".

Sample Subsequent Show Command Output

To-Do:
1. wash the car
2. buy bananas
​
Done:

Edit Item

Update the text of the given item.

Sample Show Command Output

To-Do:
1. wash the car
2. cook dinner
3. buy bananas
​
Done:

Sample Edit Command

node index.js edit 2 "cook supper"

Sample Edit Command Output

I have edited Item 2, "cook dinner" to be "cook supper".

Sample Subsequent Show Command Output

To-Do:
1. wash the car
2. cook supper
3. buy bananas
​
Done:

Created and Completed Dates

  1. 1.
    Add dates to the DB to track when each item was added and completed.
  2. 3.
    Show date values in show command output.

Reference solution

Click here to see the reference solution for this post class.
Copy link
On this page
Introduction
Base
Show Items
Add Item
Comfortable
Complete Item
More Comfortable
Add remove and editOneElement to jsonFileStorage
Remove Item
Edit Item
Created and Completed Dates
Reference solution