diff --git a/staff/angelzrc/unsocial/api/data/posts.json b/staff/angelzrc/unsocial/api/data/posts.json index 1783e755..ed0f69e9 100644 --- a/staff/angelzrc/unsocial/api/data/posts.json +++ b/staff/angelzrc/unsocial/api/data/posts.json @@ -36,16 +36,13 @@ "author": "m2x5tcx927f", "text": "yo que se", "date": "2024-10-31T17:16:45.886Z" + }, + { + "id": "m320io97u3", + "author": "m2x5tcx927f", + "text": "esto funciona?", + "date": "2024-11-03T19:56:00.715Z" } ] - }, - { - "id": "m2xhyzvb5gi", - "image": "https://media.giphy.com/media/QLiqUx7aHg10bl5FVj/giphy.gif?cid=790b7611k9bnssjt=g", - "text": "csdsadsadas", - "author": "m2x5opwqqap", - "date": "2024-10-31T16:05:44.855Z", - "likes": [], - "comments": [] } ] \ No newline at end of file diff --git a/staff/angelzrc/unsocial/api/index.js b/staff/angelzrc/unsocial/api/index.js index 617f0394..8c69ca5c 100644 --- a/staff/angelzrc/unsocial/api/index.js +++ b/staff/angelzrc/unsocial/api/index.js @@ -50,6 +50,33 @@ server.get('/users/:userId/name', (req, res) => { } }) +server.get('/posts', (req, res) => { + const userId = req.headers.authorization.slice(6) + + try { + const posts = logic.getPosts(userId) + + res.json(posts) + } catch (error) { + res.status(400).json({ error: error.constructor.name, message: error.message }) + + console.error(error) + } +}) + +server.get('/posts/:postId/comments', (req, res) => { + const { postId } = req.params + + try { + const comments = logic.getComments(postId) + + res.json(comments) + } catch (error) { + res.status(400).json({ error: error.constructor.name, message: error.message }) + + console.error(error) + } +}) server.post('/posts', jsonBodyParser, (req, res) => { const userId = req.headers.authorization.slice(6) @@ -67,6 +94,23 @@ server.post('/posts', jsonBodyParser, (req, res) => { } }) +server.patch('/posts/:postId', (req, res) => { + const { postId } = req.params + const userId = req.headers.authorization.slice(6) + + try { + logic.toggleLikePost(postId, userId) + + res.status(200).send() + } catch (error) { + res.status(400).json({ error: error.constructor.name, message: error.message }) + + console.error(error) + } +}) + + + server.post('/posts/:postId/comments', jsonBodyParser, (req, res) => { const { postId } = req.params const userId = req.headers.authorization.slice(6) // 'Basic asdfasdfas' @@ -86,12 +130,12 @@ server.post('/posts/:postId/comments', jsonBodyParser, (req, res) => { }) -/* server.delete('posts/:postId/:commentId', jsonBodyParser, (req, res) => { +server.delete('posts/:postId/comments/:commentId', (req, res) => { const { postId, commentId } = req.params const userId = req.headers.authorization.slice(6) try { - logic.aremoveComment(postId, userId, commentId) + logic.removeComment(postId, userId, commentId) res.status(200).send() } catch (error) { @@ -100,7 +144,23 @@ server.post('/posts/:postId/comments', jsonBodyParser, (req, res) => { console.error(error) } - */ +}) + +server.delete('/posts/:postId', (req, res) => { + const userId = req.headers.authorization.slice(6) + + const { postId } = req.params + + try { + logic.deletePost(postId, userId) + + res.status(200).send() + } catch (error) { + res.status(400).json({ error: error.constructor.name, message: error.message }) + + console.error(error) + } +}) server.listen(8080, () => console.log('api is up')) diff --git a/staff/angelzrc/unsocial/api/logic/createPost.test.js b/staff/angelzrc/unsocial/api/logic/createPost.test.js new file mode 100644 index 00000000..4c78cb2a --- /dev/null +++ b/staff/angelzrc/unsocial/api/logic/createPost.test.js @@ -0,0 +1,8 @@ +import createPost from './createPost.js'; + +try { + console.log(createPost("m2x5v0tqblo", "https://letsenhance.io/static/8f5e523ee6b2479e26ecc91b9c25261e/1015f/MainAfter.jpg", "This is a new post!")) + +} catch (error) { + console.error(error); +} diff --git a/staff/angelzrc/unsocial/api/logic/deletePost.js b/staff/angelzrc/unsocial/api/logic/deletePost.js new file mode 100644 index 00000000..e5d23642 --- /dev/null +++ b/staff/angelzrc/unsocial/api/logic/deletePost.js @@ -0,0 +1,23 @@ +import { validate } from './helpers/index.js' +import { storage } from '../data/index.js' + + +export default (postId, userId) => { + validate.id(postId, 'postId') + validate.id(userId, 'userId') + + const { posts } = storage + const index = posts.findIndex(({ id }) => id === postId) + + if (index < 0) throw new Error('post not fouind') + + const post = posts[index] + + const { author } = post + + if (author !== userId) throw new Error('user is not author of post') + + posts.splice(index, 1) + + storage.posts = posts +} \ No newline at end of file diff --git a/staff/angelzrc/unsocial/api/logic/deletePost.test.js b/staff/angelzrc/unsocial/api/logic/deletePost.test.js new file mode 100644 index 00000000..803da539 --- /dev/null +++ b/staff/angelzrc/unsocial/api/logic/deletePost.test.js @@ -0,0 +1,8 @@ +import deletePost from './deletePost.js'; + +try { + console.log(deletePost("m2xhyzvb5gi", "m2x5opwqqap")) + +} catch (error) { + console.error(error) +} \ No newline at end of file diff --git a/staff/angelzrc/unsocial/api/logic/getComments.js b/staff/angelzrc/unsocial/api/logic/getComments.js new file mode 100644 index 00000000..3c07f056 --- /dev/null +++ b/staff/angelzrc/unsocial/api/logic/getComments.js @@ -0,0 +1,24 @@ +import { validate } from './helpers/index.js' +import { storage } from '../data/index.js' + +export default postId => { + validate.id(postId, 'postId') + + const { posts, users } = storage + + const post = posts.find(({ id }) => id === postId) + + if (!post) throw new Error('post not found') + + const { comments } = post + + comments.forEach(comment => { + const { author: authorId } = comment + + const { username } = users.find(({ id }) => id === authorId) + + comment.author = { id: authorId, username } + }); + + return comments +} \ No newline at end of file diff --git a/staff/angelzrc/unsocial/api/logic/getPosts.js b/staff/angelzrc/unsocial/api/logic/getPosts.js new file mode 100644 index 00000000..56819267 --- /dev/null +++ b/staff/angelzrc/unsocial/api/logic/getPosts.js @@ -0,0 +1,19 @@ +import { storage } from '../data/index.js' + +export default userId => { + const { posts, users } = storage + + posts.forEach(post => { + const { author: authorId } = post + + const { username } = users.find(({ id }) => id === authorId) + + post.author = { id: authorId, username } + + post.liked = post.likedBy.includes(userId) + + post.comments = post.comments.length + }); + + return posts.toReversed() +} \ No newline at end of file diff --git a/staff/angelzrc/unsocial/api/logic/index.js b/staff/angelzrc/unsocial/api/logic/index.js index 87413d82..7f624429 100644 --- a/staff/angelzrc/unsocial/api/logic/index.js +++ b/staff/angelzrc/unsocial/api/logic/index.js @@ -3,13 +3,23 @@ import registerUser from './registerUser.js' import getUserName from './getUserName.js' import createPost from './createPost.js' import addComment from './addComment.js' +import removeComment from './removeComment.js' +import deletePost from './deletePost.js' +import getPosts from './getPosts.js' +import getComments from './getComments.js' + + const logic = { authenticateUser, registerUser, getUserName, + getPosts, createPost, - addComment + getComments, + addComment, + removeComment, + deletePost, } export default logic \ No newline at end of file diff --git a/staff/angelzrc/unsocial/api/test/add-comment.sh b/staff/angelzrc/unsocial/api/test/add-comment.sh index 49f3fc47..a1808c44 100755 --- a/staff/angelzrc/unsocial/api/test/add-comment.sh +++ b/staff/angelzrc/unsocial/api/test/add-comment.sh @@ -1 +1 @@ - curl -H 'Authorization: Basic m2x5tcx927f' -H 'Content-Type: application/json' -d '{"text":"yo que se"}' http://localhost:8080/posts/m2x69ey2d79/comments -v \ No newline at end of file + curl -H 'Authorization: Basic m2x5tcx927f' -H 'Content-Type: application/json' -d '{"text":"esto funciona?"}' http://localhost:8080/posts/m2x69ey2d79/comments -v \ No newline at end of file diff --git a/staff/angelzrc/unsocial/api/test/delete-post.sh b/staff/angelzrc/unsocial/api/test/delete-post.sh new file mode 100644 index 00000000..56d7b80d --- /dev/null +++ b/staff/angelzrc/unsocial/api/test/delete-post.sh @@ -0,0 +1 @@ +curl -H 'Authorization: Basic m2x5v0tqblo' -X DELETE http://localhost:8080/posts/m322enfihy -v \ No newline at end of file diff --git a/staff/angelzrc/unsocial/api/test/remove-comment.js b/staff/angelzrc/unsocial/api/test/remove-comment.js new file mode 100644 index 00000000..3973e4d0 --- /dev/null +++ b/staff/angelzrc/unsocial/api/test/remove-comment.js @@ -0,0 +1,10 @@ +const xhr = new XMLHttpRequest + +xhr.addEventListener('load', () => { + console.log(xhr.status, xhr.response) +}) + +xhr.open('DELETE', 'http://localhost:8080/posts/m2x69ey2d79/comments/m320io97u3') +xhr.setRequestHeader('Authorization', 'Basic m2x5tcx927f') +xhr.setRequestHeader('Content-Type', 'application/json') +xhr.send() \ No newline at end of file diff --git a/staff/angelzrc/unsocial/api/test/remove-comment.sh b/staff/angelzrc/unsocial/api/test/remove-comment.sh new file mode 100644 index 00000000..f713ab97 --- /dev/null +++ b/staff/angelzrc/unsocial/api/test/remove-comment.sh @@ -0,0 +1 @@ +curl -H 'Authorization: Basic m2x5opwqqap' -X DELETE http://localhost:8080/posts/m2x69ey2d79/comments/m2xeoohf3v -v \ No newline at end of file