Skip to content

Commit

Permalink
implement delete post and get user name logics in api b00tc4mp#140
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGonzalo committed May 30, 2024
1 parent e7e20e5 commit ec82307
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 188 deletions.
83 changes: 11 additions & 72 deletions staff/adrian-martin/socialcode/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,27 @@

```sh
🐖 curl http://localhost:8080/users -v
```
``

- register user

```sh
🐖 curl -X POST http://localhost:8080/users -H "Content-Type: application/json" -d '{"name":"Pepito","surname":"Grillo","email":"[email protected]","username":"pepitogrillo","password":"123123123","passwordRepeat":"123123123"}' -v
```

```js
const xhr = new XMLHttpRequest

xhr.onload = () => {
debugger

if (xhr.status === 201) {
console.log('user registered')

return
}

const { error, message } = JSON.parse(xhr.response)

console.error(error, message)
}

xhr.open('POST', 'http://localhost:8080/users')

const body = {name:'Peter',surname:'Grillo',email:'[email protected]',username:'pepitogrillo',password:'123123123', passwordRepeat:'123123123'}

const json = JSON.stringify(body)

xhr.setRequestHeader('Content-Type', 'application/json')
xhr.send(json)
```
- authenticate user

```sh
🐖 curl -X POST http://localhost:8080/users/auth -H "Content-Type: application/json" -d '{"username":"AdrianGon","password":"321321321"}' -v
```
```js
const xhr = new XMLHttpRequest

xhr.onload = () => {
debugger

if (xhr.status === 200) {
console.log('user authenticated')

return
}

const { error, message } = JSON.parse(xhr.response)

console.error(error, message)
}

xhr.open('POST', 'http://localhost:8080/users/auth')
- get user name

const body = {username:'pepitogrillo',password:'123123123'}

const json = JSON.stringify(body)

xhr.setRequestHeader('Content-Type', 'application/json')
xhr.send(json)
```sh
🐖 curl http://localhost:8080/users/AdrianGon -H "Authorization: Basic AdrianGon" -v
```
- list posts

```js
const xhr = new XMLHttpRequest

xhr.onload = () => {
if (xhr.status === 200) {
const posts = JSON.parse(xhr.response)

console.log('posts retrived', posts)

return
}

const { error, message } = JSON.parse(xhr.response)

console.error(error, message)
}

xhr.open('GET', 'http://localhost:8080/posts')
- list posts

