Skip to content

Commit 101d859

Browse files
committed
Init Users CRUD
0 parents  commit 101d859

File tree

12 files changed

+5475
-0
lines changed

12 files changed

+5475
-0
lines changed

.babelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env"
4+
]
5+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
*.log

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Prototype project - CRUD users on NODE.js without database ( use json.file as storage)
2+
3+
GET `curl http://localhost:3000/users`
4+
5+
POST `curl -X POST http://localhost:3000/users -H 'Content-Type: application/json' -d '{"name":"Ruslan", "id": "1"}'`
6+
7+
PATCH `curl -X PATCH http://localhost:3000/users/1 -H 'Content-Type: application/json' -d '{"name":"Ruslan changed"}'`
8+
9+
DELETE `curl -X DELETE http://localhost:3000/users/1`
10+
11+
TODO: tests, error handling

app.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import express from 'express'
2+
import App from './controllers/users'
3+
4+
const app = express()
5+
app.use(express.json())
6+
7+
App(app)
8+
9+
app.listen(process.env.PORT || 3000, () => {
10+
console.log('Server runs !!')
11+
})
12+

controllers/users.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { readUsers } from '../store'
2+
import UsersCreate from '../services/users/create'
3+
import UsersUpdate from '../services/users/update'
4+
import UsersDelete from '../services/users/delete'
5+
6+
export default function App(app) {
7+
app.get('/users', (_req, res) => {
8+
const users = readUsers()
9+
return res.send({ success: true, users })
10+
})
11+
12+
app.post('/users', (req, res) => {
13+
const { user } = new UsersCreate(req.body)
14+
return res.send({ success: true, user })
15+
})
16+
17+
app.patch('/users/:id', (req, res) => {
18+
const params = { ...req.body, id: req.params.id }
19+
const { user } = new UsersUpdate(params)
20+
return res.send({ success: true, user })
21+
})
22+
23+
app.delete('/users/:id', (req, res) => {
24+
const { id } = req.params
25+
new UsersDelete(id)
26+
return res.send( { success: true })
27+
})
28+
}

0 commit comments

Comments
 (0)