From 1da6289d9c0626e208090cfa9cfafb01157bd8a2 Mon Sep 17 00:00:00 2001 From: Othniel9 Date: Tue, 26 Oct 2021 12:42:33 -0400 Subject: [PATCH] Ticket_13 done --- src/routes/CreatePostRoute.test.ts | 3 ++- src/routes/CreatePostRoute.ts | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/routes/CreatePostRoute.test.ts b/src/routes/CreatePostRoute.test.ts index 831d07e..8ba11c6 100644 --- a/src/routes/CreatePostRoute.test.ts +++ b/src/routes/CreatePostRoute.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable prettier/prettier */ import { TEST_AUTH_COOKIE, TEST_USER } from '../../jest.setup'; import Post, { PostDocument, PostType } from '../models/Post'; import TestUtils from '../utils/TestUtils'; @@ -11,7 +12,7 @@ const TEST_POST_CONTENT = 'I have an update for you all!'; * npm run test CreatePost * - Delete this comment. */ -describe.skip('POST /posts', () => { +describe('POST /posts', () => { test('If the user is not authenticated, should return a 401.', async () => { await TestUtils.agent .post('/posts') diff --git a/src/routes/CreatePostRoute.ts b/src/routes/CreatePostRoute.ts index 73df192..7772316 100644 --- a/src/routes/CreatePostRoute.ts +++ b/src/routes/CreatePostRoute.ts @@ -1,4 +1,6 @@ +/* eslint-disable prettier/prettier */ import { body } from 'express-validator'; +import { min } from 'mathjs'; import Post, { PostDocument, PostType } from '../models/Post'; import { ApplicationRequest } from '../utils/ApplicationRequest'; @@ -20,9 +22,9 @@ export default class CreatePostRoute extends BaseRoute { * - Fill in the path string with the appropriate path to this endpoint. * - Delete this comment. */ - authenticated: false, - method: null, - path: '/' + authenticated: true, + method: RouteMethod.POST, + path: '/posts' }); } @@ -38,7 +40,9 @@ export default class CreatePostRoute extends BaseRoute { body('type') .if((value: PostType) => !!value) .isIn(Object.values(PostType)) - .withMessage('You must choose a valid PostType.') + .withMessage('You must choose a valid PostType.'), + + body('content').isLength({ min: 1 }).withMessage('Not a valid message') ]; } @@ -54,6 +58,14 @@ export default class CreatePostRoute extends BaseRoute { // content, and post type. Then return the post! // TODO: (13.04) Return the post! - return null; + const { content, type } = req.params; + + const post: PostDocument = await Post.create({ + author: req.user?._id, + content: 'I have an update for you all!', + type: 'WIN' + }); + + return post; } }