From a075da4aba1de8931db94de825eeeb365036a1aa Mon Sep 17 00:00:00 2001 From: CookieDasora Date: Tue, 5 Sep 2023 20:29:40 -0300 Subject: [PATCH] Created new route --- src/controllers/users-router.ts | 1 + src/controllers/users/fetch-user.ts | 22 ++++++++++++++++ src/controllers/users/index.ts | 2 ++ src/services/users/auth.ts | 1 + src/services/users/fetch-user.ts | 39 +++++++++++++++++++++++++++++ src/services/users/index.ts | 2 ++ 6 files changed, 67 insertions(+) create mode 100644 src/controllers/users/fetch-user.ts create mode 100644 src/services/users/fetch-user.ts diff --git a/src/controllers/users-router.ts b/src/controllers/users-router.ts index ad33c01..50c54f1 100644 --- a/src/controllers/users-router.ts +++ b/src/controllers/users-router.ts @@ -17,6 +17,7 @@ usersRouter.get('/search', user.searchUser) // POST usersRouter.post('/auth', user.auth) usersRouter.post('/delete', authenticated, user.delete) +usersRouter.post('/fetch', authenticated, user.fetchUser) usersRouter.post('/follow-user', authenticated, user.follow) usersRouter.post('/like-comment', authenticated, user.likeComment) usersRouter.post('/like-post', authenticated, user.likePost) diff --git a/src/controllers/users/fetch-user.ts b/src/controllers/users/fetch-user.ts new file mode 100644 index 0000000..788c003 --- /dev/null +++ b/src/controllers/users/fetch-user.ts @@ -0,0 +1,22 @@ +import user from 'services/users' +import type { Request, Response } from 'express' +import { badRequest } from 'helpers/http-errors' +import handleResponse from 'helpers/handle-response' + +async function userFetchUserController( + req: Request, + res: Response, +): Promise { + const id = res.locals.user.id + + if (id === undefined) { + badRequest(res, 'Missing id') + return + } + + const result = await user.fetchUser(id) + + handleResponse(res, result) +} + +export default userFetchUserController diff --git a/src/controllers/users/index.ts b/src/controllers/users/index.ts index 5caf538..7bf84c5 100644 --- a/src/controllers/users/index.ts +++ b/src/controllers/users/index.ts @@ -3,6 +3,7 @@ import userDeleteController from './delete' import userFollowController from './follow-user' import userFetchInfoController from './fetch-info' import userFetchPostsController from './fetch-posts' +import userFetchUserController from './fetch-user' import userLikeCommentController from './like-comment' import userLikePostController from './like-post' import userSearchController from './search-user' @@ -17,6 +18,7 @@ const user = { delete: userDeleteController, fetchInfo: userFetchInfoController, fetchPosts: userFetchPostsController, + fetchUser: userFetchUserController, follow: userFollowController, likeComment: userLikeCommentController, likePost: userLikePostController, diff --git a/src/services/users/auth.ts b/src/services/users/auth.ts index d8def6f..218662c 100644 --- a/src/services/users/auth.ts +++ b/src/services/users/auth.ts @@ -40,6 +40,7 @@ async function userAuthService({ return { token: bearer, + user: user.username, } } diff --git a/src/services/users/fetch-user.ts b/src/services/users/fetch-user.ts new file mode 100644 index 0000000..0583daa --- /dev/null +++ b/src/services/users/fetch-user.ts @@ -0,0 +1,39 @@ +import prisma from 'clients/prisma-client' + +async function userFetchUserService( + id: string, +): Promise | Error> { + const user = await prisma.user.findFirst({ + where: { + id, + }, + select: { + id: true, + profileImage: true, + displayName: true, + username: true, + createdAt: true, + posts: { + select: { + id: true, + content: true, + createdAt: true, + updatedAt: true, + }, + }, + likedPosts: { + select: { + postId: true, + }, + }, + }, + }) + + if (user === null) { + return new Error('User not found') + } + + return user +} + +export default userFetchUserService diff --git a/src/services/users/index.ts b/src/services/users/index.ts index fad1f8b..1c921f9 100644 --- a/src/services/users/index.ts +++ b/src/services/users/index.ts @@ -3,6 +3,7 @@ import userDeleteService from './delete' import userFollowService from './follow-user' import userFetchPostsService from './fetch-posts' import userFetchInfoService from './fetch-info' +import userFetchUserService from './fetch-user' import userLikeCommentService from './like-comment' import userLikePostService from './like-post' import userSearchService from './search-user' @@ -17,6 +18,7 @@ const user = { delete: userDeleteService, fetchInfo: userFetchInfoService, fetchPosts: userFetchPostsService, + fetchUser: userFetchUserService, follow: userFollowService, likeComment: userLikeCommentService, likePost: userLikePostService,