mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 10:46:17 +00:00
create get and post home api
This commit is contained in:
@ -12,4 +12,8 @@ export class HomeDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public homeId: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public homeName: string;
|
||||
}
|
||||
|
@ -22,6 +22,11 @@ export class HomeEntity extends AbstractEntity<HomeDto> {
|
||||
})
|
||||
public homeId: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
public homeName: string;
|
||||
|
||||
constructor(partial: Partial<HomeEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
@ -4,6 +4,7 @@ import config from './config';
|
||||
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';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
@ -11,6 +12,7 @@ import { UserModule } from './users/user.module';
|
||||
}),
|
||||
AuthenticationModule,
|
||||
UserModule,
|
||||
HomeModule,
|
||||
],
|
||||
controllers: [AuthenticationController],
|
||||
})
|
||||
|
36
src/home/controllers/home.controller.ts
Normal file
36
src/home/controllers/home.controller.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { HomeService } from './../services/home.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 { AddHomeDto } from '../dtos/add.home.dto';
|
||||
|
||||
@ApiTags('Home Module')
|
||||
@Controller({
|
||||
version: '1',
|
||||
path: 'home',
|
||||
})
|
||||
export class HomeController {
|
||||
constructor(private readonly homeService: HomeService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':userUuid')
|
||||
async userList(@Param('userUuid') userUuid: string) {
|
||||
try {
|
||||
return await this.homeService.getHomesByUserId(userUuid);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
async addHome(@Body() addHomeDto: AddHomeDto) {
|
||||
try {
|
||||
return await this.homeService.addHome(addHomeDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
}
|
1
src/home/controllers/index.ts
Normal file
1
src/home/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './home.controller';
|
20
src/home/dtos/add.home.dto.ts
Normal file
20
src/home/dtos/add.home.dto.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class AddHomeDto {
|
||||
@ApiProperty({
|
||||
description: 'userUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public userUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'homeName',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public homeName: string;
|
||||
}
|
32
src/home/dtos/home.list.dto.ts
Normal file
32
src/home/dtos/home.list.dto.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import {
|
||||
IsNotEmpty,
|
||||
IsNumberString,
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from 'class-validator';
|
||||
|
||||
export class UserListDto {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
schema: string;
|
||||
|
||||
@IsNumberString()
|
||||
@IsNotEmpty()
|
||||
page_no: number;
|
||||
|
||||
@IsNumberString()
|
||||
@IsNotEmpty()
|
||||
page_size: number;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
username: string;
|
||||
|
||||
@IsNumberString()
|
||||
@IsOptional()
|
||||
start_time: number;
|
||||
|
||||
@IsNumberString()
|
||||
@IsOptional()
|
||||
end_time: number;
|
||||
}
|
2
src/home/dtos/index.ts
Normal file
2
src/home/dtos/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './home.list.dto';
|
||||
export * from './add.home.dto';
|
14
src/home/home.module.ts
Normal file
14
src/home/home.module.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HomeService } from './services/home.service';
|
||||
import { HomeController } from './controllers/home.controller';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { HomeRepositoryModule } from '@app/common/modules/home/home.repository.module';
|
||||
import { HomeRepository } from '@app/common/modules/home/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, HomeRepositoryModule],
|
||||
controllers: [HomeController],
|
||||
providers: [HomeService, HomeRepository],
|
||||
exports: [HomeService],
|
||||
})
|
||||
export class HomeModule {}
|
66
src/home/services/home.service.ts
Normal file
66
src/home/services/home.service.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import { HomeRepository } from './../../../libs/common/src/modules/home/repositories/home.repository';
|
||||
import { HomeEntity } from './../../../libs/common/src/modules/home/entities/home.entity';
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AddHomeDto } from '../dtos';
|
||||
|
||||
@Injectable()
|
||||
export class HomeService {
|
||||
private tuya: TuyaContext;
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly homeRepository: HomeRepository,
|
||||
) {
|
||||
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);
|
||||
|
||||
return homesData;
|
||||
}
|
||||
|
||||
async findHomes(userUuid: string) {
|
||||
return await this.homeRepository.find({
|
||||
where: {
|
||||
userUuid: userUuid,
|
||||
},
|
||||
});
|
||||
}
|
||||
async addHome(addHomeDto: AddHomeDto) {
|
||||
try {
|
||||
const path = `/v2.0/cloud/space/creation`;
|
||||
const data = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
body: { name: addHomeDto.homeName },
|
||||
});
|
||||
if (data.success) {
|
||||
const homeEntity = {
|
||||
userUuid: addHomeDto.userUuid,
|
||||
homeId: data.result,
|
||||
homeName: addHomeDto.homeName,
|
||||
} as HomeEntity;
|
||||
const savedHome = await this.homeRepository.save(homeEntity);
|
||||
return savedHome;
|
||||
}
|
||||
return {
|
||||
success: data.success,
|
||||
homeId: data.result,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error adding home',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
1
src/home/services/index.ts
Normal file
1
src/home/services/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './home.service';
|
Reference in New Issue
Block a user