Skip to content

Commit

Permalink
feat: test code forum ads api (#340)
Browse files Browse the repository at this point in the history
* feat: test code for forum ads api

* feat: test code for lambda function syncadsimgs

* feat: new test code for lambda function syncadsimgs

* feat: new test code for lambda function syncadsimgs

* feat: test code for lambda function get-omgs-list

* feat: test code for lambda function get-omgs-list 2

* feat: test code for lambda get imgs list

* feat: test code for lambda get imgs list 2
  • Loading branch information
LIEN-YUHSIANG authored Oct 9, 2023
1 parent 30e164c commit 9fd3e4e
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 65 deletions.
50 changes: 25 additions & 25 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@ env:
node: true
root: true
plugins:
- "@typescript-eslint"
- '@typescript-eslint'
- import
parser: "@typescript-eslint/parser"
parser: '@typescript-eslint/parser'
parserOptions:
ecmaVersion: 2018
sourceType: module
project: "./tsconfig.json"
project: './tsconfig.json'
extends:
- "eslint:recommended"
- "plugin:@typescript-eslint/eslint-recommended"
- "plugin:@typescript-eslint/recommended"
- "plugin:import/typescript"
- 'eslint:recommended'
- 'plugin:@typescript-eslint/eslint-recommended'
- 'plugin:@typescript-eslint/recommended'
- 'plugin:import/typescript'
settings:
import/parsers:
"@typescript-eslint/parser":
- ".ts"
- ".tsx"
'@typescript-eslint/parser':
- '.ts'
- '.tsx'
import/resolver:
node: { }
node: {}
typescript:
project: "./tsconfig.json"
project: './tsconfig.json'
alwaysTryTypes: true
ignorePatterns:
- "*.js"
- "!.projenrc.js"
- "*.d.ts"
- '*.js'
- '!.projenrc.js'
- '*.d.ts'
- node_modules/
- "*.generated.ts"
- '*.generated.ts'
- coverage
rules:
indent:
- 'off'
"@typescript-eslint/indent":
'@typescript-eslint/indent':
- error
- 2
quotes:
Expand Down Expand Up @@ -81,7 +81,7 @@ rules:
- error
- multi-line
- consistent
"@typescript-eslint/member-delimiter-style":
'@typescript-eslint/member-delimiter-style':
- error
semi:
- error
Expand All @@ -97,13 +97,13 @@ rules:
quote-props:
- error
- consistent-as-needed
"@typescript-eslint/no-require-imports":
'@typescript-eslint/no-require-imports':
- error
import/no-extraneous-dependencies:
- error
- devDependencies:
- "**/test/**"
- "**/build-tools/**"
- '**/test/**'
- '**/build-tools/**'
optionalDependencies: false
peerDependencies: true
import/no-unresolved:
Expand All @@ -120,25 +120,25 @@ rules:
- error
no-shadow:
- 'off'
"@typescript-eslint/no-shadow":
'@typescript-eslint/no-shadow':
- error
key-spacing:
- error
no-multiple-empty-lines:
- error
"@typescript-eslint/no-floating-promises":
'@typescript-eslint/no-floating-promises':
- error
no-return-await:
- 'off'
"@typescript-eslint/return-await":
'@typescript-eslint/return-await':
- error
no-trailing-spaces:
- error
dot-notation:
- error
no-bitwise:
- error
"@typescript-eslint/member-ordering":
'@typescript-eslint/member-ordering':
- error
- default:
- public-static-field
Expand Down
57 changes: 57 additions & 0 deletions lib/constructs/business/rest-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
TimetableFunctions,
ForumThreadFunctions,
ForumCommentFunctions,
AdsImageProcessFunctions,
} from '../common/lambda-functions';
import { AbstractRestApiEndpoint } from './api-endpoint';

Expand All @@ -51,6 +52,62 @@ export class RestApiService extends Construct {
}
}

