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

Commit

Permalink
Merge pull request #149 from swarmion/feat/create-post
Browse files Browse the repository at this point in the history
feat(forum): add sample create post lambda
  • Loading branch information
fargito authored Apr 4, 2022
2 parents f46660d + c8ce2cd commit fb1b6c2
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 1 deletion.
13 changes: 13 additions & 0 deletions backend/forum/functions/createPost/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createPostContract } from '@swarmion-starter/forum-contracts';
import {
getHandlerPath,
LambdaFunction,
} from '@swarmion-starter/serverless-helpers';

const config: LambdaFunction = {
environment: {},
handler: getHandlerPath(__dirname),
events: [createPostContract.trigger],
};

export default config;
4 changes: 4 additions & 0 deletions backend/forum/functions/createPost/handler.mock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"pathParameters": { "threadId": "fooBar" },
"body": { "content": "Hello, I am Swarmion!" }
}
14 changes: 14 additions & 0 deletions backend/forum/functions/createPost/handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { handler } from './handler';

describe('createPost handler', () => {
it('should return a post with the created content', async () => {
const postContent = 'Hello from Swarmion';

const { content } = await handler({
pathParameters: { threadId: 'blob' },
body: { content: postContent },
});

expect(content).toEqual(postContent);
});
});
21 changes: 21 additions & 0 deletions backend/forum/functions/createPost/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createPostContract } from '@swarmion-starter/forum-contracts';
import { applyHttpMiddlewares } from '@swarmion-starter/serverless-helpers';

export const handler = createPostContract.handler(async event => {
const { threadId } = event.pathParameters;
const { content } = event.body;

await Promise.resolve({ threadId });

return {
id: 'myFirstPost',
createdAt: '2021-10-25T12:12:00Z',
editedAt: null,
content,
authorId: 'author2',
};
});

export const main = applyHttpMiddlewares(handler, {
inputSchema: createPostContract.inputSchema,
});
3 changes: 2 additions & 1 deletion backend/forum/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import createPost from './createPost/config';
import getThreadAndPosts from './getThreadAndPosts/config';

export const functions = { getThreadAndPosts };
export const functions = { getThreadAndPosts, createPost };
33 changes: 33 additions & 0 deletions contracts/forum-contracts/src/contracts/requests/createPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ApiGatewayContract } from '@swarmion/serverless-contracts';

import { postEntitySchema } from 'contracts/entities';

const pathParametersSchema = {
type: 'object',
properties: {
threadId: { type: 'string' },
},
required: ['threadId'],
additionalProperties: false,
} as const;

const bodySchema = {
type: 'object',
properties: {
content: { type: 'string' },
},
required: ['content'],
additionalProperties: false,
} as const;

export const createPostContract = new ApiGatewayContract({
id: 'forum-createPost',
path: '/forum/thread/{threadId}',
method: 'POST',
integrationType: 'httpApi',
pathParametersSchema,
queryStringParametersSchema: undefined,
bodySchema,
headersSchema: undefined,
outputSchema: postEntitySchema,
});
1 change: 1 addition & 0 deletions contracts/forum-contracts/src/contracts/requests/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './getThreadWithPosts';
export * from './createPost';

0 comments on commit fb1b6c2

Please sign in to comment.