Skip to content

Commit

Permalink
split api data into files; b00tc4mp#136
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardolans committed Jun 24, 2024
1 parent 61b8624 commit a2872cd
Show file tree
Hide file tree
Showing 40 changed files with 2,368 additions and 147 deletions.
36 changes: 36 additions & 0 deletions staff/eduardo-sanchez/socialcode/api/data/deletePost-Orig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from 'fs'
import { SystemError } from 'com/errors.js'

const 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 deletePost
36 changes: 36 additions & 0 deletions staff/eduardo-sanchez/socialcode/api/data/deletePost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from 'fs';
import { SystemError } from 'com/errors.js'; // Asegúrate de que la ruta sea correcta

const 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) {
const deletedPost = posts.splice(index, 1)[0]; // Elimina y guarda el post eliminado

const newJson = JSON.stringify(posts, null, 2); // Indentación de 2 espacios para legibilidad

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

callback(null, deletedPost); // Llama al callback con el post eliminado
});
} else {
callback(null, null); // Llama al callback con null si no se encontró ningún post para eliminar
}
});
}

export default deletePost;
28 changes: 28 additions & 0 deletions staff/eduardo-sanchez/socialcode/api/data/deletePost.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// import data from './index.js'

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

// return
// }

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



import data from "./index.js"

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

if (!deletedPost) {
console.error("Post not found")
} else {
console.log(`Post with title: ${deletedPost.title} deleted`)
}
})

22 changes: 22 additions & 0 deletions staff/eduardo-sanchez/socialcode/api/data/findOnePost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fs from 'fs'
import { SystemError } from 'com/errors.js'

const findOnePost = (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 foundPost = posts.find(condition)

callback(null, foundPost)
})
}

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

data.findOnePost(post => post.date.includes('T15'), (error, post) => {
if (error) {
console.error(error)

return
}

if (!post) {
console.error("Post not found");
} else {
console.log(post);
}
})
22 changes: 22 additions & 0 deletions staff/eduardo-sanchez/socialcode/api/data/findPosts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fs from 'fs'
import { SystemError } from 'com/errors.js'

const 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)
})
}

export default findPosts
29 changes: 29 additions & 0 deletions staff/eduardo-sanchez/socialcode/api/data/findPosts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import data from "./index.js"

data.findPosts(post => post.date.includes('T15'), (error, postsFound) => {
if (error) {
console.error(error)

return
}

if (!postsFound || postsFound.length === 0) {
console.error("Posts or Post not found")
} else {
console.log('Posts or Post with search details found')
}

console.log(postsFound)
})

//data.findPosts(post => post.title && post.title.includes("L"), (error, posts) => {

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

// return
// }

// console.log(posts)
// })
22 changes: 22 additions & 0 deletions staff/eduardo-sanchez/socialcode/api/data/findUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fs from 'fs'
import { SystemError } from 'com/errors.js'

const 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)
})
}

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

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

return
}

console.log(user)
})
Loading

0 comments on commit a2872cd

Please sign in to comment.