This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
911d23c
commit e97cb16
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Router } from 'express'; | ||
import { auth } from 'src/middlewares/auth'; | ||
import { createPost, getAllPosts, getPostsByUserId } from 'src/services/post'; | ||
import { createError, formatResponse } from 'src/utils/helpers'; | ||
|
||
const postRouter = Router(); | ||
|
||
postRouter.use(auth); | ||
|
||
postRouter.get('/', async (req, res) => { | ||
try { | ||
const posts = await getAllPosts(); | ||
res.send(formatResponse(posts)); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).send(createError('Internal server error.', error)); | ||
} | ||
}); | ||
|
||
postRouter.get('/user/:id', async (req, res) => { | ||
const { | ||
params: { id }, | ||
} = req; | ||
|
||
if (!id) return res.status(400).send(createError('User id not found')); | ||
|
||
try { | ||
const posts = await getPostsByUserId(id); | ||
res.send(formatResponse(posts)); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).send(createError('Internal server error.', error)); | ||
} | ||
}); | ||
|
||
postRouter.post('/', async (req, res) => { | ||
const { userId, body } = req; | ||
|
||
if (!Object.keys(body.post).length) res.status(400).send(createError('Post not found')); | ||
|
||
try { | ||
const post = await createPost({ ...(body.post as any), user: userId }); | ||
res.send(formatResponse(post)); | ||
} catch (error) { | ||
console.log(error); | ||
res.status(500).send(createError('Internal server error.', error)); | ||
} | ||
}); | ||
|
||
export { postRouter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { CreateQuery } from 'mongoose'; | ||
import { Post, postModel } from 'src/entities/post'; | ||
import { sanitizeResponse } from 'src/utils/helpers'; | ||
|
||
export async function createPost(post: CreateQuery<Post>) { | ||
try { | ||
const newPost = await postModel.create({ ...post }); | ||
return { ...sanitizeResponse(newPost.toJSON()) }; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
export async function getAllPosts() { | ||
try { | ||
const allPosts = await postModel.find(); | ||
return allPosts.map((post) => ({ ...sanitizeResponse(post.toJSON()) })); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
export async function getPostsByUserId(id: string) { | ||
try { | ||
const allPosts = await postModel.find({ user: id }); | ||
return allPosts.map((post) => ({ ...sanitizeResponse(post.toJSON()) })); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,9 @@ export function sanitizeResponse( | |
{}, | ||
); | ||
} | ||
|
||
export function formatResponse<T>(data: T) { | ||
return { | ||
data, | ||
}; | ||
} |