Refactored routers, changed file name

This commit is contained in:
Hackntosh 2023-07-28 10:41:28 -03:00
parent fcb4a83787
commit 141712edb4
18 changed files with 113 additions and 64 deletions

View file

@ -1,24 +1,23 @@
/* eslint-disable @typescript-eslint/no-misused-promises */ /* eslint-disable @typescript-eslint/no-misused-promises */
import { Router } from 'express' import { Router } from 'express'
// Controllers // Controllers
import commentCreateController from './comments/create' import comments from './comments'
import commentDeleteController from './comments/delete'
import commentFetchController from './comments/fetch-info'
import commentFetchLikesController from './comments/fetch-likes'
import commentUpdateController from './comments/update'
// Middlewares // Middlewares
import ensureAuthenticated from '../middlewares/ensure-authenticated' import authenticated from '../middlewares/authenticated'
const commentsRouter = Router() const commentsRouter = Router()
// Posts related // GET
commentsRouter.post('/create', ensureAuthenticated, commentCreateController) commentsRouter.get('/fetch-likes', comments.fetchLikes)
commentsRouter.post('/delete', ensureAuthenticated, commentDeleteController) commentsRouter.get('/info', comments.fetch)
commentsRouter.get('/info', commentFetchController)
commentsRouter.put('/update', ensureAuthenticated, commentUpdateController) // POST
commentsRouter.get('/fetch-likes', commentFetchLikesController) commentsRouter.post('/create', authenticated, comments.create)
commentsRouter.post('/delete', authenticated, comments.delete)
// PUT
commentsRouter.put('/update', authenticated, comments.update)
export default commentsRouter export default commentsRouter

View file

@ -1,4 +1,4 @@
import comment from '../../services/comments/index' import comment from '../../services/comments'
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { badRequest } from '../../lib/http-errors' import { badRequest } from '../../lib/http-errors'

View file

@ -1,4 +1,4 @@
import comment from '../../services/comments/index' import comment from '../../services/comments'
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { badRequest } from '../../lib/http-errors' import { badRequest } from '../../lib/http-errors'

View file

@ -1,4 +1,4 @@
import comment from '../../services/comments/index' import comment from '../../services/comments'
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { badRequest } from '../../lib/http-errors' import { badRequest } from '../../lib/http-errors'

View file

@ -1,4 +1,4 @@
import comment from '../../services/comments/index' import comment from '../../services/comments'
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { badRequest } from '../../lib/http-errors' import { badRequest } from '../../lib/http-errors'

View file

@ -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

View file

@ -1,4 +1,4 @@
import comment from '../../services/comments/index' import comment from '../../services/comments'
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { badRequest } from '../../lib/http-errors' import { badRequest } from '../../lib/http-errors'

View file

@ -3,22 +3,22 @@
import { Router } from 'express' import { Router } from 'express'
// Controllers // Controllers
import postCreateController from './posts/create' import post from './posts'
import postDeleteController from './posts/delete'
import postFetchInfoController from './posts/fetch-info'
import postUpdateController from './posts/update'
// Middlewares // Middlewares
import ensureAuthenticated from '../middlewares/ensure-authenticated' import authenticated from '../middlewares/authenticated'
import postFetchLikesController from './posts/fetch-likes'
const postsRouter = Router() const postsRouter = Router()
// Posts related // GET
postsRouter.post('/create', ensureAuthenticated, postCreateController) postsRouter.get('/fetch-likes', post.fetchLikes)
postsRouter.post('/delete', ensureAuthenticated, postDeleteController) postsRouter.get('/info', post.fetch)
postsRouter.get('/info', postFetchInfoController)
postsRouter.put('/update', ensureAuthenticated, postUpdateController) // POST
postsRouter.get('/fetch-likes', postFetchLikesController) postsRouter.post('/create', authenticated, post.create)
postsRouter.post('/delete', authenticated, post.delete)
// PUT
postsRouter.put('/update', authenticated, post.update)
export default postsRouter export default postsRouter

View file

@ -1,4 +1,4 @@
import post from '../../services/posts/index' import post from '../../services/posts'
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { badRequest } from '../../lib/http-errors' import { badRequest } from '../../lib/http-errors'

View file

@ -1,4 +1,4 @@
import post from '../../services/posts/index' import post from '../../services/posts'
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { badRequest } from '../../lib/http-errors' import { badRequest } from '../../lib/http-errors'

View file

