Last active
June 9, 2022 12:55
-
-
Save ehudthelefthand/e630b1565a70fddbab57511f6c28ae4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// package.json | |
{ | |
"name": "todo-api", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "nodemon todos.js" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"dvali": "^0.2.7", | |
"express": "^4.18.1", | |
"nodemon": "^2.0.16", | |
"uuid": "^8.3.2" | |
} | |
} | |
// server.js | |
const express = require('express') | |
const { v4: uuid } = require('uuid') | |
const { validate, isString, minLength, isBool } = require('dvali') | |
const app = express() | |
const port = 4444 | |
// Todo | |
// - ID: UUID | |
// - Name: String | |
// - Completed: Boolean | |
const validateName = validate([ isString(), minLength(3) ]) | |
let Todos = [] | |
app.use(express.json()) | |
app.get('/todos', (req, res) => { | |
res.json(Todos) | |
}) | |
app.post('/todos', async (req, res) => { | |
try { | |
const { name } = req.body | |
const validatedName = await validateName(name) | |
const todo = { | |
id: uuid(), | |
name: validatedName, | |
completed: false | |
} | |
Todos = [...Todos, todo] | |
res.sendStatus(201) | |
} catch (err) { | |
console.log(err) | |
res.status(422).json({ | |
message: err | |
}) | |
} | |
}) | |
app.get('/todos/:id', (req, res) => { | |
const id = req.params.id | |
const founds = Todos.filter(todo => todo.id === id) | |
if (founds.length === 0) { | |
res.sendStatus(404) | |
return | |
} | |
res.json(founds[0]) | |
}) | |
app.delete('/todos/:id', (req, res) => { | |
const id = req.params.id | |
Todos = Todos.filter(todo => todo.id !== id) | |
res.sendStatus(204) | |
}) | |
const validateCompleted = validate([ isBool() ]) | |
const validateUpdateTodo = validate({ | |
name: validateName, | |
completed: validateCompleted | |
}) | |
app.put('/todos/:id', async (req, res) => { | |
const id = req.params.id | |
const founds = Todos.filter(todo => todo.id === id) | |
if (founds.length === 0) { | |
res.sendStatus(404) | |
return | |
} | |
const newState = Todos.filter(todo => todo.id !== id) | |
const { name, completed } = req.body | |
try { | |
const update = { name, completed } | |
const validated = await validateUpdateTodo(update) | |
const newTodo = { ...founds[0], ...validated } | |
Todos = [...newState, newTodo ] | |
res.sendStatus(204) | |
} catch (err) { | |
console.log(err) | |
console.log(err.toString()) | |
res.status(422).json({ | |
message: err | |
}) | |
} | |
}) | |
app.listen(port, () => { | |
console.log(`server started at http://localhost:${port}`) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment