Skip to content

Commit

Permalink
add alert and theme b00tc4mp#60
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasOrtsBaguena committed Aug 9, 2024
1 parent babd2ca commit 70860a7
Show file tree
Hide file tree
Showing 141 changed files with 2,691 additions and 1,886 deletions.
34 changes: 15 additions & 19 deletions staff/lucas-orts/ponies/api/handlers/authenticateUserHandler.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
import jwt from 'jsonwebtoken'

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

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)
})
})
logic.authenticateUser(username, password)
.then(userId =>
jwt.sign({ sub: userId }, process.env.JWT_SECRET, (error, token) => {
if (error) {
next(new SessionError(error.message))

return
}

res.json(token)
})
)
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
17 changes: 5 additions & 12 deletions staff/lucas-orts/ponies/api/handlers/createPostHandler.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { logic } from '../../cor/index.js'
import { logic } from 'cor'

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

const { image, caption } = req.body

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

return
}

res.status(201).send()
})
logic.createPost(userId, image, caption)
.then(() => res.status(201).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
17 changes: 5 additions & 12 deletions staff/lucas-orts/ponies/api/handlers/deletePostHandler.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { logic } from '../../cor/index.js'
import { logic } from 'cor'

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

const { postId } = req.params

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

return
}

res.status(204).send()
})
logic.deletePost(userId, postId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
17 changes: 5 additions & 12 deletions staff/lucas-orts/ponies/api/handlers/getAllFavPostsHandler.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { logic } from '../../cor/index.js'
import { logic } from 'cor'

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

const { userId } = req
try {
logic.getAllFavPosts(username, (error, posts) => {
if (error) {
next(error)

return
}

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

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

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

return
}

res.json(posts)
})
logic.getAllFollowingUserPosts(userId)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
17 changes: 5 additions & 12 deletions staff/lucas-orts/ponies/api/handlers/getAllPostsHandler.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { logic } from '../../cor/index.js'
import { logic } from 'cor'

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

const { userId } = req
try {
logic.getAllPosts(username, (error, posts) => {
if (error) {
next(error)

return
}

res.json(posts)
})
logic.getAllPosts(userId)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
19 changes: 6 additions & 13 deletions staff/lucas-orts/ponies/api/handlers/getUserNameHandler.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { logic } from '../../cor/index.js'
import { logic } from 'cor'

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

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

const { targetUserId } = req.params
try {
logic.getUserName(username, targetUsername, (error, name) => {
if (error) {
next(error)

return
}

res.json(name)
})
logic.getUserName(userId, targetUserId)
.then(name => res.json(name))
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
4 changes: 3 additions & 1 deletion staff/lucas-orts/ponies/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import toggleLikePostHandler from './toggleLikePostHandler.js'
import toggleFavPostHandler from './toggleFavPostHandler.js'
import toggleFollowUserHandler from './toggleFollowUserHandler.js'
import updatePostCaptionHandler from './updatePostCaptionHandler.js'
import searchPostsHandler from './searchPosts.Handler.js'

export {
registerUserHandler,
Expand All @@ -23,5 +24,6 @@ export {
toggleLikePostHandler,
toggleFavPostHandler,
toggleFollowUserHandler,
updatePostCaptionHandler
updatePostCaptionHandler,
searchPostsHandler
}
13 changes: 3 additions & 10 deletions staff/lucas-orts/ponies/api/handlers/registerUserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@ 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()
})
logic.registerUser(name, surname, email, username, password, passwordRepeat)
.then(() => res.status(201).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
13 changes: 13 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/searchPosts.Handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { logic } from 'cor'

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

try {
logic.searchPosts(userId, q)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
next(error)
}
}
17 changes: 5 additions & 12 deletions staff/lucas-orts/ponies/api/handlers/toggleFavPostHandler.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { logic } from '../../cor/index.js'
import { logic } from 'cor'

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

const { postId } = req.params

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

return
}

res.status(204).send()
})
logic.toggleFavPost(userId, postId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
19 changes: 6 additions & 13 deletions staff/lucas-orts/ponies/api/handlers/toggleFollowUserHandler.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { logic } from '../../cor/index.js'
import { logic } from 'cor'

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

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

const { targetUserId } = req.params
try {
logic.toggleFollowUser(username, targetUsername, error => {
if (error) {
next(error)

return
}

res.status(204).send()
})
logic.toggleFollowUser(userId, targetUserId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
17 changes: 5 additions & 12 deletions staff/lucas-orts/ponies/api/handlers/toggleLikePostHandler.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { logic } from '../../cor/index.js'
import { logic } from 'cor'

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

const { postId } = req.params

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

return
}

res.status(204).send()
})
logic.toggleLikePost(userId, postId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
15 changes: 4 additions & 11 deletions staff/lucas-orts/ponies/api/handlers/updatePostCaptionHandler.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { logic } from 'cor'

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, error => {
if (error) {
next(error)

return
}

res.status(204).send()
})
logic.updatePostCaption(userId, postId, caption)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
Expand Down
19 changes: 16 additions & 3 deletions staff/lucas-orts/ponies/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
toggleLikePostHandler,
toggleFavPostHandler,
toggleFollowUserHandler,
updatePostCaptionHandler
updatePostCaptionHandler,
searchPostsHandler
} from './handlers/index.js'

mongoose.connect(process.env.MONGODB_URI)
Expand All @@ -35,7 +36,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,10 +52,22 @@ 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)

api.get('/posts/search', jwtVerifier, searchPostsHandler)

api.get('/search', (req, res, next) => {
const colors = ['red', 'green', 'blue', 'violette', 'brown', 'yellow']

const { q } = req.query

const filtered = colors.filter(color => color.includes(q))

res.json(filtered)
})

api.use(errorHandler)

api.listen(process.env.PORT, () => console.info(`API listening on PORT ${process.env.PORT}`))
Expand Down
Loading

0 comments on commit 70860a7

Please sign in to comment.