mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
finshed get home by id api
This commit is contained in:
@ -1,5 +1,13 @@
|
||||
import { HomeService } from './../services/home.service';
|
||||
import { Body, Controller, Get, Post, Param, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Param,
|
||||
UseGuards,
|
||||
Query,
|
||||
} 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';
|
||||
@ -14,14 +22,24 @@ export class HomeController {
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':userUuid')
|
||||
async userList(@Param('userUuid') userUuid: string) {
|
||||
@Get()
|
||||
async getHomesByUserId(@Query('userUuid') userUuid: string) {
|
||||
try {
|
||||
return await this.homeService.getHomesByUserId(userUuid);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':homeId')
|
||||
async getHomesByHomeId(@Param('homeId') homeId: string) {
|
||||
try {
|
||||
return await this.homeService.getHomeByHomeId(homeId);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
|
6
src/home/interfaces/get.home.interface.ts
Normal file
6
src/home/interfaces/get.home.interface.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export class GetHomeDetailsInterface {
|
||||
result: {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
}
|
@ -4,6 +4,7 @@ import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AddHomeDto } from '../dtos';
|
||||
import { GetHomeDetailsInterface } from '../interfaces/get.home.interface';
|
||||
|
||||
@Injectable()
|
||||
export class HomeService {
|
||||
@ -78,4 +79,35 @@ export class HomeService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async getHomeDetails(homeId: string): Promise<GetHomeDetailsInterface> {
|
||||
try {
|
||||
const path = `/v2.0/cloud/space/${homeId}`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
});
|
||||
|
||||
return response as GetHomeDetailsInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching home details',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getHomeByHomeId(homeId: string) {
|
||||
try {
|
||||
const response = await this.getHomeDetails(homeId);
|
||||
|
||||
return {
|
||||
homeId: response.result.id,
|
||||
homeName: response.result.name,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching home',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user