mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
finished add room api
This commit is contained in:
@ -5,6 +5,7 @@ import { AuthenticationModule } from './auth/auth.module';
|
||||
import { AuthenticationController } from './auth/controllers/authentication.controller';
|
||||
import { UserModule } from './users/user.module';
|
||||
import { HomeModule } from './home/home.module';
|
||||
import { RoomModule } from './room/room.module';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
@ -13,6 +14,7 @@ import { HomeModule } from './home/home.module';
|
||||
AuthenticationModule,
|
||||
UserModule,
|
||||
HomeModule,
|
||||
RoomModule,
|
||||
],
|
||||
controllers: [AuthenticationController],
|
||||
})
|
||||
|
1
src/room/controllers/index.ts
Normal file
1
src/room/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './room.controller';
|
36
src/room/controllers/room.controller.ts
Normal file
36
src/room/controllers/room.controller.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { RoomService } from '../services/room.service';
|
||||
import { Body, Controller, Get, Post, Param, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddRoomDto } from '../dtos/add.room.dto';
|
||||
|
||||
@ApiTags('Room Module')
|
||||
@Controller({
|
||||
version: '1',
|
||||
path: 'room',
|
||||
})
|
||||
export class RoomController {
|
||||
constructor(private readonly roomService: RoomService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':userUuid')
|
||||
async userList(@Param('userUuid') userUuid: string) {
|
||||
try {
|
||||
return await this.roomService.getHomesByUserId(userUuid);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
// @ApiBearerAuth()
|
||||
// @UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
async addRoom(@Body() addRoomDto: AddRoomDto) {
|
||||
try {
|
||||
return await this.roomService.addRoom(addRoomDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
}
|
20
src/room/dtos/add.room.dto.ts
Normal file
20
src/room/dtos/add.room.dto.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString, IsNumberString } from 'class-validator';
|
||||
|
||||
export class AddRoomDto {
|
||||
@ApiProperty({
|
||||
description: 'roomName',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public roomName: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'homeId',
|
||||
required: true,
|
||||
})
|
||||
@IsNumberString()
|
||||
@IsNotEmpty()
|
||||
public homeId: string;
|
||||
}
|
1
src/room/dtos/index.ts
Normal file
1
src/room/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './add.room.dto';
|
11
src/room/room.module.ts
Normal file
11
src/room/room.module.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { RoomService } from './services/room.service';
|
||||
import { RoomController } from './controllers/room.controller';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
controllers: [RoomController],
|
||||
providers: [RoomService],
|
||||
exports: [RoomService],
|
||||
})
|
||||
export class RoomModule {}
|
1
src/room/services/index.ts
Normal file
1
src/room/services/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './room.service';
|
66
src/room/services/room.service.ts
Normal file
66
src/room/services/room.service.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AddRoomDto } from '../dtos';
|
||||
|
||||
@Injectable()
|
||||
export class RoomService {
|
||||
private tuya: TuyaContext;
|
||||
constructor(private readonly configService: ConfigService) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||
// const clientId = this.configService.get<string>('auth-config.CLIENT_ID');
|
||||
this.tuya = new TuyaContext({
|
||||
baseUrl: 'https://openapi.tuyaeu.com',
|
||||
accessKey,
|
||||
secretKey,
|
||||
});
|
||||
}
|
||||
|
||||
async getHomesByUserId(userUuid: string) {
|
||||
// const homesData = await this.findHomes(userUuid);
|
||||
|
||||
// const homesMapper = homesData.map((home) => ({
|
||||
// homeId: home.homeId,
|
||||
// homeName: home.homeName,
|
||||
// }));
|
||||
|
||||
// return homesMapper;
|
||||
console.log(userUuid);
|
||||
}
|
||||
|
||||
// async findHomes(userUuid: string) {
|
||||
// try {
|
||||
// return await this.homeRepository.find({
|
||||
// where: {
|
||||
// userUuid: userUuid,
|
||||
// },
|
||||
// });
|
||||
// } catch (error) {
|
||||
// throw new HttpException(
|
||||
// 'Error get homes',
|
||||
// HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
async addRoom(addRoomDto: AddRoomDto) {
|
||||
try {
|
||||
const path = `/v2.0/cloud/space/creation`;
|
||||
const data = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
body: { name: addRoomDto.roomName, parent_id: addRoomDto.homeId },
|
||||
});
|
||||
|
||||
return {
|
||||
success: data.success,
|
||||
roomId: data.result,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error adding room',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user