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.
split api data into files; b00tc4mp#136
- Loading branch information
1 parent
61b8624
commit a2872cd
Showing
40 changed files
with
2,368 additions
and
147 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
staff/eduardo-sanchez/socialcode/api/data/deletePost-Orig.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,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 |
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,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
28
staff/eduardo-sanchez/socialcode/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,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`) | ||
} | ||
}) | ||
|
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,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
15
staff/eduardo-sanchez/socialcode/api/data/findOnePost.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 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); | ||
} | ||
}) |
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,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
29
staff/eduardo-sanchez/socialcode/api/data/findPosts.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 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) | ||
// }) |
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,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
11
staff/eduardo-sanchez/socialcode/api/data/findUser.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,11 @@ | ||
import data from './index.js' | ||
|
||
data.findUser(user => user.surname === 'Grillo', (error, user) => { | ||
if (error) { | ||
console.error(error) | ||
|
||
return | ||
} | ||
|
||
console.log(user) | ||
}) |
Oops, something went wrong.