forked from b00tc4mp/isdi-parttime-202403
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creation of the API folder and modification of the data and logic fol…
…ders to work with a server b00tc4mp#150
- Loading branch information
1 parent
79e286f
commit 9e1a345
Showing
51 changed files
with
2,195 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
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 | ||
``` |
This file contains 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
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 |
This file contains 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
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.
This file contains 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
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.
This file contains 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
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"}] |
This file contains 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
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 | ||
} |
Oops, something went wrong.