mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
finshed get home by id api
This commit is contained in:
@ -1,5 +1,13 @@
|
|||||||
import { HomeService } from './../services/home.service';
|
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 { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||||
import { AddHomeDto } from '../dtos/add.home.dto';
|
import { AddHomeDto } from '../dtos/add.home.dto';
|
||||||
@ -14,14 +22,24 @@ export class HomeController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get(':userUuid')
|
@Get()
|
||||||
async userList(@Param('userUuid') userUuid: string) {
|
async getHomesByUserId(@Query('userUuid') userUuid: string) {
|
||||||
try {
|
try {
|
||||||
return await this.homeService.getHomesByUserId(userUuid);
|
return await this.homeService.getHomesByUserId(userUuid);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(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()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@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 { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { AddHomeDto } from '../dtos';
|
import { AddHomeDto } from '../dtos';
|
||||||
|
import { GetHomeDetailsInterface } from '../interfaces/get.home.interface';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HomeService {
|
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