Skip to content

Commit

Permalink
modified app class components to functions, and modified logic and ha…
Browse files Browse the repository at this point in the history
…ndles acordingly b00tc4mp#173
  • Loading branch information
angelzrc committed Nov 7, 2024
2 parents da14eaf + 1b46827 commit a573a81
Show file tree
Hide file tree
Showing 26 changed files with 1,125 additions and 3 deletions.
33 changes: 33 additions & 0 deletions staff/angelzrc/unsocial/api/data/posts.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,40 @@
"author": "m2x5tcx927f",
"text": "esto funciona?",
"date": "2024-11-03T19:56:00.715Z"
},
{
"id": "m333ii8q7m",
"author": "m2x5tcx927f",
"text": "esto funciona de verdad?",
"date": "2024-11-04T14:07:37.946Z"
}
]
},
{
"id": "m3349wfx28d",
"image": "https://imgs.search.brave.com/qJFCUICSuhAGFLNBvcENRsxrBEOBE46zgHMhWf4wDnA/rs:fit:500:0:0:0/g:ce/aHR0cHM6Ly9waXhs/ci5jb20vaW1hZ2Vz/L2luZGV4L3Byb2R1/Y3QtaW1hZ2Utb25l/LndlYnA",
"text": "Buenos dias",
"author": "m2x5opwqqap",
"date": "2024-11-04T14:28:56.061Z",
"likes": [],
"comments": []
},
{
"id": "m3349xkhtm",
"image": "https://imgs.search.brave.com/qJFCUICSuhAGFLNBvcENRsxrBEOBE46zgHMhWf4wDnA/rs:fit:500:0:0:0/g:ce/aHR0cHM6Ly9waXhs/ci5jb20vaW1hZ2Vz/L2luZGV4L3Byb2R1/Y3QtaW1hZ2Utb25l/LndlYnA",
"text": "Buenos dias",
"author": "m2x5opwqqap",
"date": "2024-11-04T14:28:57.521Z",
"likes": [],
"comments": []
},
{
"id": "m3349y1rr5e",
"image": "https://imgs.search.brave.com/qJFCUICSuhAGFLNBvcENRsxrBEOBE46zgHMhWf4wDnA/rs:fit:500:0:0:0/g:ce/aHR0cHM6Ly9waXhs/ci5jb20vaW1hZ2Vz/L2luZGV4L3Byb2R1/Y3QtaW1hZ2Utb25l/LndlYnA",
"text": "Buenos dias",
"author": "m2x5opwqqap",
"date": "2024-11-04T14:28:58.143Z",
"likes": [],
"comments": []
}
]
14 changes: 14 additions & 0 deletions staff/angelzrc/unsocial/api/data/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,19 @@
"email": "[email protected]",
"username": "Jiamei",
"password": "123123123"
},
{
"id": "m3379tj97",
"name": "hang1",
"email": "[email protected]",
"username": "hang1",
"password": "123123123"
},
{
"id": "m337kcd9x1",
"name": "peterpan",
"email": "[email protected]",
"username": "peterpan",
"password": "123123123"
}
]
46 changes: 46 additions & 0 deletions staff/angelzrc/unsocial/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const jsonBodyParser = json()

server.get('/', (_, res) => res.send('Hello, API!'))


