forked from b00tc4mp/isdi-bootcamp-202405
-
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.
add new exercise about validation on data logic b00tc4mp#79
- Loading branch information
Fabián Romero
authored and
Fabián Romero
committed
Jul 15, 2024
1 parent
3c19b66
commit 12d25b4
Showing
101 changed files
with
1,021 additions
and
109 deletions.
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,21 @@ | ||
import fs from 'fs' // importo esto para saber la forma en que se tienen que leer los codigos | ||
|
||
function deletePost(condition) { | ||
let json = fs.readFileSync('./data/posts.json', 'utf8') // ya no uso localstorage, uso disco y esta es la manera apra poder que se lea la info que tengo almacenada en el disco | ||
|
||
const posts = json ? JSON.parse(json) : [] //si en el json hay algo, lo parsee, y si no.. que me devuelva un array vacio | ||
|
||
const postIndex = posts.findIndex(condition) | ||
|
||
if (postIndex > -1) { | ||
posts.splice(postIndex, 1) | ||
|
||
json = JSON.stringify(posts) // local no más ahora json | ||
fs.writeFileSync('./data/posts.json', json) | ||
} | ||
} | ||
|
||
export default deletePost | ||
// esta carpeta datos de api es solo para manejar datos no sesiones | ||
|
||
// cambiar a los validation |
12 changes: 12 additions & 0 deletions
12
staff/fabian-romero/react/ponies/api/data/deletePost.test.js
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,12 @@ | ||
import deletePost from "./deletePost.js" | ||
|
||
|
||
const post1 = { | ||
author: "Valito", | ||
caption: "ÑamÑam!", | ||
date: "2024-07-11T09:41:57.634Z", | ||
id: "gud3txmwlqo", | ||
image: "https://media.tenor.com/u3QduFHVtJ8AAAAM/tacos-dog.gif" | ||
} | ||
|
||
deletePost(post => post.id === post1) // aqui puedo poner cualquer dato.. la logica es que me lo va a eliminar |
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,65 @@ | ||
import fs from 'fs' | ||
|
||
const users = [] // aqui es paras cargar la info en tu local | ||
|
||
const Fabian = { | ||
name: 'Fabian', | ||
surname: 'Romero', | ||
email: '[email protected]', | ||
username: 'Fabito', | ||
password: 'fabi1234' | ||
} | ||
|
||
const Valentin = { | ||
name: 'Valentin', | ||
surname: 'Romero', | ||
email: '[email protected]', | ||
username: 'Valito', | ||
password: 'vali1234' | ||
} | ||
|
||
users.push(Fabian) // aqui los pusheo' para añadirlos a las arrys vacias en caso de pedirlos en alguna funcion | ||
users.push(Valentin) | ||
|
||
|
||
const usersJSON = JSON.stringify(users) | ||
|
||
console.log('usersJSON', usersJSON) | ||
|
||
fs.writeFileSync('./data/users.json', usersJSON) | ||
|
||
const usersJSON2 = fs.readFileSync('./data/users.json', 'utf-8') | ||
|
||
console.log('usersJSON2', usersJSON2) | ||
|
||
const posts = [] | ||
|
||
const post1 = { | ||
|
||
author: "Fabito", | ||
caption: "TOT EL DIA!!", | ||
date: "2024-07-10T12:42:54.986Z", | ||
id: "gho3apb3njs", | ||
image: "https://media.tenor.com/LLTYGBtru5kAAAAM/mila-stauffer-whatever.gif" | ||
} | ||
|
||
const post2 = { | ||
author: "Valito", | ||
caption: "ÑamÑam!", | ||
date: "2024-07-11T09:41:57.634Z", | ||
id: "gud3txmwlqo", | ||
image: "https://media.tenor.com/u3QduFHVtJ8AAAAM/tacos-dog.gif" | ||
} | ||
|
||
posts.push(post1) | ||
posts.push(post2) | ||
|
||
const postsJSON = JSON.stringify(posts) | ||
|
||
console.log('postsJSON', postsJSON) | ||
|
||
fs.writeFileSync('./data/posts.json', postsJSON) | ||
|
||
const postsJSON2 = fs.readFileSync('./data/users.json', 'utf-8') | ||
|
||
console.log('postsJSON2', postsJSON2) |
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,13 @@ | ||
import fs from 'fs' | ||
|
||
function findPost(condition) { | ||
let json = fs.readFileSync('./data/posts.json', 'utf8') | ||
|
||
const posts = json ? JSON.parse(json) : [] | ||
|
||
const post = posts.find(condition) | ||
|
||
return post || null | ||
} | ||
|
||
export default findPost // este se queda tal cual porque no tiene stringyfy |
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,3 @@ | ||
import findPost from './findPost.js' | ||
|
||
console.log(findPost(post => post.author === "Valito")) |
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,14 @@ | ||
import fs from 'fs' | ||
|
||
function findPosts(condition) { | ||
|
||
let json = fs.readFileSync('./data/posts.json', 'utf8') | ||
|
||
const posts = json ? JSON.parse(json) : [] | ||
|
||
const foundPosts = posts.filter(condition) | ||
|
||
return foundPosts | ||
} | ||
|
||
export default findPosts |
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,3 @@ | ||
import findPosts from './findPosts.js' | ||
|
||
console.log(findPosts(post => post.author === 'Valito')) |
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,13 @@ | ||
import fs from 'fs' | ||
|
||
function findUser(condition) { | ||
let json = fs.readFileSync('./data/users.json', 'utf8') | ||
|
||
const users = json ? JSON.parse(json) : [] | ||
|
||
const user = users.find(condition) | ||
|
||
return user || null | ||
} | ||
|
||
export default findUser |
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,4 @@ | ||
import findUser from './findUser.js' | ||
|
||
console.log(findUser(user => user.surname === "Romero")) | ||
console.log(findUser(user => user.username === "Valito")) |
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,21 @@ | ||
import deletePost from './deletePost' | ||
import findPost from './findPost' | ||
import findPosts from './findPosts' | ||
import findUser from './findUser' | ||
import insertPost from './insertPost' | ||
import insertUser from './insertUser' | ||
import updatePost from './updatePost' | ||
import updateUser from './updateUser' | ||
|
||
const data = { | ||
deletePost, | ||
findPost, | ||
findPosts, | ||
findUser, | ||
insertPost, | ||
insertUser, | ||
updatePost, | ||
updateUser | ||
} | ||
|
||
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,15 @@ | ||
import fs from 'fs' | ||
|
||
function insertPost(post) { | ||
let json = fs.readFileSync('./data/posts.json', 'utf8') | ||
|
||
const posts = json ? JSON.parse(json) : [] | ||
|
||
posts.push(post) | ||
|
||
json = JSON.stringify(posts) | ||
|
||
fs.writeFileSync('./data/posts.json', json) | ||
} | ||
|
||
export default insertPost |
29 changes: 29 additions & 0 deletions
29
staff/fabian-romero/react/ponies/api/data/insertPost.test.js
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,29 @@ | ||
import insertPost from './insertPost.js' | ||
|
||
const post = { | ||
author: "Valito", | ||
caption: "ÑamÑam!", | ||
date: "2024-07-11T09:41:57.634Z", | ||
id: "gud3txmwlqo", | ||
image: "https://media.tenor.com/u3QduFHVtJ8AAAAM/tacos-dog.gif" | ||
} | ||
|
||
const post2 = { | ||
author: "Valito", | ||
caption: "ÑamÑam!", | ||
date: "2024-07-11T09:41:57.634Z", | ||
id: "gud3txmwlqo", | ||
image: "https://media.tenor.com/u3QduFHVtJ8AAAAM/tacos-dog.gif" | ||
} | ||
|
||
const post3 = { | ||
author: "Valito", | ||
caption: "ÑamÑam!", | ||
date: "2024-07-11T09:41:57.634Z", | ||
id: "gud3txmwlqo", | ||
image: "https://media.tenor.com/u3QduFHVtJ8AAAAM/tacos-dog.gif" | ||
} | ||
|
||
insertPost(post) | ||
insertPost(post2) | ||
insertPost(post3) |
15 changes: 15 additions & 0 deletions
15
staff/fabian-romero/react/ponies/api/data/insertUser.Test.js
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,15 @@ | ||
import insertUser from './insertUser.js' | ||
|
||
const user = { | ||
name: 'Fabian', | ||
surname: 'Romero', | ||
email: '[email protected]', | ||
username: 'Fabito', | ||
password: 'fabi1234' | ||
} | ||
|
||
insertUser(user) | ||
|
||
// para probar los test tengo que abrir la consola y escribir | ||
|
||
// node data/demo.js |
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,15 @@ | ||
import fs from 'fs' | ||
|
||
function insertUser(user) { | ||
let json = fs.readFileSync('./data/users.json', 'utf8') | ||
|
||
const users = json ? JSON.parse(json) : [] | ||
|
||
users.push(user) | ||
|
||
json = JSON.stringify(users) | ||
|
||
fs.writeFileSync('./data/users.json', json) | ||
} | ||
|
||
export default insertUser |
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,37 @@ | ||
[ | ||
{ | ||
"author": "Valito", | ||
"caption": "ÑamÑam!", | ||
"date": "2024-07-11T09:41:57.634Z", | ||
"id": "gud3txmwlqo", | ||
"image": "https://media.tenor.com/u3QduFHVtJ8AAAAM/tacos-dog.gif" | ||
}, | ||
{ | ||
"author": "Valito", | ||
"caption": "ÑamÑam!", | ||
"date": "2024-07-11T09:41:57.634Z", | ||
"id": "gud3txmwlqo", | ||
"image": "https://media.tenor.com/u3QduFHVtJ8AAAAM/tacos-dog.gif" | ||
}, | ||
{ | ||
"author": "Valito", | ||
"caption": "ÑamÑam!", | ||
"date": "2024-07-11T09:41:57.634Z", | ||
"id": "gud3txmwlqo", | ||
"image": "https://media.tenor.com/u3QduFHVtJ8AAAAM/tacos-dog.gif" | ||
}, | ||
{ | ||
"author": "Valito", | ||
"caption": "ÑamÑam!", | ||
"date": "2024-07-11T09:41:57.634Z", | ||
"id": "gud3txmwlqo", | ||
"image": "https://media.tenor.com/u3QduFHVtJ8AAAAM/tacos-dog.gif" | ||
}, | ||
{ | ||
"author": "Valito", | ||
"caption": "ÑamÑam!", | ||
"date": "2024-07-11T09:41:57.634Z", | ||
"id": "gud3txmwlqo", | ||
"image": "https://media.tenor.com/u3QduFHVtJ8AAAAM/tacos-dog.gif" | ||
} | ||
] |
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,25 @@ | ||
import fs from 'fs' | ||
|
||
import validate from '../validate.js' | ||
|
||
function updatePost(condition, post) { | ||
validate.callback(condition, 'condition') | ||
vaklidate.object(post, 'post') | ||
|
||
|
||
let json = fs.readFileSync('./data/posts.json', 'utf8') | ||
|
||
const posts = json ? JSON.parse(json) : [] | ||
|
||
const index = posts.findIndex(condition) | ||
|
||
if (index > -1) { | ||
posts.splice(index, 1, post) | ||
|
||
json = JSON.stringify(posts) | ||
|
||
fs.writeFileSync('./data/posts.json', json) | ||
|
||
} | ||
} | ||
export default updatePost |
13 changes: 13 additions & 0 deletions
13
staff/fabian-romero/react/ponies/api/data/updatePost.test.js
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,13 @@ | ||
import updatePost from './updatePost.js' | ||
|
||
const post = { | ||
|
||
author: "Valito", | ||
caption: "tiki tiki", | ||
date: "2024-07-10T12:47:25.218Z", | ||
id: "mi8drcuyseo", | ||
image: "https://media.tenor.com/lhUSFl0CnpEAAAAM/frenchie-french.gif" | ||
|
||
} | ||
|
||
updatePost(post => post.id === "mi8drcuyseo", post) |
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,24 @@ | ||
import fs from 'fs' | ||
|
||
import validate from '../validate.js' | ||
|
||
function updateUser(condition, user) { | ||
validate.callback(condition, 'condition') | ||
validate.object(user, 'user') | ||
|
||
let json = fs.readFileSync('./data/user.json', 'uft8') | ||
|
||
const user = json ? JSON.parse(json) : [] | ||
|
||
const index = users.findIndex(condition) | ||
|
||
if (index > -1) { | ||
users.splice(index, 1, user) | ||
|
||
json = JSON.stringify(users) | ||
|
||
fs.writeFileSync('./data/users.json', json) | ||
} | ||
} | ||
|
||
export default updateUser |
13 changes: 13 additions & 0 deletions
13
staff/fabian-romero/react/ponies/api/data/updateUser.test.js
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,13 @@ | ||
import updateUser from './updateUser.js' | ||
|
||
const Valito = { | ||
|
||
"name": "Valentin", | ||
"surname": "Romero", | ||
"email": "[email protected]", | ||
"username": "Valito", | ||
"password": "vali1234" | ||
|
||
} | ||
|
||
updateUser(user => user.username === 'Valentin', Valito) |
Oops, something went wrong.