xhr.send()
```
```sh
🐖 curl http://localhost:8080/posts -v
```
Expand All @@ -98,4 +31,10 @@ xhr.send()

```sh
🐖 $ curl -X POST http://localhost:8080/posts -H "Authorization: Basic AdrianGon" -H "Content-Type: application/json" -d '{"title":"Buenos Dias","image":"https://imgs.search.brave.com/J8imP8bduv7lIhsfCsnGwaeKNgdHqp2g5dCpK8aVkYA/rs:fit:860:0:0/g:ce/aHR0cHM6Ly90My5m/dGNkbi5uZXQvanBn/LzAxLzAzLzM2LzA0/LzM2MF9GXzEwMzM2/MDQyNV9IekJxZEkx/d1dOTjBWU3Z2ZXkw/R3RRTlRvaDJLenhy/Ny5qcGc","description":"blah blah"}' -v
```

- delete post

```sh
🐖 $ curl -X DELETE http://localhost:8080/posts/7956804321626201-1716989998980 -H "Authorization: Basic AdrianGon" -v
```
16 changes: 16 additions & 0 deletions staff/adrian-martin/socialcode/api/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,23 @@ data.findPosts = (condition, callback) => {
})
}

data.findPost = (condition, callback) => {
fs.readFile('./data/posts.json', 'utf8', (error, json) => {
if (error) {
callback(new SystemError(error.message))

return
}

if (!json) json = '[]'

const posts = JSON.parse(json)

const post = posts.find(condition)

callback(null, post)
})
}


data.insertPost = (post, callback) => {
Expand Down
2 changes: 1 addition & 1 deletion staff/adrian-martin/socialcode/api/data/posts.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"author":"jameshook","title":"smile1","image":"https://m.media-amazon.com/images/I/41xsPjrM-pL._AC_UF350,350_QL50_.jpg","description":"hi 2","date":"2024-05-27T14:44:14.236Z","id":"8448820043690848-1716821054237"},{"author":"jameshook","title":"smile2","image":"https://m.media-amazon.com/images/I/41xsPjrM-pL._AC_UF350,350_QL50_.jpg","description":"hi 2","date":"2024-05-27T14:44:21.737Z","id":"05670034285101688-1716821061738"},{"author":"jameshook","title":"smile3","image":"https://m.media-amazon.com/images/I/41xsPjrM-pL._AC_UF350,350_QL50_.jpg","description":"hi 2","date":"2024-05-27T14:44:23.250Z","id":"549161270915079-1716821063251"},{"id":"7049627815708344-1716989932607","author":"AdrianGon","title":"smil3","image":"https://imgs.search.brave.com/rY4vd7ChrTffot87xezWVyJZcsjp10UPNHx2EQMRCfs/rs:fit:860:0:0/g:ce/aHR0cHM6Ly9pLnBp/bmltZy5jb20vb3Jp/Z2luYWxzLzFiL2Qx/L2I2LzFiZDFiNjE1/ZTZkYTcwZGQ3MWRj/ODRmZDJmNDdjODBk/LmpwZw","description":"hi 2","date":"2024-05-29T13:38:52.607Z"},{"id":"11703272214754645-1716989975016","author":"AdrianGon","title":"smil3","image":"https://imgs.search.brave.com/rY4vd7ChrTffot87xezWVyJZcsjp10UPNHx2EQMRCfs/rs:fit:860:0:0/g:ce/aHR0cHM6Ly9pLnBp/bmltZy5jb20vb3Jp/Z2luYWxzLzFiL2Qx/L2I2LzFiZDFiNjE1/ZTZkYTcwZGQ3MWRj/ODRmZDJmNDdjODBk/LmpwZw","description":"hi 2","date":"2024-05-29T13:39:35.015Z"},{"id":"7956804321626201-1716989998965","author":"AdrianGon","title":"smile4","image":"https://imgs.search.brave.com/rY4vd7ChrTffot87xezWVyJZcsjp10UPNHx2EQMRCfs/rs:fit:860:0:0/g:ce/aHR0cHM6Ly9pLnBp/bmltZy5jb20vb3Jp/Z2luYWxzLzFiL2Qx/L2I2LzFiZDFiNjE1/ZTZkYTcwZGQ3MWRj/ODRmZDJmNDdjODBk/LmpwZw","description":"hi 2","date":"2024-05-29T13:39:58.965Z"},{"author":"AdrianGon","title":"Buenos Dias","image":"https://imgs.search.brave.com/J8imP8bduv7lIhsfCsnGwaeKNgdHqp2g5dCpK8aVkYA/rs:fit:860:0:0/g:ce/aHR0cHM6Ly90My5m/dGNkbi5uZXQvanBn/LzAxLzAzLzM2LzA0/LzM2MF9GXzEwMzM2/MDQyNV9IekJxZEkx/d1dOTjBWU3Z2ZXkw/R3RRTlRvaDJLenhy/Ny5qcGc","description":"blah blah","date":"2024-05-29T13:55:28.417Z","id":"7707579376266043-1716990928418"},{"author":"AdrianGon","title":"Buenos Dias","image":"https://imgs.search.brave.com/J8imP8bduv7lIhsfCsnGwaeKNgdHqp2g5dCpK8aVkYA/rs:fit:860:0:0/g:ce/aHR0cHM6Ly90My5m/dGNkbi5uZXQvanBn/LzAxLzAzLzM2LzA0/LzM2MF9GXzEwMzM2/MDQyNV9IekJxZEkx/d1dOTjBWU3Z2ZXkw/R3RRTlRvaDJLenhy/Ny5qcGc","description":"blah blah","date":"2024-05-29T14:01:59.825Z","id":"7741423721689134-1716991319825"},{"author":"AdrianGon","title":"Adrian","image":"https://imgs.search.brave.com/2TUrjrEEig4i0QI4ygiyBBQipd90pfaTC6MV2CYvsjg/rs:fit:500:0:0/g:ce/aHR0cHM6Ly93d3cu/aGFpa3Utb3Mub3Jn/L2RvY3MvdXNlcmd1/aWRlL2VzL2ltYWdl/cy9hcHBzLWltYWdl/cy9kZWJ1Z2dlci5w/bmc","description":"debugger","date":"2024-05-29T14:16:48.728Z","id":"9617450442045541-1716992208729"}]
[{"author":"jameshook","title":"smile1","image":"https://m.media-amazon.com/images/I/41xsPjrM-pL._AC_UF350,350_QL50_.jpg","description":"hi 2","date":"2024-05-27T14:44:14.236Z","id":"8448820043690848-1716821054237"},{"author":"jameshook","title":"smile2","image":"https://m.media-amazon.com/images/I/41xsPjrM-pL._AC_UF350,350_QL50_.jpg","description":"hi 2","date":"2024-05-27T14:44:21.737Z","id":"05670034285101688-1716821061738"},{"author":"jameshook","title":"smile3","image":"https://m.media-amazon.com/images/I/41xsPjrM-pL._AC_UF350,350_QL50_.jpg","description":"hi 2","date":"2024-05-27T14:44:23.250Z","id":"549161270915079-1716821063251"},{"id":"7049627815708344-1716989932607","author":"AdrianGon","title":"smil3","image":"https://imgs.search.brave.com/rY4vd7ChrTffot87xezWVyJZcsjp10UPNHx2EQMRCfs/rs:fit:860:0:0/g:ce/aHR0cHM6Ly9pLnBp/bmltZy5jb20vb3Jp/Z2luYWxzLzFiL2Qx/L2I2LzFiZDFiNjE1/ZTZkYTcwZGQ3MWRj/ODRmZDJmNDdjODBk/LmpwZw","description":"hi 2","date":"2024-05-29T13:38:52.607Z"},{"id":"11703272214754645-1716989975016","author":"AdrianGon","title":"smil3","image":"https://imgs.search.brave.com/rY4vd7ChrTffot87xezWVyJZcsjp10UPNHx2EQMRCfs/rs:fit:860:0:0/g:ce/aHR0cHM6Ly9pLnBp/bmltZy5jb20vb3Jp/Z2luYWxzLzFiL2Qx/L2I2LzFiZDFiNjE1/ZTZkYTcwZGQ3MWRj/ODRmZDJmNDdjODBk/LmpwZw","description":"hi 2","date":"2024-05-29T13:39:35.015Z"},{"id":"7956804321626201-1716989998965","author":"AdrianGon","title":"smile4","image":"https://imgs.search.brave.com/rY4vd7ChrTffot87xezWVyJZcsjp10UPNHx2EQMRCfs/rs:fit:860:0:0/g:ce/aHR0cHM6Ly9pLnBp/bmltZy5jb20vb3Jp/Z2luYWxzLzFiL2Qx/L2I2LzFiZDFiNjE1/ZTZkYTcwZGQ3MWRj/ODRmZDJmNDdjODBk/LmpwZw","description":"hi 2","date":"2024-05-29T13:39:58.965Z"}]
56 changes: 40 additions & 16 deletions staff/adrian-martin/socialcode/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,24 @@ api.post('/users/auth', jsonBodyParser, (req, res) => {
}
})

// api.post('/home', jsonBodyParser, (req, res) => {
// const {username} = req.body
api.get('/users/:targetUsername', (req, res) => {
const username = req.headers.authorization.slice(6)

// try {
// logic.findUser(username, error => {
// if (error) {
// res.status(500).json({error: error.constructor.name, message: error.message})

// return
// }
// res.status(200).send()
// })
// } catch (error) {
// res.status(500).json({error: error.constructor.name, message: error.message})
// }
// })
const { targetUsername } = req.params

try {
logic.getUserName(username, targetUsername, (error, name) => {
if(error){
res.status(500).json({error: error.constructor.name, message: error.message})

return
}
res.json(name)
})
} catch (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })
}
})


api.get('/posts', (req, res) => {
Expand All @@ -80,7 +82,7 @@ api.get('/posts', (req, res) => {
})
} catch (error) {
res.status(500).json({ error: error.constructor.name, message: error.message })
}logic
}
})


Expand All @@ -105,6 +107,28 @@ api.post('/posts', jsonBodyParser, (req, res) => {
}
})


api.delete('/posts/:postId', (req, res) => {
const username = req.headers.authorization.slice(6)

const { postId } = req.params


try {
logic.deletePost(username, postId, error => {
if (error) {
res.status(500).json({error: error.constructor.name, message: error.message})

return
}

res.status(204).send()
})
} catch (error) {
console.error(error)
}
})

api.listen(8080, () => console.log('api is up'))

//?--------------------------------------- jsonBodyParser manually
Expand Down
Loading

0 comments on commit ec82307

Please sign in to comment.