Skip to content

Commit

Permalink
creation of the API folder and modification of the data and logic fol…
Browse files Browse the repository at this point in the history
…ders to work with a server b00tc4mp#150
  • Loading branch information
Johnnyrc26 committed May 30, 2024
1 parent 79e286f commit 9e1a345
Show file tree
Hide file tree
Showing 51 changed files with 2,195 additions and 1 deletion.
83 changes: 83 additions & 0 deletions staff/johnny-rojas/socialcode/api/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
- list users

```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]',bodyname:'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":"pepitogrillo","password":"123123123"}' -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')

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

const json = JSON.stringify(body)

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

- list posts

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

- create post

```sh
🐖 curl -X POST http://localhost:8080/posts -H "Authorization: Basic Flash" -H "Content-Type: application/json" -d '{"title":"blah","image":"https://upload.wikimedia.org/wikipedia/commons/1/1d/Blah_Blah_Blah.jpg","description":"blah blah"}' -v
```
150 changes: 150 additions & 0 deletions staff/johnny-rojas/socialcode/api/data/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import fs from 'fs'
import { SystemError } from '../errors.js'

const data = {}

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

return
}

if (!json) json = '[]'

const users = JSON.parse(json)

const user = users.find(condition)

callback(null, user)
})
}

data.insertUser = (user, callback) => {
fs.readFile('./data/users.json', 'utf8', (error, json) => {
if (error) {
callback(new SystemError(error.message))

return
}

if (!json) json = '[]'

const users = JSON.parse(json)

users.push(user)

const newJson = JSON.stringify(users)

fs.writeFile('./data/users.json', newJson, error => {
if (error) {
callback(new SystemError(error.message))

return
}

callback(null)
})
})
}

data.findPosts = (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 filtered = posts.filter(condition)

callback(null, filtered)
})
}

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) => {
fs.readFile('./data/posts.json', 'utf8', (error, json) => {
if (error) {
callback(new SystemError(error.message))

return
}

if (!json) json = '[]'

const posts = JSON.parse(json)

post.id = `${Math.random().toString().slice(2)}-${Date.now()}`

posts.push(post)

const newJson = JSON.stringify(posts)

fs.writeFile('./data/posts.json', newJson, error => {
if (error) {
callback(new SystemError(error.message))

return
}

callback(null)
})
})
}

data.deletePost = (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 index = posts.findIndex(condition)

if (index > -1) {
posts.splice(index, 1)

const newJson = JSON.stringify(posts)

fs.writeFile('./data/posts.json', newJson, error => {
if (error) {
callback(new SystemError(error.message))

return
}

callback(null)
})
} else callback(null)
})
}

export default data
56 changes: 56 additions & 0 deletions staff/johnny-rojas/socialcode/api/data/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import data from './index.js'

// data.findUser(user => user.surname === 'Grillo', (error, user) => {
// if (error) {
// console.error(error)

// return
// }

// console.log(user)
// })

// data.insertUser({ name: 'James', surname: 'Hook', email: '[email protected]', username: 'jameshook', password: '123123123' }, error => {
// if (error) {
// console.error(error)

// return
// }

// console.log('user inserted')
// })

// data.findPosts(post => post.date.includes('T19'), (error, posts) => {
// if (error) {
// console.error(error)

// return
// }

// console.log(posts)
// })

// data.insertPost({
// author: 'jameshook',
// title: 'smile 2',
// image: 'https://m.media-amazon.com/images/I/41xsPjrM-pL._AC_UF350,350_QL50_.jpg', description: 'hi 2',
// date: new Date().toISOString()
// }, error => {
// if (error) {
// console.error(error)

// return
// }

// console.log('post inserted')
// })

data.deletePost(post => post.title === 'smile 2', error => {
if (error) {
console.error(error)

return
}

console.log('post deleted')
})
Empty file.
1 change: 1 addition & 0 deletions staff/johnny-rojas/socialcode/api/data/posts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"author":"Freakazoid","title":"Freakazoid freakazoid!","image":"https://amblin.com/wp-content/uploads/2019/09/freakazoid_1995_photo_hero.jpg","description":"Qui a coupé le fromage","date":"2024-05-29T20:56:42.584Z","id":"8418584534790448-1717016202588"},{"author":"Flash","title":"CMIYC","image":"https://media.giphy.com/media/Tte4rCQALbiwAYXvPR/giphy.gif?cid=790b76112rg5mygn5f9ugptai4j8mmw22p8hbpu51upxtczb&ep=v1_gifs_search&rid=giphy.gif&ct=g","description":"Fast fast fast...","date":"2024-05-29T21:03:08.272Z","id":"6274552332766918-1717016588274"},{"author":"ValHallen","title":"Concert to nigth ⚡️🎸","image":"https://64.media.tumblr.com/b81ff95da2e4c645bc05ffccfaf80992/tumblr_ncn6pfY3VY1tzqospo1_500.gif","description":"Keep awake for the info🤘🏽","date":"2024-05-29T21:06:31.878Z","id":"1793392930363531-1717016791880"}]
Empty file.
1 change: 1 addition & 0 deletions staff/johnny-rojas/socialcode/api/data/users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"Barry","surname":"Allen","email":"[email protected]","username":"Flash","password":"1234"},{"name":"Lorem","surname":"Ipsum","email":"[email protected]","username":"Lorem","password":"1234"},{"name":"Dexter","surname":"Douglas","email":"[email protected]","username":"Freakazoid","password":"1234"},{"name":"Val","surname":"Hallen","email":"[email protected]","username":"ValHallen","password":"1234"}]
39 changes: 39 additions & 0 deletions staff/johnny-rojas/socialcode/api/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class ContentError extends Error {
constructor(message) {
super(message)

//this.name = ContentError.name
this.name = this.constructor.name
}
}

class MatchError extends Error {
constructor(message) {
super(message)

this.name = this.constructor.name
}
}

class DuplicityError extends Error {
constructor(message) {
super(message)

this.name = this.constructor.name
}
}

class SystemError extends Error {
constructor(message) {
super(message)

this.name = this.constructor.name
}
}

export {
ContentError,
MatchError,
DuplicityError,
SystemError
}
Loading

0 comments on commit 9e1a345

Please sign in to comment.