diff --git a/src/controllers/comments-router.ts b/src/controllers/comments-router.ts index b225249..96404b8 100644 --- a/src/controllers/comments-router.ts +++ b/src/controllers/comments-router.ts @@ -1,24 +1,23 @@ /* eslint-disable @typescript-eslint/no-misused-promises */ - import { Router } from 'express' // Controllers -import commentCreateController from './comments/create' -import commentDeleteController from './comments/delete' -import commentFetchController from './comments/fetch-info' -import commentFetchLikesController from './comments/fetch-likes' -import commentUpdateController from './comments/update' +import comments from './comments' // Middlewares -import ensureAuthenticated from '../middlewares/ensure-authenticated' +import authenticated from '../middlewares/authenticated' const commentsRouter = Router() -// Posts related -commentsRouter.post('/create', ensureAuthenticated, commentCreateController) -commentsRouter.post('/delete', ensureAuthenticated, commentDeleteController) -commentsRouter.get('/info', commentFetchController) -commentsRouter.put('/update', ensureAuthenticated, commentUpdateController) -commentsRouter.get('/fetch-likes', commentFetchLikesController) +// GET +commentsRouter.get('/fetch-likes', comments.fetchLikes) +commentsRouter.get('/info', comments.fetch) + +// POST +commentsRouter.post('/create', authenticated, comments.create) +commentsRouter.post('/delete', authenticated, comments.delete) + +// PUT +commentsRouter.put('/update', authenticated, comments.update) export default commentsRouter diff --git a/src/controllers/comments/create.ts b/src/controllers/comments/create.ts index 2d5ceca..2565bce 100644 --- a/src/controllers/comments/create.ts +++ b/src/controllers/comments/create.ts @@ -1,4 +1,4 @@ -import comment from '../../services/comments/index' +import comment from '../../services/comments' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/comments/delete.ts b/src/controllers/comments/delete.ts index 34463ef..0358b83 100644 --- a/src/controllers/comments/delete.ts +++ b/src/controllers/comments/delete.ts @@ -1,4 +1,4 @@ -import comment from '../../services/comments/index' +import comment from '../../services/comments' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/comments/fetch-info.ts b/src/controllers/comments/fetch-info.ts index 2ae5a88..73b1a04 100644 --- a/src/controllers/comments/fetch-info.ts +++ b/src/controllers/comments/fetch-info.ts @@ -1,4 +1,4 @@ -import comment from '../../services/comments/index' +import comment from '../../services/comments' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/comments/fetch-likes.ts b/src/controllers/comments/fetch-likes.ts index b3c9157..a2b95e2 100644 --- a/src/controllers/comments/fetch-likes.ts +++ b/src/controllers/comments/fetch-likes.ts @@ -1,4 +1,4 @@ -import comment from '../../services/comments/index' +import comment from '../../services/comments' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/comments/index.ts b/src/controllers/comments/index.ts new file mode 100644 index 0000000..46a6bd3 --- /dev/null +++ b/src/controllers/comments/index.ts @@ -0,0 +1,15 @@ +import commentCreateController from './create' +import commentDeleteController from './delete' +import commentFetchController from './fetch-info' +import commentFetchLikesController from './fetch-likes' +import commentUpdateController from './update' + +const comments = { + create: commentCreateController, + delete: commentDeleteController, + fetch: commentFetchController, + fetchLikes: commentFetchLikesController, + update: commentUpdateController +} + +export default comments diff --git a/src/controllers/comments/update.ts b/src/controllers/comments/update.ts index 6801fa7..88e9045 100644 --- a/src/controllers/comments/update.ts +++ b/src/controllers/comments/update.ts @@ -1,4 +1,4 @@ -import comment from '../../services/comments/index' +import comment from '../../services/comments' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/posts-router.ts b/src/controllers/posts-router.ts index 7035f1b..ad2fac1 100644 --- a/src/controllers/posts-router.ts +++ b/src/controllers/posts-router.ts @@ -3,22 +3,22 @@ import { Router } from 'express' // Controllers -import postCreateController from './posts/create' -import postDeleteController from './posts/delete' -import postFetchInfoController from './posts/fetch-info' -import postUpdateController from './posts/update' +import post from './posts' // Middlewares -import ensureAuthenticated from '../middlewares/ensure-authenticated' -import postFetchLikesController from './posts/fetch-likes' +import authenticated from '../middlewares/authenticated' const postsRouter = Router() -// Posts related -postsRouter.post('/create', ensureAuthenticated, postCreateController) -postsRouter.post('/delete', ensureAuthenticated, postDeleteController) -postsRouter.get('/info', postFetchInfoController) -postsRouter.put('/update', ensureAuthenticated, postUpdateController) -postsRouter.get('/fetch-likes', postFetchLikesController) +// GET +postsRouter.get('/fetch-likes', post.fetchLikes) +postsRouter.get('/info', post.fetch) + +// POST +postsRouter.post('/create', authenticated, post.create) +postsRouter.post('/delete', authenticated, post.delete) + +// PUT +postsRouter.put('/update', authenticated, post.update) export default postsRouter diff --git a/src/controllers/posts/create.ts b/src/controllers/posts/create.ts index 83367f7..35ca6ad 100644 --- a/src/controllers/posts/create.ts +++ b/src/controllers/posts/create.ts @@ -1,4 +1,4 @@ -import post from '../../services/posts/index' +import post from '../../services/posts' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/posts/delete.ts b/src/controllers/posts/delete.ts index e3cd3fa..f2347af 100644 --- a/src/controllers/posts/delete.ts +++ b/src/controllers/posts/delete.ts @@ -1,4 +1,4 @@ -import post from '../../services/posts/index' +import post from '../../services/posts' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/posts/fetch-info.ts b/src/controllers/posts/fetch-info.ts index 6b3343b..648cca4 100644 --- a/src/controllers/posts/fetch-info.ts +++ b/src/controllers/posts/fetch-info.ts @@ -1,4 +1,4 @@ -import post from '../../services/posts/index' +import post from '../../services/posts' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' @@ -9,7 +9,7 @@ async function postFetchInfoController (req: Request, res: Response): Promise { +async function authenticated (req: Request, res: Response, next: NextFunction): Promise { if (req.headers.authorization === undefined || req.headers.authorization.length === 0) { return unauthorized(res, 'Missing token') } @@ -44,4 +44,4 @@ async function ensureAuthenticated (req: Request, res: Response, next: NextFunct } } -export default ensureAuthenticated +export default authenticated diff --git a/src/services/posts/index.ts b/src/services/posts/index.ts index cc99081..eaa464a 100644 --- a/src/services/posts/index.ts +++ b/src/services/posts/index.ts @@ -7,8 +7,8 @@ import postUpdateService from './update' const post = { create: postCreateService, delete: postDeleteService, + fetch: postFetchInfoService, fetchLikes: postFetchLikesService, - info: postFetchInfoService, update: postUpdateService }