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 */
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

View file

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

View file

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

View file

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

View file

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

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 { badRequest } from '../../lib/http-errors'

View file

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

View file

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

View file

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

View file

@ -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<vo
return badRequest(res, 'Missing post id')
}
const result = await post.info(id)
const result = await post.fetch(id)
if (result instanceof Error) {
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 { 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 { badRequest } from '../../lib/http-errors'

View file

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