Skip to content

Commit

Permalink
use user-id instead of username in ponies b00tc4mp#68
Browse files Browse the repository at this point in the history
  • Loading branch information
NerinaHctz committed Aug 4, 2024
1 parent 4684858 commit c01f64e
Show file tree
Hide file tree
Showing 82 changed files with 671 additions and 1,034 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default (req, res, next) => {

try {
logic.authenticateUser(username, password)
.then(() =>
jwt.sign({ sub: username }, process.env.JWT_SECRET, (error, token) => {
.then(userId =>
jwt.sign({ sub: userId }, process.env.JWT_SECRET, (error, token) => {
if (error) {
next(new SessionError(error.message))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { username } = req
const { userId } = req

const { image, caption } = req.body

try {
logic.createPost(username, image, caption)
logic.createPost(userId, image, caption)
.then(() => res.status(201).send())
.catch(error => next(error))
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { username } = req
const { userId } = req

const { postId } = req.params

try {
logic.deletePost(username, postId)
logic.deletePost(userId, postId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { username } = req
const { userId } = req

try {
logic.getAllFavPosts(username)
logic.getAllFavPosts(userId)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { username } = req

try {
logic.getAllFollowingUserPosts(username)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
next(error)
}
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { userId } = req

try {
logic.getAllFollowingUserPosts(userId)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
next(error)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { logic } from '../../cor/index.js'

export default (req, res) => {
const { username } = req
export default (req, res, next) => {
const { userId } = req

try {
logic.getAllPosts(username)
logic.getAllPosts(userId)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
nexct(error)
next(error)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { username } = req
const { userId } = req

const { targetUsername } = req.params
const { targetUserId } = req.params

try {
logic.getUserName(username, targetUsername)
logic.getUserName(userId, targetUserId)
.then(name => res.json(name))
.catch(error => next(error))
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import registerUserHandler from '../handlers/registerUserHandler.js'
import authenticateUserHandler from '../handlers/authenticateUserHandler.js'
import getUserNameHandler from '../handlers/getUserNameHandler.js'
import getAllPostsHandler from '../handlers/getAllPostsHandler.js'
import getAllFollowingUserPostsHandler from '../handlers/getAllFollowingUserPosts.js'
import getAllFollowingUserPostsHandler from '../handlers/getAllFollowingUserPostsHandler.js'
import getAllFavPostsHandler from '../handlers/getAllFavPostsHandler.js'
import createPostHandler from '../handlers/createPostHandler.js'
import deletePostHandler from '../handlers/deletePostHandler.js'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { username } = req
const { userId } = req

const { postId } = req.params

try {
logic.toggleFavPost(username, postId)
logic.toggleFavPost(userId, postId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { username } = req
const { userId } = req

const { targetUsername } = req.params
const { targetUserId } = req.params

try {
logic.toggleFollowUser(username, targetUsername)
logic.toggleFollowUser(userId, targetUserId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { username } = req
const { userId } = req

const { postId } = req.params

try {
logic.toggleLikePost(username, postId)
logic.toggleLikePost(userId, postId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { logic } from '../../cor/index.js'

export default (req, res, next) => {
const { username } = req
const { userId } = req

const { postId } = req.params

const { caption } = req.body

try {
logic.updatePostCaption(username, postId, caption)
logic.updatePostCaption(userId, postId, caption)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions staff/nerina-castillo/ponies/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mongoose.connect(process.env.MONGODB_URI)

api.post('/users/auth', jsonBodyParser, authenticateUserHandler)

api.get('/users/:targetUsername/name', jwtVerifier, getUserNameHandler)
api.get('/users/:targetUserId/name', jwtVerifier, getUserNameHandler)

api.get('/posts', jwtVerifier, getAllPostsHandler)

Expand All @@ -51,7 +51,7 @@ mongoose.connect(process.env.MONGODB_URI)

api.patch('/posts/:postId/favs', jwtVerifier, toggleFavPostHandler)

api.patch('/users/:targetUsername/follows', jwtVerifier, toggleFollowUserHandler)
api.patch('/users/:targetUserId/follows', jwtVerifier, toggleFollowUserHandler)

api.patch('/posts/:postId/caption', jwtVerifier, jsonBodyParser, updatePostCaptionHandler)

Expand Down
4 changes: 2 additions & 2 deletions staff/nerina-castillo/ponies/api/middlewares/jwtVerifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export default (req, res, next) => {
return
}

const { sub: username } = payload
const { sub: userId } = payload

req.username = username
req.userId = userId

next()
})
Expand Down
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/test/create-post.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -v -X POST http://localhost:8080/posts -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I" -d '{"image": "https://media.giphy.com/media/Ty9Sg8oHghPWg/giphy.gif?cid=790b7611k1isspkgnlxqcvk07kqq26fe137qherkek4mavvf&ep=v1_gifs_trending&rid=giphy.gif&ct=g", "caption": "This is a caption"}' -H "Content-Type: application/json"
curl -v -X POST http://localhost:8080/posts -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFjZDIyOWZiNGJiYzZjYjY1ODFiNTIiLCJpYXQiOjE3MjI2MDIwNTJ9.VkAHNtRaPniHxh4laXR50kGVxRlzzU3SylcgQerj8iQ" -d '{"image": "https://media.giphy.com/media/Ty9Sg8oHghPWg/giphy.gif?cid=790b7611k1isspkgnlxqcvk07kqq26fe137qherkek4mavvf&ep=v1_gifs_trending&rid=giphy.gif&ct=g", "caption": "This is a caption"}' -H "Content-Type: application/json"
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/test/delete-post.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -v -X DELETE http://localhost:8080/posts/3bi1ryn98p00 -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I"
curl -v -X DELETE http://localhost:8080/posts/66acdecc4b37b603b718ebb6 -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFjZDIyOWZiNGJiYzZjYjY1ODFiNTIiLCJpYXQiOjE3MjI2MDIwNTJ9.VkAHNtRaPniHxh4laXR50kGVxRlzzU3SylcgQerj8iQ"
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/test/get-favPosts.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -v http://localhost:8080/posts/favs -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I"
curl -v http://localhost:8080/posts/favs -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFjZDIyOWZiNGJiYzZjYjY1ODFiNTIiLCJpYXQiOjE3MjI2MDIwNTJ9.VkAHNtRaPniHxh4laXR50kGVxRlzzU3SylcgQerj8iQ"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -v http://localhost:8080/posts/following -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I"
curl -v http://localhost:8080/posts/following -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFjZDIyOWZiNGJiYzZjYjY1ODFiNTIiLCJpYXQiOjE3MjI2MDIwNTJ9.VkAHNtRaPniHxh4laXR50kGVxRlzzU3SylcgQerj8iQ"
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/test/get-posts.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -v http://localhost:8080/posts -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I"
curl -v http://localhost:8080/posts -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFjZDIyOWZiNGJiYzZjYjY1ODFiNTIiLCJpYXQiOjE3MjI2MDIwNTJ9.VkAHNtRaPniHxh4laXR50kGVxRlzzU3SylcgQerj8iQ"
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/test/get-user.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -v http://localhost:8080/users/janfryTopera/name -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I"
curl -v http://localhost:8080/users/66acc0c8863c3606c60ba912/name -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFjZDIyOWZiNGJiYzZjYjY1ODFiNTIiLCJpYXQiOjE3MjI2MDIwNTJ9.VkAHNtRaPniHxh4laXR50kGVxRlzzU3SylcgQerj8iQ"
2 changes: 2 additions & 0 deletions staff/nerina-castillo/ponies/api/test/register-user.sh
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
curl -v http://localhost:8080/users -X POST -d '{"name":"janfry","surname":"topera","email":"[email protected]","username":"janfryTopera","password":"janfry123","passwordRepeat":"janfry123"}' -H "Content-Type: application/json"

# eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFjZDIyOWZiNGJiYzZjYjY1ODFiNTIiLCJpYXQiOjE3MjI2MDIwNTJ9.VkAHNtRaPniHxh4laXR50kGVxRlzzU3SylcgQerj8iQ
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/test/toggleFav-post.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -v http://localhost:8080/posts/66ab7e7385156518ecdcd558/favs -X PATCH -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I"
curl -v http://localhost:8080/posts/66acdf2c4b37b603b718ebb9/favs -X PATCH -H "Authorization: Bearer Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFjZDIyOWZiNGJiYzZjYjY1ODFiNTIiLCJpYXQiOjE3MjI2MDIwNTJ9.VkAHNtRaPniHxh4laXR50kGVxRlzzU3SylcgQerj8iQ"
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/test/toggleFollow-user.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -v http://localhost:8080/users/janfryTopera/follows -X PATCH -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I"
curl -v http://localhost:8080/users/66adf6279f45573c51492c75/follows -X PATCH -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFkZjYyNzlmNDU1NzNjNTE0OTJjNzUiLCJpYXQiOjE3MjI2NzY5MDh9.WuRv_S4DJVszuQtqdijMaN4kj0L7fg_p6Gpp0_DrFwU"
2 changes: 1 addition & 1 deletion staff/nerina-castillo/ponies/api/test/toggleLike-post.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -v -X PATCH http://localhost:8080/posts/3fswobxum974/likes -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I"
curl -v -X PATCH http://localhost:8080/posts/66acd344fb4bbc6cb6581b57/likes -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFjZDIyOWZiNGJiYzZjYjY1ODFiNTIiLCJpYXQiOjE3MjI2MDIwNTJ9.VkAHNtRaPniHxh4laXR50kGVxRlzzU3SylcgQerj8iQ"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -v http://localhost:8080/posts/lfhkbfbgers/caption -X PATCH -d '{"caption":"ta bien"}' -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYW5mcnlUb3BlcmEiLCJpYXQiOjE3MjIzMzAzNzl9.psSpv8cj_SrZSNGn2alTkkJ8-Aq7FTTkCGDsl9VEI-I" -H "Content-Type: application/json"
curl -v http://localhost:8080/posts/66acd344fb4bbc6cb6581b57/caption -X PATCH -d '{"caption":"ta bien"}' -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmFjZDIyOWZiNGJiYzZjYjY1ODFiNTIiLCJpYXQiOjE3MjI2MDIwNTJ9.VkAHNtRaPniHxh4laXR50kGVxRlzzU3SylcgQerj8iQ" -H "Content-Type: application/json"
51 changes: 26 additions & 25 deletions staff/nerina-castillo/ponies/app/logic/createPost.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { validate, errors } from 'com'

export default (image, caption, callback) => {
const { SystemError } = errors

export default (image, caption) => {
validate.url(image, 'image')
validate.string(caption, 'caption')
validate.callback(callback)

const xhr = new XMLHttpRequest

xhr.onload = () => {
if (xhr.status === 201) {
callback(null)

return
}

const { error, message } = JSON.parse(xhr.response)

const constructor = errors[error]

callback(new constructor(message))
}

xhr.onerror = () => callback(new Error('network error'))

xhr.open('POST', `${import.meta.env.VITE_API_URL}/posts`)
xhr.setRequestHeader('Authorization', `Bearer ${sessionStorage.token}`)
xhr.setRequestHeader('Content-Type', 'application/json')

xhr.send(JSON.stringify({ image, caption }))
return fetch(`${import.meta.env.VITE_API_URL}/posts`, {
method: 'POST',
headers: {
Authorization: `Bearer ${sessionStorage.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ image, caption })
})
.catch(error => { throw new SystemError(error.message) })
.then(response => {
const { status } = response

if (status === 201) return

return response.json()
.then(body => {
const { error, message } = body

const constructor = errors[error]

throw new constructor(message)
})
})
}
38 changes: 19 additions & 19 deletions staff/nerina-castillo/ponies/app/logic/deletePost.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { validate, errors } from "com"

export default (postId, callback) => {
validate.string(postId, 'postId')
validate.callback(callback)

const xhr = new XMLHttpRequest
const { SystemError } = errors

xhr.onload = () => {
if (xhr.status === 204) {
callback(null)
export default postId => {
validate.string(postId, 'postId')

return
return fetch(`${import.meta.env.VITE_API_URL}/posts/${postId}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${sessionStorage.token}`
}
})
.catch(error => { throw new SystemError(error.message) })
.then(response => {
const { status } = response

const { error, message } = JSON.parse(xhr.response)

const constructor = errors[error]

callback(new constructor(message))
}
if (status === 204) return

xhr.onerror = () => callback(new Error('network error'))
return response.json()
.then(body => {
const { error, message } = body

xhr.open('DELETE', `${import.meta.env.VITE_API_URL}/posts/${postId}`)
xhr.setRequestHeader('Authorization', `Bearer ${sessionStorage.token}`)
const constructor = errors[error]

xhr.send()
throw new constructor(message)
})
})
}
Loading

0 comments on commit c01f64e

Please sign in to comment.