Skip to content

Commit

Permalink
add new exercise about validation on data logic b00tc4mp#79
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabián Romero authored and Fabián Romero committed Jul 15, 2024
1 parent 3c19b66 commit 12d25b4
Show file tree
Hide file tree
Showing 101 changed files with 1,021 additions and 109 deletions.
21 changes: 21 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/deletePost.js
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 staff/fabian-romero/react/ponies/api/data/deletePost.test.js
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
65 changes: 65 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/demo.js
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)
13 changes: 13 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/findPost.js
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
3 changes: 3 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/findPost.test.js
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"))
14 changes: 14 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/findPosts.js
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
3 changes: 3 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/findPosts.test.js
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'))
13 changes: 13 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/findUser.js
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
4 changes: 4 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/findUser.test.js
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"))
21 changes: 21 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/index.js
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
15 changes: 15 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/insertPost.js
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 staff/fabian-romero/react/ponies/api/data/insertPost.test.js
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 staff/fabian-romero/react/ponies/api/data/insertUser.Test.js
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
15 changes: 15 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/insertUser.js
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
37 changes: 37 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/posts.json
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"
}
]
25 changes: 25 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/updatePost.js
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 staff/fabian-romero/react/ponies/api/data/updatePost.test.js
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)
24 changes: 24 additions & 0 deletions staff/fabian-romero/react/ponies/api/data/updateUser.js
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 staff/fabian-romero/react/ponies/api/data/updateUser.test.js
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)
Loading

0 comments on commit 12d25b4

Please sign in to comment.