//! New code for adsImgs
export class ForumAdsApiService extends RestApiService {
readonly resourceMapping: {
[path: string]: { [method in apigw2.HttpMethod]?: apigw.Method };
};

constructor(
scope: AbstractRestApiEndpoint,
id: string,
props: RestApiServiceProps,
) {
super(scope, id, props);

// Create resources for the api
const root = scope.apiEndpoint.root.addResource('adsImgs');

const adsImageProcessFunctions = new AdsImageProcessFunctions(
this,
'crud-functions',
{
envVars: {
TABLE_NAME: props.dataSource!,
},
},
);

const getIntegration = new apigw.LambdaIntegration(
adsImageProcessFunctions.getFunction,
{ proxy: true },
);

const optionsAdsImgs = root.addCorsPreflight({
allowOrigins: allowOrigins,
allowHeaders: allowHeaders,
allowMethods: [apigw2.HttpMethod.GET, apigw2.HttpMethod.POST],
});

const getImgsList = root.addMethod(apigw2.HttpMethod.GET, getIntegration, {
operationName: 'GetImgsList',
methodResponses: [
{
statusCode: '200',
responseParameters: lambdaRespParams,
},
],
requestValidator: props.validator,
});

this.resourceMapping = {
'/adsImgs': {
[apigw2.HttpMethod.GET]: getImgsList,
[apigw2.HttpMethod.OPTIONS]: optionsAdsImgs,
},
};
}
}
export class SyllabusApiService extends RestApiService {
readonly resourceMapping: {
[path: string]: { [method in apigw2.HttpMethod]?: apigw.Method };
Expand Down
2 changes: 2 additions & 0 deletions lib/constructs/business/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type RestApiServiceId =
| 'timetable'
| 'thread'
| 'comment'
| 'ads'
| 'graphql';

export const restApiServiceMap: {
Expand All @@ -25,6 +26,7 @@ export const restApiServiceMap: {
'timetable': rest.TimetableApiService,
'thread': rest.ForumThreadsApiService,
'comment': rest.ForumCommentsApiService,
'ads': rest.ForumAdsApiService, //TODO Add service in construct/business/restapi
'graphql': rest.GraphqlApiService,
};

Expand Down
50 changes: 25 additions & 25 deletions lib/constructs/common/lambda-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,18 +877,18 @@ export class ThreadImageProcessFunctions extends Construct {
},
);

// this.syncImageFunction = new lambda_py.PythonFunction(this, 'sync-image', {
// entry: 'src/lambda/sync-image',
// description:
// 'post image to dyanamo db database when image inputed in s3 bucket',
// functionName: 'sync-image',
// logRetention: logs.RetentionDays.ONE_MONTH,
// memorySize: 256,
// role: DBSyncRole,
// runtime: lambda.Runtime.PYTHON_3_9,
// timeout: Duration.seconds(5),
// environment: props.envVars,
// });
this.syncImageFunction = new lambda_py.PythonFunction(this, 'sync-image', {
entry: 'src/lambda/sync-image',
description:
'post image to dyanamo db database when image inputed in s3 bucket',
functionName: 'sync-image',
logRetention: logs.RetentionDays.ONE_MONTH,
memorySize: 256,
role: DBSyncRole,
runtime: lambda.Runtime.PYTHON_3_9,
timeout: Duration.seconds(5),
environment: props.envVars,
});

this.resizeImageFunction = new lambda_py.PythonFunction(
this,
Expand Down Expand Up @@ -934,7 +934,7 @@ export class ThreadImageProcessFunctions extends Construct {
}

export class AdsImageProcessFunctions extends Construct {
// readonly getFunction: lambda.Function;
readonly getFunction: lambda.Function;
readonly syncImageFunction: lambda.Function;
readonly resizeImageFunction: lambda.Function;
// readonly deleteFunction: lambda.Function;
Expand Down Expand Up @@ -1013,6 +1013,18 @@ export class AdsImageProcessFunctions extends Construct {
environment: props.envVars,
});

this.getFunction = new lambda_py.PythonFunction(this, 'get-imgs-list', {
entry: 'src/lambda/get-imgs-list',
description: 'get imgs list from the database.',
functionName: 'get-imgs-list',
logRetention: logs.RetentionDays.ONE_MONTH,
memorySize: 128,
role: DBReadRole,
runtime: lambda.Runtime.PYTHON_3_9,
timeout: Duration.seconds(3),
environment: props.envVars,
});

// this.resizeImageFunction = new lambda_py.PythonFunction(
// this,
// "resize-image",
Expand All @@ -1030,18 +1042,6 @@ export class AdsImageProcessFunctions extends Construct {
// }
// );

// this.getFunction = new lambda_py.PythonFunction(this, "get-comment", {
// entry: "src/lambda/get-comments",
// description: "get forum comments from the database.",
// functionName: "get-forum-comments",
// logRetention: logs.RetentionDays.ONE_MONTH,
// memorySize: 128,
// role: DBReadRole,
// runtime: lambda.Runtime.PYTHON_3_9,
// timeout: Duration.seconds(3),
// environment: props.envVars,
// });

// this.deleteFunction = new lambda_py.PythonFunction(this, "delete-comment", {
// entry: "src/lambda/delete-comment",
// description: "Delete forum comment in the database.",
Expand Down
24 changes: 11 additions & 13 deletions lib/constructs/persistence/data-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,17 @@ export class AdsDataPipeline extends AbstractDataPipeline {

this.dataWarehouse = props.dataWarehouse!;

// this.processor = new AdsImageProcessFunctions(this, "image-process-func", {
// envVars: {
// ["BUCKET_NAME"]: this.dataSource.bucketName,
// ["TABLE_NAME"]: this.dataWarehouse.tableName,
// ["OBJECT_PATH"]: "syllabus/",
// },
// }).syncImageFunction;
this.processor = new AdsImageProcessFunctions(this, 'image-process-func', {
envVars: {
['BUCKET_NAME']: this.dataSource.bucketName,
['TABLE_NAME']: this.dataWarehouse.tableName,
},
}).syncImageFunction;

// this.processor.addEventSource(
// new event_sources.S3EventSource(this.dataSource, {
// events: [s3.EventType.OBJECT_CREATED_PUT],
// filters: [{ prefix: "syllabus/" }],
// })
// );
this.processor.addEventSource(
new event_sources.S3EventSource(this.dataSource, {
events: [s3.EventType.OBJECT_CREATED],
}),
);
}
}
5 changes: 5 additions & 0 deletions lib/stacks/business.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export class WasedaTimeBusinessLayer extends BusinessLayer {
'comment',
this.dataInterface.getEndpoint(DataEndpoint.COMMENT),
true,
)
.addService(
'ads',
this.dataInterface.getEndpoint(DataEndpoint.ADS),
true,
);
// .addService("graphql", graphqlApiEndpoint.apiEndpoint.graphqlUrl);
restApiEndpoint.deploy();
Expand Down
30 changes: 30 additions & 0 deletions src/lambda/get-imgs-list/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from boto3.dynamodb.conditions import Key
from utils import JsonPayloadBuilder
from utils import resp_handler
from utils import table


@resp_handler
def get_imgs_list(board_id):

if board_id:
response = table.query(KeyConditionExpression=Key(
"board_id").eq(board_id), ScanIndexForward=False)
else:
response = table.scan(ConsistentRead=False)

results = response.get('Items', [])

# response = table.scan()
# results = response.get('Items', [])

body = JsonPayloadBuilder().add_status(
True).add_data(results).add_message('').compile()
return body


def handler(event, context):

params = event["queryStringParameters"]
board_id = params.get("board_id", "")
return get_imgs_list(board_id)
Loading

0 comments on commit 9fd3e4e

Please sign in to comment.