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

Commit fb1b6c2

Browse files
authored
Merge pull request #149 from swarmion/feat/create-post
feat(forum): add sample create post lambda
2 parents f46660d + c8ce2cd commit fb1b6c2

7 files changed

Lines changed: 88 additions & 1 deletion

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createPostContract } from '@swarmion-starter/forum-contracts';
2+
import {
3+
getHandlerPath,
4+
LambdaFunction,
5+
} from '@swarmion-starter/serverless-helpers';
6+
7+
const config: LambdaFunction = {
8+
environment: {},
9+
handler: getHandlerPath(__dirname),
10+
events: [createPostContract.trigger],
11+
};
12+
13+
export default config;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"pathParameters": { "threadId": "fooBar" },
3+
"body": { "content": "Hello, I am Swarmion!" }
4+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { handler } from './handler';
2+
3+
describe('createPost handler', () => {
4+
it('should return a post with the created content', async () => {
5+
const postContent = 'Hello from Swarmion';
6+
7+
const { content } = await handler({
8+
pathParameters: { threadId: 'blob' },
9+
body: { content: postContent },
10+
});
11+
12+
expect(content).toEqual(postContent);
13+
});
14+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createPostContract } from '@swarmion-starter/forum-contracts';
2+
import { applyHttpMiddlewares } from '@swarmion-starter/serverless-helpers';
3+
4+
export const handler = createPostContract.handler(async event => {
5+
const { threadId } = event.pathParameters;
6+
const { content } = event.body;
7+
8+
await Promise.resolve({ threadId });
9+
10+
return {
11+
id: 'myFirstPost',
12+
createdAt: '2021-10-25T12:12:00Z',
13+
editedAt: null,
14+
content,
15+
authorId: 'author2',
16+
};
17+
});
18+
19+
export const main = applyHttpMiddlewares(handler, {
20+
inputSchema: createPostContract.inputSchema,
21+
});

backend/forum/functions/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import createPost from './createPost/config';
12
import getThreadAndPosts from './getThreadAndPosts/config';
23

3-
export const functions = { getThreadAndPosts };
4+
export const functions = { getThreadAndPosts, createPost };
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ApiGatewayContract } from '@swarmion/serverless-contracts';
2+
3+
import { postEntitySchema } from 'contracts/entities';
4+
5+
const pathParametersSchema = {
6+
type: 'object',
7+
properties: {
8+
threadId: { type: 'string' },
9+
},
10+
required: ['threadId'],
11+
additionalProperties: false,
12+
} as const;
13+
14+
const bodySchema = {
15+
type: 'object',
16+
properties: {
17+
content: { type: 'string' },
18+
},
19+
required: ['content'],
20+
additionalProperties: false,
21+
} as const;
22+
23+
export const createPostContract = new ApiGatewayContract({
24+
id: 'forum-createPost',
25+
path: '/forum/thread/{threadId}',
26+
method: 'POST',
27+
integrationType: 'httpApi',
28+
pathParametersSchema,
29+
queryStringParametersSchema: undefined,
30+
bodySchema,
31+
headersSchema: undefined,
32+
outputSchema: postEntitySchema,
33+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './getThreadWithPosts';
2+
export * from './createPost';

0 commit comments

Comments
 (0)