Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
be50795
Init npm, add nodemon and express
yulsmir May 16, 2023
0ea3b8d
Add basic routes
yulsmir May 16, 2023
ed871aa
Add todos.json
yulsmir May 16, 2023
1c7311f
Add basic routing
yulsmir May 16, 2023
9ed5bae
Add get for one item
yulsmir May 16, 2023
2526c0c
Fix get item by id
yulsmir May 16, 2023
7c277d9
Add start file to package.json, fix index.js
yulsmir May 17, 2023
daaa557
Change todos.json structure
yulsmir May 17, 2023
c029ecc
Fix get todo by id
yulsmir May 17, 2023
34c1322
Rewrite get all todos
yulsmir May 17, 2023
740926f
Rewrite get todo by id
yulsmir May 17, 2023
3417b53
Add status codes & error messages for get request
yulsmir May 17, 2023
b9d9f14
Add delete todo by id
yulsmir May 17, 2023
cf2ebe4
Remove commented old code
yulsmir May 17, 2023
b77a1f2
Move routes and controllers into todosRoutes.js and todosController.js
yulsmir May 17, 2023
81c59ac
Fix routes in todosRoutes.js for get and delete by id
yulsmir May 17, 2023
193094d
Add POST todo
yulsmir May 17, 2023
40db82f
Add PATCH todo
yulsmir May 17, 2023
5fc6bae
Add TODO: comments
yulsmir May 17, 2023
68658be
Create todo.js model
yulsmir May 17, 2023
592025a
Create handlers for errors and file read/write, move repeated code in…
yulsmir May 17, 2023
1ff8a3b
Update README.md
yulsmir May 18, 2023
04a88b9
Fix return statement for every handleFileReadError
yulsmir May 21, 2023
e91aead
Fix return statement for every handleFileWriteError in fileHandlers.js
yulsmir May 21, 2023
880f4fd
Remove console.error(err) from errorHandlers
yulsmir May 23, 2023
364d718
Destruct controller methods
yulsmir May 23, 2023
c05be44
Move todos.json into data, fix path in todosControllers.js
yulsmir May 23, 2023
b26b64d
Fix paths of utilities files
yulsmir May 23, 2023
98aab6d
Fix file paths for utilities
yulsmir May 23, 2023
cce9750
Move errorHandler to middleware folder, rename path
yulsmir May 23, 2023
bdfe12a
Update files and folders in readme.md
yulsmir May 23, 2023
c8c78ca
Fix readme.md
yulsmir May 23, 2023
c93237f
Install uuid package, add uniqueIdController.file
yulsmir May 23, 2023
391dba4
Add unique id for a new post todo item
yulsmir May 23, 2023
f982b65
Fix readme file: add more id numbers
yulsmir May 23, 2023
5c330f6
Fix typo in readme file: todoRoutes => todosRoutes
yulsmir May 23, 2023
3e8edeb
Fix endpoint routes in readme.md
yulsmir May 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [

{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"program": "${file}"
}
]
}
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# todo-express-api
Todo api with express module

### Short description
- NB: skip the database and use an array to save the todos
- Todo is an object with id: number, title: string, completed: boolean
- Data type validation

### Endpoints implemented

- [X] GET /todos ---> List all todos
- [X] GET /todos/1 ----> List only single todo 1 in this case is just example of id but it should work with any id stored
- [X] POST /todos ---> create new todo
- [X] PATCH /todos/1 ---> update existing todo using the id
- [X] DELETE /todos/1 ---> removes existing todo using the id

### Files
- utilities
- todosController.js - all requests - GET, POST, DELETE, PATCH requests
- fileController.js - readTodosFile, writeTodosFile
- middleware
- errorHandler.js - handleFileReadError, handleFileWriteError
- modules
- todo.js - class Todo
- routes
- todosRoutes.js - routes for all GET, POST, DELETE, PATCH requests
- data
- todos.json - file where all todos are stored and available upon request

## Project setup
1. Clone repo
2. Run ```npm install``` to install dependencies
3. Run ```nodemon run``` or ```nodemon index.js```to start server
4. Open in browser ```localhost:3000``` or ```127.0.0.1:3000```
5. Check urls and response results in dev tools:
- ```localhost:3000/todos``` - shows all todos
- ```localhost:3000/todos/?id=``` + add number 1-12 to show one existing item in a todo list
- use Thunder Client or Postman to check POST/PATCH/DELETE requests

62 changes: 62 additions & 0 deletions data/todos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"id": 1,
"title": "Updated",
"completed": false
},
{
"id": 2,
"title": "Title 2",
"completed": false
},
{
"id": 3,
"title": "Title 3",
"completed": false
},
{
"id": 4,
"title": "Title 4",
"completed": false
},
{
"id": 5,
"title": "Title 5",
"completed": false
},
{
"id": 6,
"title": "Title 6",
"completed": false
},
{
"id": 7,
"title": "Title 7",
"completed": false
},
{
"id": 8,
"title": "Title 8",
"completed": false
},
{
"id": 9,
"title": "Title 9",
"completed": false
},
{
"id": 10,
"title": "Updated again",
"completed": false
},
{
"id": 11,
"title": "Title 11",
"completed": false
},
{
"id": 12,
"title": "Title 12",
"completed": false
}
]
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const express = require('express');
const app = express();
const PORT = 3000;
const todosRoutes = require('./routes/todosRoutes');

// Middleware
app.use(express.json());

// Define routes
app.use(todosRoutes);

app.listen(PORT, () => {
console.log(`Listening port on ${PORT}`);
});
14 changes: 14 additions & 0 deletions middleware/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const handleFileReadError = (err, res) => {
res.status(500).json({ error: 'Failed to read todos file.' });
};

const handleFileWriteError = (err, res) => {
res.status(500).json({ error: 'Failed to write todos file.' });
};

module.exports = {
handleFileReadError,
handleFileWriteError,
};
9 changes: 9 additions & 0 deletions models/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Todo {
constructor(id, title, completed) {
this.id = id;
this.title = title;
this.completed = completed;
}
}

module.exports = Todo;
Loading