This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from swarmion/feat/create-post
feat(forum): add sample create post lambda
- Loading branch information
Showing
7 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"pathParameters": { "threadId": "fooBar" }, | ||
"body": { "content": "Hello, I am Swarmion!" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
contracts/forum-contracts/src/contracts/requests/createPost.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './getThreadWithPosts'; | ||
export * from './createPost'; |