Skip to content

Commit

Permalink
added more logic from app adapted to api and logic tests b00tc4mp#173
Browse files Browse the repository at this point in the history
  • Loading branch information
angelzrc committed Nov 3, 2024
1 parent 1627579 commit ee4cb88
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 14 deletions.
15 changes: 6 additions & 9 deletions staff/angelzrc/unsocial/api/data/posts.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
]
66 changes: 63 additions & 3 deletions staff/angelzrc/unsocial/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'
Expand All @@ -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) {
Expand All @@ -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'))

8 changes: 8 additions & 0 deletions staff/angelzrc/unsocial/api/logic/createPost.test.js
Original file line number Diff line number Diff line change
@@ -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);
}
23 changes: 23 additions & 0 deletions staff/angelzrc/unsocial/api/logic/deletePost.js
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions staff/angelzrc/unsocial/api/logic/deletePost.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import deletePost from './deletePost.js';

try {
console.log(deletePost("m2xhyzvb5gi", "m2x5opwqqap"))

} catch (error) {
console.error(error)
}
24 changes: 24 additions & 0 deletions staff/angelzrc/unsocial/api/logic/getComments.js
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions staff/angelzrc/unsocial/api/logic/getPosts.js
Original file line number Diff line number Diff line change
@@ -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()
}
12 changes: 11 additions & 1 deletion staff/angelzrc/unsocial/api/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion staff/angelzrc/unsocial/api/test/add-comment.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -H 'Authorization: Basic m2x5tcx927f' -H 'Content-Type: application/json' -d '{"text":"yo que se"}' http://localhost:8080/posts/m2x69ey2d79/comments -v
curl -H 'Authorization: Basic m2x5tcx927f' -H 'Content-Type: application/json' -d '{"text":"esto funciona?"}' http://localhost:8080/posts/m2x69ey2d79/comments -v
1 change: 1 addition & 0 deletions staff/angelzrc/unsocial/api/test/delete-post.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -H 'Authorization: Basic m2x5v0tqblo' -X DELETE http://localhost:8080/posts/m322enfihy -v
10 changes: 10 additions & 0 deletions staff/angelzrc/unsocial/api/test/remove-comment.js
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions staff/angelzrc/unsocial/api/test/remove-comment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -H 'Authorization: Basic m2x5opwqqap' -X DELETE http://localhost:8080/posts/m2x69ey2d79/comments/m2xeoohf3v -v

0 comments on commit ee4cb88

Please sign in to comment.