finshed get home by id api

This commit is contained in:
faris Aljohari
2024-03-12 15:40:16 +03:00
parent dce87ac2b6
commit e011949bda
3 changed files with 59 additions and 3 deletions

View File

@ -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)

View File

@ -0,0 +1,6 @@
export class GetHomeDetailsInterface {
result: {
id: string;
name: string;
};
}

View File

@ -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,
);
}
}
}