Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/ponies #72

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added staff/lucas-orts/.gitkeep
Empty file.
28 changes: 28 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/authenticateUserHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import jwt from 'jsonwebtoken'

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)
.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)
}
}
14 changes: 14 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/createPostHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { logic } from 'cor'

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

const { image, caption } = req.body
try {
logic.createPost(userId, image, caption)
.then(() => res.status(201).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
}
14 changes: 14 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/deletePostHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { logic } from 'cor'

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

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

export default (req, res, next) => {
const { userId } = req
try {
logic.getAllFavPosts(userId)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
next(error)
}
}
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 } = req

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

export default (req, res, next) => {
const { userId } = req
try {
logic.getAllPosts(userId)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
next(error)
}
}
14 changes: 14 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/getUserNameHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { logic } from 'cor'

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

const { targetUserId } = req.params
try {
logic.getUserName(userId, targetUserId)
.then(name => res.json(name))
.catch(error => next(error))
} catch (error) {
next(error)
}
}
29 changes: 29 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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'
import searchPostsHandler from './searchPosts.Handler.js'

export {
registerUserHandler,
authenticateUserHandler,
getUserNameHandler,
getAllPostsHandler,
getAllFollowingUserPostsHandler,
getAllFavPostsHandler,
createPostHandler,
deletePostHandler,
toggleLikePostHandler,
toggleFavPostHandler,
toggleFollowUserHandler,
updatePostCaptionHandler,
searchPostsHandler
}
12 changes: 12 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/registerUserHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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)
.then(() => res.status(201).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
}
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)
}
}
14 changes: 14 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/toggleFavPostHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { logic } from 'cor'

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

const { postId } = req.params
try {
logic.toggleFavPost(userId, postId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
}
14 changes: 14 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/toggleFollowUserHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { logic } from 'cor'

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

const { targetUserId } = req.params
try {
logic.toggleFollowUser(userId, targetUserId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
}
14 changes: 14 additions & 0 deletions staff/lucas-orts/ponies/api/handlers/toggleLikePostHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { logic } from 'cor'

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

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

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

const { postId } = req.params

const { caption } = req.body
try {
logic.updatePostCaption(userId, postId, caption)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
}
11 changes: 11 additions & 0 deletions staff/lucas-orts/ponies/api/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from 'express'
const app = express()
const port = 3000

app.get('/', (req, res) => {
res.send('Hello World!')
})

app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
75 changes: 75 additions & 0 deletions staff/lucas-orts/ponies/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'dotenv/config'
import express from 'express'

import { mongoose } from '../cor/index.js'

import { cors, jsonBodyParser, jwtVerifier, errorHandler } from './middlewares/index.js'
import {
registerUserHandler,
authenticateUserHandler,
getAllFollowingUserPostsHandler,
getUserNameHandler,
getAllPostsHandler,
getAllFavPostsHandler,
createPostHandler,
deletePostHandler,
toggleLikePostHandler,
toggleFavPostHandler,
toggleFollowUserHandler,
updatePostCaptionHandler,
searchPostsHandler
} from './handlers/index.js'

mongoose.connect(process.env.MONGODB_URI)
.then(() => {
console.info(`API connected to ${process.env.MONGODB_URI}`)

const api = express()

api.use(cors)

api.get('/', (req, res) => {
res.send('Hello, World!')
})

api.post('/users', jsonBodyParser, registerUserHandler)

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

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

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

api.get('/posts/follows', jwtVerifier, getAllFollowingUserPostsHandler)

api.get('/posts/favs', jwtVerifier, getAllFavPostsHandler)

api.post('/posts', jwtVerifier, jsonBodyParser, createPostHandler)

api.delete('/posts/:postId', jwtVerifier, deletePostHandler)

api.patch('/posts/:postId/likes', jwtVerifier, toggleLikePostHandler)

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

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}`))
})
.catch(error => console.error(error))
Loading