made first lambda function and deployed it successfully
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
// src/modules/host/handler/host.handler.ts
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from 'src/app.module';
|
||||
import { HostService } from '../services/host.service';
|
||||
import { APIGatewayProxyHandler } from 'aws-lambda';
|
||||
import { CreateHostDto, UpdateHostDto } from '../dto/host.dto';
|
||||
|
||||
let app;
|
||||
let hostService: HostService;
|
||||
|
||||
async function bootstrap() {
|
||||
if (!app) {
|
||||
const nestApp = await NestFactory.createApplicationContext(AppModule);
|
||||
hostService = nestApp.get(HostService);
|
||||
app = nestApp;
|
||||
}
|
||||
}
|
||||
|
||||
export const handler: APIGatewayProxyHandler = async (event) => {
|
||||
await bootstrap();
|
||||
|
||||
const method = event.httpMethod;
|
||||
const path = event.path;
|
||||
const body = event.body ? JSON.parse(event.body) : {};
|
||||
|
||||
try {
|
||||
if (method === 'POST' && path.endsWith('/host')) {
|
||||
const host = await hostService.createHost(body as CreateHostDto);
|
||||
return response(201, host);
|
||||
}
|
||||
|
||||
if (method === 'GET' && path.endsWith('/host')) {
|
||||
const hosts = await hostService.getAllHosts();
|
||||
return response(200, hosts);
|
||||
}
|
||||
|
||||
if (method === 'GET' && path.match(/\/host\/\d+$/)) {
|
||||
const id = Number(path.split('/').pop());
|
||||
const host = await hostService.getHostById(id);
|
||||
return response(200, host);
|
||||
}
|
||||
|
||||
if (method === 'PUT' && path.match(/\/host\/\d+$/)) {
|
||||
const id = Number(path.split('/').pop());
|
||||
const host = await hostService.updateHost(id, body as UpdateHostDto);
|
||||
return response(200, host);
|
||||
}
|
||||
|
||||
if (method === 'DELETE' && path.match(/\/host\/\d+$/)) {
|
||||
const id = Number(path.split('/').pop());
|
||||
const host = await hostService.deleteHost(id);
|
||||
return response(200, { message: 'Host deleted successfully', host });
|
||||
}
|
||||
|
||||
return response(404, { message: 'Not Found' });
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
return response(500, { message: error.message || 'Internal Server Error' });
|
||||
}
|
||||
};
|
||||
|
||||
function response(statusCode: number, body: any) {
|
||||
return {
|
||||
statusCode,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
};
|
||||
}
|
||||
28
src/modules/host/handlers/host.ts
Normal file
28
src/modules/host/handlers/host.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export const handler = safeHandler(async (
|
||||
event: APIGatewayProxyEvent
|
||||
): Promise<APIGatewayProxyResult> => {
|
||||
|
||||
const result = await prisma.hostHeader.findMany({
|
||||
select: {
|
||||
hostParent: true,
|
||||
hostRefNumber: true,
|
||||
hostStatusDisplay: true,
|
||||
accountManager: true,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
body: JSON.stringify(result),
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user