Space CRUD apis

This commit is contained in:
hannathkadher
2024-10-17 11:20:05 +04:00
parent d6777c8ec1
commit 96d52962aa
11 changed files with 479 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
export * from './space.service';
+271
View File
@@ -0,0 +1,271 @@
import { SpaceRepository } from '@app/common/modules/space/repositories';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { AddSpaceDto } from '../dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { SpaceEntity } from '@app/common/modules/space/entities';
import {
TypeORMCustomModel,
TypeORMCustomModelFindAllQuery,
} from '@app/common/models/typeOrmCustom.model';
import { SpaceDto } from '@app/common/modules/space/dtos';
import { PageResponse } from '@app/common/dto/pagination.response.dto';
@Injectable()
export class SpaceService {
constructor(
private readonly spaceRepository: SpaceRepository,
private readonly communityRepository: CommunityRepository,
) {}
async createSpace(
addSpaceDto: AddSpaceDto,
communityId: string,
): Promise<BaseResponseDto> {
let parent: SpaceEntity | null = null;
const { parentUuid } = addSpaceDto;
const community = await this.communityRepository.findOne({
where: { uuid: communityId },
});
// If the community doesn't exist, throw a 404 error
if (!community) {
throw new HttpException(
`Community with ID ${communityId} not found`,
HttpStatus.NOT_FOUND,
);
}
if (parentUuid) {
parent = await this.spaceRepository.findOne({
where: { uuid: parentUuid },
});
// If the community doesn't exist, throw a 404 error
if (!parent) {
throw new HttpException(
`Parent with ID ${parentUuid} not found`,
HttpStatus.NOT_FOUND,
);
}
}
try {
const newSpace = this.spaceRepository.create({
...addSpaceDto,
community,
parent: parentUuid ? parent : null,
});
await this.spaceRepository.save(newSpace);
return new SuccessResponseDto({
statusCode: HttpStatus.CREATED,
data: newSpace,
message: 'Space created successfully',
});
} catch (error) {
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
async list(
communityId: string,
pageable: Partial<TypeORMCustomModelFindAllQuery>,
): Promise<BaseResponseDto> {
const community = await this.communityRepository.findOne({
where: { uuid: communityId },
});
// If the community doesn't exist, throw a 404 error
if (!community) {
throw new HttpException(
`Community with ID ${communityId} not found`,
HttpStatus.NOT_FOUND,
);
}
try {
pageable.modelName = 'space';
pageable.where = {
community: { uuid: communityId }, // Ensure the relation is used in the where clause
};
console.log('Where clause before building:', pageable.where);
const customModel = TypeORMCustomModel(this.spaceRepository);
const { baseResponseDto, paginationResponseDto } =
await customModel.findAll(pageable);
return new PageResponse<SpaceDto>(baseResponseDto, paginationResponseDto);
} catch (error) {
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
async findOne(
spaceId: string,
communityId: string,
): Promise<BaseResponseDto> {
try {
const community = await this.communityRepository.findOne({
where: { uuid: communityId },
});
// If the community doesn't exist, throw a 404 error
if (!community) {
throw new HttpException(
`Community with ID ${communityId} not found`,
HttpStatus.NOT_FOUND,
);
}
const space = await this.spaceRepository.findOne({
where: {
uuid: spaceId,
community: {
uuid: communityId,
},
},
});
// If space is not found, throw a NotFoundException
if (!space) {
throw new HttpException(
`Space with UUID ${spaceId} not found`,
HttpStatus.NOT_FOUND,
);
}
return new SuccessResponseDto({
message: `Space with ID ${spaceId} successfully fetched`,
data: space,
});
} 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 community',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
async delete(spaceId: string, communityId: string): Promise<BaseResponseDto> {
try {
// First, check if the community exists
const community = await this.communityRepository.findOne({
where: { uuid: communityId },
});
if (!community) {
throw new HttpException(
`Community with ID ${communityId} not found`,
HttpStatus.NOT_FOUND,
);
}
// Check if the space exists
const space = await this.spaceRepository.findOne({
where: { uuid: spaceId, community: { uuid: communityId } },
});
if (!space) {
throw new HttpException(
`Space with ID ${spaceId} not found`,
HttpStatus.NOT_FOUND,
);
}
// Delete the space
await this.spaceRepository.remove(space);
return new SuccessResponseDto({
message: `Space with ID ${spaceId} successfully deleted`,
statusCode: HttpStatus.OK,
});
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
'An error occurred while deleting the space',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async updateSpace(
spaceId: string,
communityId: string,
updateSpaceDto: AddSpaceDto,
): Promise<BaseResponseDto> {
try {
// First, check if the community exists
const community = await this.communityRepository.findOne({
where: { uuid: communityId },
});
if (!community) {
throw new HttpException(
`Community with ID ${communityId} not found`,
HttpStatus.NOT_FOUND,
);
}
// Check if the space exists
const space = await this.spaceRepository.findOne({
where: { uuid: spaceId, community: { uuid: communityId } },
});
if (!space) {
throw new HttpException(
`Space with ID ${spaceId} not found`,
HttpStatus.NOT_FOUND,
);
}
// If a parentId is provided, check if the parent exists
const { parentUuid } = updateSpaceDto;
let parent: SpaceEntity | null = null;
if (parentUuid) {
parent = await this.spaceRepository.findOne({
where: { uuid: parentUuid, community: { uuid: communityId } },
});
// If the parent doesn't exist, throw a 404 error
if (!parent) {
throw new HttpException(
`Parent space with ID ${parentUuid} not found`,
HttpStatus.NOT_FOUND,
);
}
// Set the parent of the current space
space.parent = parent;
} else {
space.parent = null; // If no parent is provided, clear the parent
}
// Update other space properties from updateSpaceDto
Object.assign(space, updateSpaceDto);
// Save the updated space
await this.spaceRepository.save(space);
return new SuccessResponseDto({
message: `Space with ID ${spaceId} successfully updated`,
data: space,
statusCode: HttpStatus.OK,
});
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
'An error occurred while updating the space',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}