mirror of
https://github.com/hknsh/project-knedita.git
synced 2024-11-28 17:41:15 +00:00
Created new route
This commit is contained in:
parent
b6ed80db68
commit
a075da4aba
6 changed files with 67 additions and 0 deletions
|
@ -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)
|
||||
|
|
22
src/controllers/users/fetch-user.ts
Normal file
22
src/controllers/users/fetch-user.ts
Normal file
|
@ -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<void> {
|
||||
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
|
|
@ -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,
|
||||
|
|
|
@ -40,6 +40,7 @@ async function userAuthService({
|
|||
|
||||
return {
|
||||
token: bearer,
|
||||
user: user.username,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
39
src/services/users/fetch-user.ts
Normal file
39
src/services/users/fetch-user.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import prisma from 'clients/prisma-client'
|
||||
|
||||
async function userFetchUserService(
|
||||
id: string,
|
||||
): Promise<Record<string, unknown> | 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
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue