add subspace controller

This commit is contained in:
hannathkadher
2024-10-28 09:53:47 +04:00
parent 6d6560b3e0
commit 74428b408e
15 changed files with 401 additions and 18 deletions
+1
View File
@@ -1,2 +1,3 @@
export * from './space.service';
export * from './space-user.service';
export * from './subspace.service';
+203
View 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;
}
}