forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api refactoring index into handlers files and middleware files b00tc4…
- Loading branch information
1 parent
02a3084
commit babd2ca
Showing
114 changed files
with
5,527 additions
and
1,499 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
staff/lucas-orts/ponies/api/handlers/authenticateUserHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
staff/lucas-orts/ponies/api/handlers/getAllFavPostsHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
staff/lucas-orts/ponies/api/handlers/getAllFollowingUserPostsHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
staff/lucas-orts/ponies/api/handlers/getAllPostsHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
staff/lucas-orts/ponies/api/handlers/getUserNameHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
staff/lucas-orts/ponies/api/handlers/registerUserHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
staff/lucas-orts/ponies/api/handlers/toggleFavPostHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
staff/lucas-orts/ponies/api/handlers/toggleFollowUserHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
staff/lucas-orts/ponies/api/handlers/toggleLikePostHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
staff/lucas-orts/ponies/api/handlers/updatePostCaptionHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.