mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-25 13:37:55 +00:00
add subspace controller
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export * from './space.service';
|
||||
export * from './space-user.service';
|
||||
export * from './subspace.service';
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user