diff --git a/backend/forum/functions/createPost/config.ts b/backend/forum/functions/createPost/config.ts new file mode 100644 index 00000000..07a37fce --- /dev/null +++ b/backend/forum/functions/createPost/config.ts @@ -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; diff --git a/backend/forum/functions/createPost/handler.mock.json b/backend/forum/functions/createPost/handler.mock.json new file mode 100644 index 00000000..049ce9a2 --- /dev/null +++ b/backend/forum/functions/createPost/handler.mock.json @@ -0,0 +1,4 @@ +{ + "pathParameters": { "threadId": "fooBar" }, + "body": { "content": "Hello, I am Swarmion!" } +} diff --git a/backend/forum/functions/createPost/handler.test.ts b/backend/forum/functions/createPost/handler.test.ts new file mode 100644 index 00000000..5bd48337 --- /dev/null +++ b/backend/forum/functions/createPost/handler.test.ts @@ -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); + }); +}); diff --git a/backend/forum/functions/createPost/handler.ts b/backend/forum/functions/createPost/handler.ts new file mode 100644 index 00000000..0c688231 --- /dev/null +++ b/backend/forum/functions/createPost/handler.ts @@ -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, +}); diff --git a/backend/forum/functions/index.ts b/backend/forum/functions/index.ts index 00188fca..f9b2e55a 100644 --- a/backend/forum/functions/index.ts +++ b/backend/forum/functions/index.ts @@ -1,3 +1,4 @@ +import createPost from './createPost/config'; import getThreadAndPosts from './getThreadAndPosts/config'; -export const functions = { getThreadAndPosts }; +export const functions = { getThreadAndPosts, createPost }; diff --git a/contracts/forum-contracts/src/contracts/requests/createPost.ts b/contracts/forum-contracts/src/contracts/requests/createPost.ts new file mode 100644 index 00000000..03645b05 --- /dev/null +++ b/contracts/forum-contracts/src/contracts/requests/createPost.ts @@ -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, +}); diff --git a/contracts/forum-contracts/src/contracts/requests/index.ts b/contracts/forum-contracts/src/contracts/requests/index.ts index 60645ec6..4640663c 100644 --- a/contracts/forum-contracts/src/contracts/requests/index.ts +++ b/contracts/forum-contracts/src/contracts/requests/index.ts @@ -1 +1,2 @@ export * from './getThreadWithPosts'; +export * from './createPost';