server.post('/authenticate', jsonBodyParser, (req, res) => {
const { username, password } = req.body

Expand Down Expand Up @@ -70,7 +71,12 @@ server.post('/posts', jsonBodyParser, (req, res) => {
}
})

<<<<<<< HEAD
server.get('/posts', (req, res) => {
=======
server.patch('/posts/:postId/likes', (req, res) => {
const { postId } = req.params
>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b
const userId = req.headers.authorization.slice(6)

try {
Expand All @@ -84,6 +90,46 @@ server.get('/posts', (req, res) => {
}
})

<<<<<<< HEAD
=======


server.post('/posts/:postId/comments', jsonBodyParser, (req, res) => {
const { postId } = req.params
const userId = req.headers.authorization.slice(6) // 'Basic asdfasdfas'
const { text } = req.body

try {
logic.addComment(userId, postId, text)

res.status(201).send()
} catch (error) {

res.status(400).json({ error: error.constructor.name, message: error.message })

console.error(error)
}


})

server.delete('posts/:postId/comments/:commentId', (req, res) => {
const { postId, commentId } = req.params
const userId = req.headers.authorization.slice(6)

try {
logic.removeComment(postId, userId, commentId)

res.status(204).send()
} catch (error) {

res.status(400).json({ error: error.constructor.name, message: error.message })

console.error(error)
}
})

>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b
server.delete('/posts/:postId', (req, res) => {
const userId = req.headers.authorization.slice(6)

Expand Down
20 changes: 20 additions & 0 deletions staff/angelzrc/unsocial/api/logic/addComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,27 @@ export default (userId, postId, text) => {

if (!found) throw new Error('user not found')

<<<<<<< HEAD
const post = posts.find(({ id }) => id === postId)
=======
if (!found) {
throw new Error('user not found')
} else if (!postFound) {
throw new Error('post not found')
} else {
const post = posts.find(({ id }) => id === postId)

const comment = {
id: uuid(),
author: userId,
text,
date: new Date
}
post.comments.push(comment)

storage.posts = posts
}
>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b

if (!post) throw new Error('post not found')

Expand Down
8 changes: 8 additions & 0 deletions staff/angelzrc/unsocial/api/logic/deletePost.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export default (userId, postId) => {

if (!found) throw new Error('user not found')

<<<<<<< HEAD
=======
const { users, posts } = storage
const user = users.find(({ id }) => id === userId)

if (!user) throw new Error('user not found')

>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b
const index = posts.findIndex(({ id }) => id === postId)

if (index < 0) throw new Error('post not found')
Expand Down
4 changes: 4 additions & 0 deletions staff/angelzrc/unsocial/api/logic/getComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export default (userId, postId) => {

if (!found) throw new Error('user not found')

const found = users.some(({ id }) => id === userId)

if (!found) throw new Error('user not found')

const post = posts.find(({ id }) => id === postId)

if (!post) throw new Error('post not found')
Expand Down
12 changes: 12 additions & 0 deletions staff/angelzrc/unsocial/api/logic/getPosts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { storage } from '../data/index.js'
<<<<<<< HEAD
import { validate } from 'com'
=======
import { validate } from './helpers/index.js'
>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b
export default userId => {
validate.id(userId, 'userId')
Expand All @@ -21,6 +25,14 @@ export default userId => {

post.comments = post.comments.length
})
<<<<<<< HEAD
=======

/* const reversedPosts = posts.slice().reverse();
return reversedPosts */
/* console.log(Array.isArray(posts))
console.log(posts) */
>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b

return posts.toReversed()
}
8 changes: 8 additions & 0 deletions staff/angelzrc/unsocial/api/logic/getPosts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import getPosts from './getPosts.js'

try {
console.log(getPosts('m2x5opwqqap'))

} catch (error) {
console.error(error)
}
7 changes: 7 additions & 0 deletions staff/angelzrc/unsocial/api/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import toggleLikePost from './toggleLikePost.js'
import addComment from './addComment.js'
import removeComment from './removeComment.js'
import getComments from './getComments.js'
import toggleLikePost from './toggleLikePost.js'


const logic = {
authenticateUser,
Expand All @@ -21,7 +23,12 @@ const logic = {

addComment,
removeComment,
<<<<<<< HEAD
getComments
=======
deletePost,
toggleLikePost
>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b
}

export default logic
4 changes: 4 additions & 0 deletions staff/angelzrc/unsocial/api/logic/removeComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export default (userId, postId, commentId) => {
const found = users.some(({ id }) => id === userId)

if (!found) throw new Error('user not found')
<<<<<<< HEAD
=======

>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b

const post = posts.find(({ id }) => id === postId)

Expand Down
16 changes: 16 additions & 0 deletions staff/angelzrc/unsocial/api/logic/toggleLikePost.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
<<<<<<< HEAD
import { validate } from 'com'
import { storage } from '../data/index.js'
=======
import { storage } from "../data/index.js"
>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b

export default (userId, postId) => {
validate.id(userId, 'userId')
validate.id(postId, 'postId')

const { users, posts } = storage
<<<<<<< HEAD

const found = users.some(({ id }) => id === userId)
=======
const found = users.som(({ id }) => id === userId)
>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b

if (!found) throw new Error('user not found')

const post = posts.find(({ id }) => id === postId)

<<<<<<< HEAD
if (!post) throw new Error('post not found')
=======
if (!post) throw new Error('user not found')
>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b

const { likes } = post

Expand All @@ -25,4 +37,8 @@ export default (userId, postId) => {
likes.splice(index, 1)

storage.posts = posts
<<<<<<< HEAD
=======

>>>>>>> 1b468274c84eb6f3853c660b2b2683f639a5aa7b
}
Loading

0 comments on commit a573a81

Please sign in to comment.