mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-24 13:27:54 +00:00
Refactor group controller methods and add device product guard
This commit is contained in:
@@ -5,17 +5,18 @@ import {
|
||||
Get,
|
||||
Post,
|
||||
UseGuards,
|
||||
Query,
|
||||
Param,
|
||||
Put,
|
||||
Delete,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddGroupDto } from '../dtos/add.group.dto';
|
||||
import { GetGroupDto } from '../dtos/get.group.dto';
|
||||
import { ControlGroupDto } from '../dtos/control.group.dto';
|
||||
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
|
||||
import { CheckProductUuidForAllDevicesGuard } from 'src/guards/device.product.guard';
|
||||
|
||||
@ApiTags('Group Module')
|
||||
@Controller({
|
||||
@@ -25,34 +26,43 @@ import { RenameGroupDto } from '../dtos/rename.group.dto copy';
|
||||
export class GroupController {
|
||||
constructor(private readonly groupService: GroupService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get()
|
||||
async getGroupsByHomeId(@Query() getGroupsDto: GetGroupDto) {
|
||||
// @ApiBearerAuth()
|
||||
// @UseGuards(JwtAuthGuard)
|
||||
@Get('space/:spaceUuid')
|
||||
async getGroupsBySpaceUuid(@Param('spaceUuid') spaceUuid: string) {
|
||||
try {
|
||||
return await this.groupService.getGroupsByHomeId(getGroupsDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
return await this.groupService.getGroupsBySpaceUuid(spaceUuid);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':groupId')
|
||||
async getGroupsByGroupId(@Param('groupId') groupId: number) {
|
||||
// @ApiBearerAuth()
|
||||
// @UseGuards(JwtAuthGuard)
|
||||
@Get(':groupUuid')
|
||||
async getGroupsByGroupId(@Param('groupUuid') groupUuid: string) {
|
||||
try {
|
||||
return await this.groupService.getGroupsByGroupId(groupId);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
return await this.groupService.getGroupsByGroupUuid(groupUuid);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
// @ApiBearerAuth()
|
||||
@UseGuards(CheckProductUuidForAllDevicesGuard)
|
||||
@Post()
|
||||
async addGroup(@Body() addGroupDto: AddGroupDto) {
|
||||
try {
|
||||
return await this.groupService.addGroup(addGroupDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,25 +77,37 @@ export class GroupController {
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put('rename')
|
||||
async renameGroup(@Body() renameGroupDto: RenameGroupDto) {
|
||||
// @ApiBearerAuth()
|
||||
// @UseGuards(JwtAuthGuard)
|
||||
@Put('rename/:groupUuid')
|
||||
async renameGroupByUuid(
|
||||
@Param('groupUuid') groupUuid: string,
|
||||
@Body() renameGroupDto: RenameGroupDto,
|
||||
) {
|
||||
try {
|
||||
return await this.groupService.renameGroup(renameGroupDto);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
return await this.groupService.renameGroupByUuid(
|
||||
groupUuid,
|
||||
renameGroupDto,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Delete(':groupId')
|
||||
async deleteGroup(@Param('groupId') groupId: number) {
|
||||
// @ApiBearerAuth()
|
||||
// @UseGuards(JwtAuthGuard)
|
||||
@Delete(':groupUuid')
|
||||
async deleteGroup(@Param('groupUuid') groupUuid: string) {
|
||||
try {
|
||||
return await this.groupService.deleteGroup(groupId);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
return await this.groupService.deleteGroup(groupUuid);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user