Skip to content

Commit

Permalink
api refactoring index into handlers files and middleware files b00tc4…
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasOrtsBaguena committed Jul 31, 2024
1 parent 02a3084 commit babd2ca
Show file tree
Hide file tree
Showing 114 changed files with 5,527 additions and 1,499 deletions.
32 changes: 32 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/authenticateUserHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import jwt from 'jsonwebtoken'

import { logic } from '../../cor/index.js'
import { errors } from '../../com/index.js'

const { SessionError } = errors

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

try {
logic.authenticateUser(username, password, error => {
if (error) {
next(error)

return
}

jwt.sign({ sub: username }, process.env.JWT_SECRET, (error, token) => {
if (error) {
next(new SessionError(error.message))

return
}

res.json(token)
})
})
} catch (error) {
next(error)
}
}
21 changes: 21 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/createPostHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { logic } from '../../cor/index.js'

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

const { image, caption } = req.body

try {
logic.createPost(username, image, caption, error => {
if (error) {
next(error)

return
}

res.status(201).send()
})
} catch (error) {
next(error)
}
}
21 changes: 21 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/deletePostHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { logic } from '../../cor/index.js'

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

const { postId } = req.params

try {
logic.deletePost(username, postId, error => {
if (error) {
next(error)

return
}

res.status(204).send()
})
} catch (error) {
next(error)
}
}
19 changes: 19 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/getAllFavPostsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { logic } from '../../cor/index.js'

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

try {
logic.getAllFavPosts(username, (error, posts) => {
if (error) {
next(error)

return
}

res.json(posts)
})
} catch (error) {
next(error)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { logic } from '../../cor/index.js'

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

try {
logic.getAllFollowingUserPosts(username, (error, posts) => {
if (error) {
next(error)

return
}

res.json(posts)
})
} catch (error) {
next(error)
}
}
19 changes: 19 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/getAllPostsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { logic } from '../../cor/index.js'

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

try {
logic.getAllPosts(username, (error, posts) => {
if (error) {
next(error)

return
}

res.json(posts)
})
} catch (error) {
next(error)
}
}
21 changes: 21 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/getUserNameHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { logic } from '../../cor/index.js'

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

const { targetUsername } = req.params

try {
logic.getUserName(username, targetUsername, (error, name) => {
if (error) {
next(error)

return
}

res.json(name)
})
} catch (error) {
next(error)
}
}
27 changes: 27 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import registerUserHandler from './registerUserHandler.js'
import authenticateUserHandler from './authenticateUserHandler.js'
import getUserNameHandler from './getUserNameHandler.js'
import getAllPostsHandler from './getAllPostsHandler.js'
import getAllFollowingUserPostsHandler from './getAllFollowingUserPostsHandler.js'
import getAllFavPostsHandler from './getAllFavPostsHandler.js'
import createPostHandler from './createPostHandler.js'
import deletePostHandler from './deletePostHandler.js'
import toggleLikePostHandler from './toggleLikePostHandler.js'
import toggleFavPostHandler from './toggleFavPostHandler.js'
import toggleFollowUserHandler from './toggleFollowUserHandler.js'
import updatePostCaptionHandler from './updatePostCaptionHandler.js'

export {
registerUserHandler,
authenticateUserHandler,
getUserNameHandler,
getAllPostsHandler,
getAllFollowingUserPostsHandler,
getAllFavPostsHandler,
createPostHandler,
deletePostHandler,
toggleLikePostHandler,
toggleFavPostHandler,
toggleFollowUserHandler,
updatePostCaptionHandler
}
19 changes: 19 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/registerUserHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { logic } from 'cor'

export default (req, res, next) => {
const { name, surname, email, username, password, passwordRepeat } = req.body

try {
logic.registerUser(name, surname, email, username, password, passwordRepeat, error => {
if (error) {
next(error)

return
}

res.status(201).send()
})
} catch (error) {
next(error)
}
}
21 changes: 21 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/toggleFavPostHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { logic } from '../../cor/index.js'

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

const { postId } = req.params

try {
logic.toggleFavPost(username, postId, error => {
if (error) {
next(error)

return
}

res.status(204).send()
})
} catch (error) {
next(error)
}
}
21 changes: 21 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/toggleFollowUserHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { logic } from '../../cor/index.js'

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

const { targetUsername } = req.params

try {
logic.toggleFollowUser(username, targetUsername, error => {
if (error) {
next(error)

return
}

res.status(204).send()
})
} catch (error) {
next(error)
}
}
21 changes: 21 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/toggleLikePostHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { logic } from '../../cor/index.js'

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

const { postId } = req.params

try {
logic.toggleLikePost(username, postId, error => {
if (error) {
next(error)

return
}

res.status(204).send()
})
} catch (error) {
next(error)
}
}
23 changes: 23 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/updatePostCaptionHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { logic } from 'cor'

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

const { postId } = req.params

const { caption } = req.body

try {
logic.updatePostCaption(username, postId, caption, error => {
if (error) {
next(error)

return
}

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

0 comments on commit babd2ca

Please sign in to comment.