Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
feat(api): basic post endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
soulsam480 committed Aug 6, 2021
1 parent 911d23c commit e97cb16
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
50 changes: 50 additions & 0 deletions api/src/controllers/post.ts
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 };
2 changes: 2 additions & 0 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { authRouter } from 'src/controllers/auth';
import { setupOauth } from './oauth';
import { serve, setup } from 'swagger-ui-express';
import specs from '../swagger-spec.json';
import { postRouter } from './controllers/post';

const PORT = parseEnv<number>('PORT') || 3000;

Expand All @@ -33,6 +34,7 @@ async function main() {
setupOauth(app);

app.use('/auth', authRouter);
app.use('/posts', postRouter);
app.use('/api-docs', serve, setup(specs));

try {
Expand Down
30 changes: 30 additions & 0 deletions api/src/services/post.ts
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;
}
}
6 changes: 6 additions & 0 deletions api/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ export function sanitizeResponse(
{},
);
}

export function formatResponse<T>(data: T) {
return {
data,
};
}

0 comments on commit e97cb16

Please sign in to comment.