first commit
This commit is contained in:
0
src/modules/auth/auth.module.ts
Normal file
0
src/modules/auth/auth.module.ts
Normal file
51
src/modules/host/dto/host.dto.ts
Normal file
51
src/modules/host/dto/host.dto.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
// src/modules/host/dto/host.dto.ts
|
||||
import { IsInt, IsOptional, IsString, IsBoolean, IsEmail } from 'class-validator';
|
||||
|
||||
export class CreateHostDto {
|
||||
@IsString()
|
||||
firstName: string;
|
||||
|
||||
@IsString()
|
||||
lastName: string;
|
||||
|
||||
@IsEmail()
|
||||
emailAddress: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
isdCode?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
mobileNumber?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
userPasscode?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
roleXid?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
isActive?: boolean;
|
||||
}
|
||||
|
||||
export class UpdateHostDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
firstName?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
lastName?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEmail()
|
||||
emailAddress?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
isActive?: boolean;
|
||||
}
|
||||
68
src/modules/host/handlers/host.handler.ts
Normal file
68
src/modules/host/handlers/host.handler.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
// 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),
|
||||
};
|
||||
}
|
||||
34
src/modules/host/services/host.service.ts
Normal file
34
src/modules/host/services/host.service.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
// src/modules/host/services/host.service.ts
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../../../common/database/prisma.service';
|
||||
import { CreateHostDto, UpdateHostDto } from '../dto/host.dto';
|
||||
|
||||
@Injectable()
|
||||
export class HostService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async createHost(data: CreateHostDto) {
|
||||
return this.prisma.user.create({ data });
|
||||
}
|
||||
|
||||
async getAllHosts() {
|
||||
return this.prisma.user.findMany({ where: { roleXid: 3 } });
|
||||
}
|
||||
|
||||
async getHostById(id: number) {
|
||||
const host = await this.prisma.user.findUnique({ where: { id } });
|
||||
if (!host || host.roleXid !== 3) throw new Error('Host not found');
|
||||
return host;
|
||||
}
|
||||
|
||||
async updateHost(id: number, data: UpdateHostDto) {
|
||||
return this.prisma.user.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
async deleteHost(id: number) {
|
||||
return this.prisma.user.delete({ where: { id } });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user