@ -1,4 +1,4 @@
import post from '../../services/posts/index' import post from '../../services/posts'
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { badRequest } from '../../lib/http-errors' import { badRequest } from '../../lib/http-errors'
@ -9,7 +9,7 @@ async function postFetchInfoController (req: Request, res: Response): Promise<vo
return badRequest(res, 'Missing post id') return badRequest(res, 'Missing post id')
} }
const result = await post.info(id) const result = await post.fetch(id)
if (result instanceof Error) { if (result instanceof Error) {
return badRequest(res, result.message) return badRequest(res, result.message)

View file

@ -1,4 +1,4 @@
import post from '../../services/posts/index' import post from '../../services/posts'
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { badRequest } from '../../lib/http-errors' import { badRequest } from '../../lib/http-errors'

View file

@ -0,0 +1,15 @@
import postCreateController from './create'
import postDeleteController from './delete'
import postFetchInfoController from './fetch-info'
import postUpdateController from './update'
import postFetchLikesController from './fetch-likes'
const post = {
create: postCreateController,
delete: postDeleteController,
fetch: postFetchInfoController,
fetchLikes: postFetchLikesController,
update: postUpdateController
}
export default post

View file

@ -1,4 +1,4 @@
import post from '../../services/posts/index' import post from '../../services/posts'
import type { Request, Response } from 'express' import type { Request, Response } from 'express'
import { badRequest } from '../../lib/http-errors' import { badRequest } from '../../lib/http-errors'

View file

@ -1,37 +1,30 @@
/* eslint-disable @typescript-eslint/no-misused-promises */ /* eslint-disable @typescript-eslint/no-misused-promises */
import { Router } from 'express' import { Router } from 'express'
// Controllers // Controllers
import userAuthController from './users/auth' import user from './users'
import userDeleteController from './users/delete'
import userFollowController from './users/follow-user'
import userFetchInfoController from './users/fetch-info'
import userFetchPostsController from './users/fetch-posts'
import userLikeCommentController from './users/like-comment'
import userLikePostController from './users/like-post'
import userSearchController from './users/search-user'
import userSignupController from './users/signup'
import userUpdateController from './users/update'
import userUploadPictureController from './users/upload-picture'
// Middlewares // Middlewares
import ensureAuthenticated from '../middlewares/ensure-authenticated' import authenticated from '../middlewares/authenticated'
import uploadFile from '../middlewares/upload-image' import uploadFile from '../middlewares/upload-image'
const usersRouter = Router() const usersRouter = Router()
// Users related // GET
usersRouter.post('/auth', userAuthController) usersRouter.get('/fetch-posts', user.fetchPosts)
usersRouter.post('/delete', ensureAuthenticated, userDeleteController) usersRouter.get('/info', user.fetchInfo)
usersRouter.get('/info', userFetchInfoController) usersRouter.get('/search', user.searchUser)
usersRouter.post('/signup', userSignupController)
usersRouter.put('/update', ensureAuthenticated, userUpdateController) // POST
usersRouter.put('/profile-picture/upload', ensureAuthenticated, uploadFile, userUploadPictureController) usersRouter.post('/auth', user.auth)
usersRouter.post('/like-post', ensureAuthenticated, userLikePostController) usersRouter.post('/delete', authenticated, user.delete)
usersRouter.post('/follow-user', ensureAuthenticated, userFollowController) usersRouter.post('/follow-user', authenticated, user.follow)
usersRouter.get('/fetch-posts', userFetchPostsController) usersRouter.post('/like-comment', authenticated, user.likeComment)
usersRouter.get('/search', userSearchController) usersRouter.post('/like-post', authenticated, user.likePost)
usersRouter.post('/like-comment', ensureAuthenticated, userLikeCommentController) usersRouter.post('/signup', user.signup)
// PUT
usersRouter.put('/profile-picture/upload', authenticated, uploadFile, user.uploadPicture)
usersRouter.put('/update', authenticated, user.update)
export default usersRouter export default usersRouter

View file

@ -0,0 +1,27 @@
import userAuthController from './auth'
import userDeleteController from './delete'
import userFollowController from './follow-user'
import userFetchInfoController from './fetch-info'
import userFetchPostsController from './fetch-posts'
import userLikeCommentController from './like-comment'
import userLikePostController from './like-post'
import userSearchController from './search-user'
import userSignupController from './signup'
import userUpdateController from './update'
import userUploadPictureController from './upload-picture'
const user = {
auth: userAuthController,
delete: userDeleteController,
fetchInfo: userFetchInfoController,
fetchPosts: userFetchPostsController,
follow: userFollowController,
likeComment: userLikeCommentController,
likePost: userLikePostController,
searchUser: userSearchController,
signup: userSignupController,
update: userUpdateController,
uploadPicture: userUploadPictureController
}
export default user

View file

@ -4,7 +4,7 @@ import type { Response, Request, NextFunction } from 'express'
import jwtPayload from '../interfaces/jwt' import jwtPayload from '../interfaces/jwt'
import { unauthorized } from '../lib/http-errors' import { unauthorized } from '../lib/http-errors'
async function ensureAuthenticated (req: Request, res: Response, next: NextFunction): Promise<void> { async function authenticated (req: Request, res: Response, next: NextFunction): Promise<void> {
if (req.headers.authorization === undefined || req.headers.authorization.length === 0) { if (req.headers.authorization === undefined || req.headers.authorization.length === 0) {
return unauthorized(res, 'Missing token') return unauthorized(res, 'Missing token')
} }
@ -44,4 +44,4 @@ async function ensureAuthenticated (req: Request, res: Response, next: NextFunct
} }
} }
export default ensureAuthenticated export default authenticated

View file

@ -7,8 +7,8 @@ import postUpdateService from './update'
const post = { const post = {
create: postCreateService, create: postCreateService,
delete: postDeleteService, delete: postDeleteService,
fetch: postFetchInfoService,
fetchLikes: postFetchLikesService, fetchLikes: postFetchLikesService,
info: postFetchInfoService,
update: postUpdateService update: postUpdateService
} }