Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: user profile api2 #379

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions lib/configs/api-gateway/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,98 @@ export const forumCommentPatchReqSchema: apigw.JsonSchema = {
required: ['data'],
};

export const userProfileGetRespSchema: apigw.JsonSchema = {
schema: apigw.JsonSchemaVersion.DRAFT7,
type: apigw.JsonSchemaType.OBJECT,
properties: {
data: {
type: apigw.JsonSchemaType.OBJECT,
properties: {
uid: {
type: apigw.JsonSchemaType.STRING,
},
name: {
type: apigw.JsonSchemaType.STRING,
},
email: {
type: apigw.JsonSchemaType.STRING,
},
year: {
type: apigw.JsonSchemaType.STRING,
},
class_of: {
type: apigw.JsonSchemaType.STRING,
},
language: {
type: apigw.JsonSchemaType.STRING,
},
interest: {
type: apigw.JsonSchemaType.STRING,
},
school: {
type: apigw.JsonSchemaType.STRING,
},
created_at: {
type: apigw.JsonSchemaType.STRING,
},
updated_at: {
type: apigw.JsonSchemaType.STRING,
},
},
required: [
'uid',
'name',
'email',
'year',
'class_of',
'language',
'interests',
'school',
'create_at',
'updated_at',
],
},
message: {
type: apigw.JsonSchemaType.STRING,
},
},
required: ['success', 'data', 'message'],
};

export const userProfilePostReqSchema: apigw.JsonSchema = {
schema: apigw.JsonSchemaVersion.DRAFT7,
type: apigw.JsonSchemaType.OBJECT,
properties: {
data: {
type: apigw.JsonSchemaType.OBJECT,
properties: {
body: {
type: apigw.JsonSchemaType.STRING,
},
},
required: ['body'],
},
},
required: ['data'],
};

export const userProfilePatchReqSchema: apigw.JsonSchema = {
schema: apigw.JsonSchemaVersion.DRAFT7,
type: apigw.JsonSchemaType.OBJECT,
properties: {
data: {
type: apigw.JsonSchemaType.OBJECT,
properties: {
body: {
type: apigw.JsonSchemaType.STRING,
},
},
required: ['body'],
},
},
required: ['data'],
};

