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.
    1
    {
    2
    "items": []
    3
    }
    Copied!
  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

1
node index.js show
Copied!

Sample Output

1
- wash the car
2
- cook dinner
3
- buy bananas
Copied!

Add Item

Add an item to the list.

Sample Command

1
node index.js add "send flowers"
Copied!

Sample Output

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

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

1
To-Do:
2
1. wash the car
3
2. cook dinner
4
3. buy bananas
5
​
6
Done:
Copied!

Sample Command

1
node index.js complete 2
Copied!

Sample Complete Command Output

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

Sample Subsequent Show Command Output

1
To-Do:
2
1. wash the car
3
2. buy bananas
4
​
5
Done:
6
1. cook dinner
Copied!

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.
1
/**
2
* Remove an element from an array in JSON
3
* @param {string} filename
4
* @param {string} key - The name of the key of the array we wish to edit
5
* @param {number} index - The index of the array we wish to delete from
6
* @param {function} callback - The callback function to call after removing
7
* @returns undefined
8
*/
9
remove(
10
'data.json',
11
'items',
12
2,
13
(dataWithoutItem) => {
14
// ...
15
}
16
);
17
​
18
/**
19
* Edit the properties of an element in an array in JSON
20
* @param {string} filename
21
* @param {string} key - The name of the key of the array we wish to edit
22
* @param {number} index - The index of the array we wish to edit
23
* @param {object} payload - The attributes we wish to add to the element
24
* @param {function} callback - The callback function to call after editing
25
* @returns undefined
26
*/
27
editOneElement(
28
'data.json',
29
'items',
30
2,
31
{ text: 'buy soap and toothpaste' },
32
(dataWithEditedItem) => {
33
// ...
34
}
35
);
Copied!

Remove Item

Remove the item that corresponds to the given number.

Sample Show Command Output

1
To-Do:
2
1. wash the car
3
2. cook dinner
4
3. buy bananas
5
​
6
Done:
Copied!

Sample Remove Command

1
node index.js remove 2
Copied!

Sample Remove Command Output

1
I have removed Item 2, "cook dinner".
Copied!

Sample Subsequent Show Command Output

1
To-Do:
2
1. wash the car
3
2. buy bananas
4
​
5
Done:
Copied!

Edit Item

Update the text of the given item.

Sample Show Command Output

1
To-Do:
2
1. wash the car
3
2. cook dinner
4
3. buy bananas
5
​
6
Done:
Copied!

Sample Edit Command

1
node index.js edit 2 "cook supper"
Copied!

Sample Edit Command Output

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

Sample Subsequent Show Command Output

1
To-Do:
2
1. wash the car
3
2. cook supper
4
3. buy bananas
5
​
6
Done:
Copied!

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.