mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 18:44:54 +00:00
add subspace controller
This commit is contained in:
@ -1,2 +1,3 @@
|
||||
export * from './space.controller';
|
||||
export * from './space-user.controller';
|
||||
export * from './subspace.controller';
|
||||
|
||||
91
src/space/controllers/subspace.controller.ts
Normal file
91
src/space/controllers/subspace.controller.ts
Normal file
@ -0,0 +1,91 @@
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { SubSpaceService } from '../services';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { AddSubspaceDto, GetSpaceParam, GetSubSpaceParam } from '../dtos';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
|
||||
|
||||
@ApiTags('Space Module')
|
||||
@Controller({
|
||||
version: '1',
|
||||
path: ControllerRoute.SUBSPACE.ROUTE,
|
||||
})
|
||||
export class SubSpaceController {
|
||||
constructor(private readonly subSpaceService: SubSpaceService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.SUBSPACE.ACTIONS.CREATE_SUBSPACE_SUMMARY,
|
||||
description: ControllerRoute.SUBSPACE.ACTIONS.CREATE_SUBSPACE_DESCRIPTION,
|
||||
})
|
||||
async createSubspace(
|
||||
@Param() params: GetSpaceParam,
|
||||
@Body() addSubspaceDto: AddSubspaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return this.subSpaceService.createSubspace(addSubspaceDto, params);
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.SUBSPACE.ACTIONS.LIST_SUBSPACES_SUMMARY,
|
||||
description: ControllerRoute.SUBSPACE.ACTIONS.LIST_SUBSPACES_DESCRIPTION,
|
||||
})
|
||||
@Get()
|
||||
async list(
|
||||
@Param() params: GetSpaceParam,
|
||||
@Query() query: PaginationRequestGetListDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return this.subSpaceService.list(params, query);
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.SUBSPACE.ACTIONS.GET_SUBSPACE_SUMMARY,
|
||||
description: ControllerRoute.SUBSPACE.ACTIONS.GET_SUBSPACE_DESCRIPTION,
|
||||
})
|
||||
@Get(':subSpaceUuid')
|
||||
async findOne(@Param() params: GetSubSpaceParam): Promise<BaseResponseDto> {
|
||||
return this.subSpaceService.findOne(params);
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.SUBSPACE.ACTIONS.UPDATE_SUBSPACE_SUMMARY,
|
||||
description: ControllerRoute.SUBSPACE.ACTIONS.UPDATE_SUBSPACE_DESCRIPTION,
|
||||
})
|
||||
@Put(':subSpaceUuid')
|
||||
async updateSubspace(
|
||||
@Param() params: GetSubSpaceParam,
|
||||
@Body() updateSubSpaceDto: AddSubspaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return this.subSpaceService.updateSubSpace(params, updateSubSpaceDto);
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.SUBSPACE.ACTIONS.DELETE_SUBSPACE_SUMMARY,
|
||||
description: ControllerRoute.SUBSPACE.ACTIONS.DELETE_SUBSPACE_DESCRIPTION,
|
||||
})
|
||||
@Delete(':subSpaceUuid')
|
||||
async delete(@Param() params: GetSubSpaceParam): Promise<BaseResponseDto> {
|
||||
return this.subSpaceService.delete(params);
|
||||
}
|
||||
}
|
||||
@ -2,3 +2,4 @@ export * from './add.space.dto';
|
||||
export * from './community-space.param';
|
||||
export * from './get.space.param';
|
||||
export * from './user-space.param';
|
||||
export * from './subspace';
|
||||
|
||||
12
src/space/dtos/subspace/add.subspace.dto.ts
Normal file
12
src/space/dtos/subspace/add.subspace.dto.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class AddSubspaceDto {
|
||||
@ApiProperty({
|
||||
example: 'Meeting Room 1',
|
||||
description: 'Name of the subspace',
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
subspaceName: string;
|
||||
}
|
||||
12
src/space/dtos/subspace/get.subspace.param.ts
Normal file
12
src/space/dtos/subspace/get.subspace.param.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsUUID } from 'class-validator';
|
||||
import { GetSpaceParam } from '../get.space.param';
|
||||
|
||||
export class GetSubSpaceParam extends GetSpaceParam {
|
||||
@ApiProperty({
|
||||
description: 'UUID of the sub space',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
})
|
||||
@IsUUID()
|
||||
subSpaceUuid: string;
|
||||
}
|
||||
2
src/space/dtos/subspace/index.ts
Normal file
2
src/space/dtos/subspace/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './add.subspace.dto';
|
||||
export * from './get.subspace.param';
|
||||
@ -1,2 +1,3 @@
|
||||
export * from './space.service';
|
||||
export * from './space-user.service';
|
||||
export * from './subspace.service';
|
||||
|
||||
203
src/space/services/subspace.service.ts
Normal file
203
src/space/services/subspace.service.ts
Normal file
@ -0,0 +1,203 @@
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import {
|
||||
SpaceRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { AddSubspaceDto, GetSpaceParam, GetSubSpaceParam } from '../dtos';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import {
|
||||
TypeORMCustomModel,
|
||||
TypeORMCustomModelFindAllQuery,
|
||||
} from '@app/common/models/typeOrmCustom.model';
|
||||
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
||||
import { SubspaceDto } from '@app/common/modules/space/dtos';
|
||||
|
||||
@Injectable()
|
||||
export class SubSpaceService {
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly communityRepository: CommunityRepository,
|
||||
private readonly subspaceRepository: SubspaceRepository,
|
||||
) {}
|
||||
|
||||
async createSubspace(
|
||||
addSubspaceDto: AddSubspaceDto,
|
||||
params: GetSpaceParam,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { communityUuid, spaceUuid } = params;
|
||||
const space = await this.validateCommunityAndSpace(
|
||||
communityUuid,
|
||||
spaceUuid,
|
||||
);
|
||||
|
||||
try {
|
||||
const newSubspace = this.subspaceRepository.create({
|
||||
...addSubspaceDto,
|
||||
space,
|
||||
});
|
||||
|
||||
await this.subspaceRepository.save(newSubspace);
|
||||
|
||||
return new SuccessResponseDto({
|
||||
statusCode: HttpStatus.CREATED,
|
||||
data: newSubspace,
|
||||
message: 'Subspace created successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
async list(
|
||||
params: GetSpaceParam,
|
||||
pageable: Partial<TypeORMCustomModelFindAllQuery>,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { communityUuid, spaceUuid } = params;
|
||||
await this.validateCommunityAndSpace(communityUuid, spaceUuid);
|
||||
|
||||
try {
|
||||
pageable.modelName = 'subspace';
|
||||
pageable.where = { space: { uuid: spaceUuid } };
|
||||
const customModel = TypeORMCustomModel(this.subspaceRepository);
|
||||
|
||||
const { baseResponseDto, paginationResponseDto } =
|
||||
await customModel.findAll(pageable);
|
||||
return new PageResponse<SubspaceDto>(
|
||||
baseResponseDto,
|
||||
paginationResponseDto,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
async findOne(params: GetSubSpaceParam): Promise<BaseResponseDto> {
|
||||
const { communityUuid, subSpaceUuid, spaceUuid } = params;
|
||||
await this.validateCommunityAndSpace(communityUuid, spaceUuid);
|
||||
try {
|
||||
const subSpace = await this.subspaceRepository.findOne({
|
||||
where: {
|
||||
uuid: subSpaceUuid,
|
||||
},
|
||||
});
|
||||
|
||||
// If space is not found, throw a NotFoundException
|
||||
if (!subSpace) {
|
||||
throw new HttpException(
|
||||
`Sub Space with UUID ${subSpaceUuid} not found`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
return new SuccessResponseDto({
|
||||
message: `Subspace with ID ${subSpaceUuid} successfully fetched`,
|
||||
data: subSpace,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error; // If it's an HttpException, rethrow it
|
||||
} else {
|
||||
throw new HttpException(
|
||||
'An error occurred while deleting the subspace',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async updateSubSpace(
|
||||
params: GetSubSpaceParam,
|
||||
updateSubSpaceDto: AddSubspaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { spaceUuid, communityUuid, subSpaceUuid } = params;
|
||||
await this.validateCommunityAndSpace(communityUuid, spaceUuid);
|
||||
|
||||
const subSpace = await this.subspaceRepository.findOne({
|
||||
where: { uuid: subSpaceUuid },
|
||||
});
|
||||
|
||||
if (!subSpace) {
|
||||
throw new HttpException(
|
||||
`SubSpace with ID ${subSpaceUuid} not found`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
Object.assign(subSpace, updateSubSpaceDto);
|
||||
|
||||
await this.subspaceRepository.save(subSpace);
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: `Subspace with ID ${subSpaceUuid} successfully updated`,
|
||||
data: subSpace,
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
throw new HttpException(
|
||||
'An error occurred while updating the subspace',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async delete(params: GetSubSpaceParam): Promise<BaseResponseDto> {
|
||||
const { spaceUuid, communityUuid, subSpaceUuid } = params;
|
||||
await this.validateCommunityAndSpace(communityUuid, spaceUuid);
|
||||
|
||||
const subspace = await this.subspaceRepository.findOne({
|
||||
where: { uuid: subSpaceUuid },
|
||||
});
|
||||
|
||||
if (!subspace) {
|
||||
throw new HttpException(
|
||||
`Subspace with ID ${subSpaceUuid} not found`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
await this.subspaceRepository.remove(subspace);
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: `Subspace with ID ${subSpaceUuid} successfully deleted`,
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'An error occurred while deleting the subspace',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async validateCommunityAndSpace(
|
||||
communityUuid: string,
|
||||
spaceUuid: string,
|
||||
) {
|
||||
const community = await this.communityRepository.findOne({
|
||||
where: { uuid: communityUuid },
|
||||
});
|
||||
if (!community) {
|
||||
throw new HttpException(
|
||||
`Community with ID ${communityUuid} not found`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const space = await this.spaceRepository.findOne({
|
||||
where: { uuid: spaceUuid, community: { uuid: communityUuid } },
|
||||
});
|
||||
if (!space) {
|
||||
throw new HttpException(
|
||||
`Space with ID ${spaceUuid} not found`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
return space;
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,17 @@
|
||||
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { SpaceController } from './controllers';
|
||||
import { SpaceService, SpaceUserService } from './services';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import {
|
||||
SpaceController,
|
||||
SpaceUserController,
|
||||
SubSpaceController,
|
||||
} from './controllers';
|
||||
import { SpaceService, SpaceUserService, SubSpaceService } from './services';
|
||||
import {
|
||||
SpaceRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { SpaceUserController } from './controllers/space-user.controller';
|
||||
import {
|
||||
UserRepository,
|
||||
UserSpaceRepository,
|
||||
@ -13,11 +19,13 @@ import {
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, SpaceRepositoryModule],
|
||||
controllers: [SpaceController, SpaceUserController],
|
||||
controllers: [SpaceController, SpaceUserController, SubSpaceController],
|
||||
providers: [
|
||||
SpaceService,
|
||||
SubSpaceService,
|
||||
SpaceRepository,
|
||||
CommunityRepository,
|
||||
SubspaceRepository,
|
||||
UserSpaceRepository,
|
||||
UserRepository,
|
||||
SpaceUserService,
|
||||
|
||||
Reference in New Issue
Block a user