export const baseJsonApiSchema: apigw.JsonSchema = {
schema: apigw.JsonSchemaVersion.DRAFT7,
type: apigw.JsonSchemaType.OBJECT,
Expand Down
3 changes: 2 additions & 1 deletion lib/configs/common/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const enum DataEndpoint {
COURSE,
THREAD,
COMMENT,
ADS, //! New ADS value
ADS,
PROFILE, //! New ADS value
}

export enum OperationEndpoint {
Expand Down
167 changes: 166 additions & 1 deletion lib/constructs/business/rest-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import {
forumCommentPostReqSchema,
forumCommentPatchReqSchema,
syllabusSchema,
userProfileGetRespSchema,
userProfilePostReqSchema,
userProfilePatchReqSchema,
} from '../../configs/api-gateway/schema';
import { AwsServicePrincipal } from '../../configs/common/aws';
import {
Expand All @@ -30,6 +33,7 @@ import {
ForumCommentFunctions,
AdsImageProcessFunctionsAPI,
CareerRestFunctions,
ProfileProcessFunctions,
} from '../common/lambda-functions';
import { AbstractRestApiEndpoint } from './api-endpoint';

Expand All @@ -53,7 +57,6 @@ export class RestApiService extends Construct {
}
}

//! New code for adsImgs
export class ForumAdsApiService extends RestApiService {
readonly resourceMapping: {
[path: string]: { [method in apigw2.HttpMethod]?: apigw.Method };
Expand Down Expand Up @@ -1190,3 +1193,165 @@ export class ForumCommentsApiService extends RestApiService {
};
}
}

export class ProfileProcessApiService extends RestApiService {
readonly resourceMapping: {
[path: string]: { [method in apigw2.HttpMethod]?: apigw.Method };
};

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

const root = scope.apiEndpoint.root.addResource('profile');

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

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

const getIntegration = new apigw.LambdaIntegration(
profileProcessFunctions.getFunction,
{ proxy: true },
);
const postIntegration = new apigw.LambdaIntegration(
profileProcessFunctions.postFunction,
{ proxy: true },
);
const patchIntegration = new apigw.LambdaIntegration(
profileProcessFunctions.patchFunction,
{ proxy: true },
);
const deleteIntegration = new apigw.LambdaIntegration(
profileProcessFunctions.deleteFunction,
{ proxy: true },
);
// bug fixed
const getRespModel = scope.apiEndpoint.addModel('profile-get-resp-model', {
schema: userProfileGetRespSchema,
contentType: 'application/json',
description:
'HTTP GET response body schema for fetching user profile.',
modelName: 'GetCommentsResp',
});
const postReqModel = scope.apiEndpoint.addModel('profile-post-req-model', {
schema: userProfilePostReqSchema,
contentType: 'application/json',
description:
'HTTP POST request body schema for submitting a user profile.',
modelName: 'PostCommentReq',
});
const patchReqModel = scope.apiEndpoint.addModel(
'profile-patch-req-model',
{
schema: userProfilePatchReqSchema,
contentType: 'application/json',
description:
'HTTP PATCH request body schema for updating a user profile',
modelName: 'PatchCommentReq',
},
);

const getUserProfile = root.addMethod(
apigw2.HttpMethod.GET,
getIntegration,
{
requestParameters: {
'method.request.querystring.uid': false,
},
operationName: 'GetUserProfile',
methodResponses: [
{
statusCode: '200',
responseModels: { ['application/json']: getRespModel },
responseParameters: lambdaRespParams,
},
],
requestValidator: props.validator,
},
);
const postUserProfile = root.addMethod(
apigw2.HttpMethod.POST,
postIntegration,
{
operationName: 'PostUserProfile',
requestModels: { ['application/json']: postReqModel },
methodResponses: [
{
statusCode: '200',
responseParameters: lambdaRespParams,
},
],
authorizer: props.authorizer,
requestValidator: props.validator,
},
);
const patchUserProfile = root.addMethod(
apigw2.HttpMethod.PATCH,
patchIntegration,
{
operationName: 'UpdateUseProfile',
requestParameters: {
'method.request.querystring.ts': true,
},
requestModels: { ['application/json']: patchReqModel },
methodResponses: [
{
statusCode: '200',
responseParameters: lambdaRespParams,
},
],
authorizer: props.authorizer,
requestValidator: props.validator,
},
);
const deleteUserProfile = root.addMethod(
apigw2.HttpMethod.DELETE,
deleteIntegration,
{
operationName: 'DeleteUserProfile',
requestParameters: {
'method.request.querystring.ts': true,
},
methodResponses: [
{
statusCode: '200',
responseParameters: lambdaRespParams,
},
],
authorizer: props.authorizer,
requestValidator: props.validator,
},
);

this.resourceMapping = {
'/profile': {
[apigw2.HttpMethod.GET]: getUserProfile,
[apigw2.HttpMethod.OPTIONS]: optionsProfileProcess,
[apigw2.HttpMethod.PATCH]: patchUserProfile,
[apigw2.HttpMethod.POST]: postUserProfile,
[apigw2.HttpMethod.DELETE]: deleteUserProfile,
},
};
}
}
6 changes: 4 additions & 2 deletions lib/constructs/business/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export type RestApiServiceId =
| 'thread'
| 'comment'
| 'ads'
| 'graphql';
| 'graphql'
| 'profile';

export const restApiServiceMap: {
[name in RestApiServiceId]: typeof rest.RestApiService;
Expand All @@ -26,8 +27,9 @@ export const restApiServiceMap: {
'timetable': rest.TimetableApiService,
'thread': rest.ForumThreadsApiService,
'comment': rest.ForumCommentsApiService,
'ads': rest.ForumAdsApiService, //TODO Add service in construct/business/restapi
'ads': rest.ForumAdsApiService,
'graphql': rest.GraphqlApiService,
'profile': rest.ProfileProcessApiService,
};

export type GraphqlApiServiceId = 'course';
Expand Down
Loading
Loading