import { Injectable, NotFoundException, BadRequestException, ConflictException } from "@nestjs/common"; import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger"; import { SpaceRepository, PageRepository, UserRepository } from "../global/database/repositories"; export class CreateSpaceDto { @ApiProperty({ description: '스페이스 키 (고유 식별자, URL에 사용)', example: 'engineering', maxLength: 50, pattern: '^[a-z0-9-]+$' }) spaceKey: string; @ApiProperty({ description: '스페이스 이름', example: 'Engineering Team', maxLength: 255 }) name: string; @ApiPropertyOptional({ description: '스페이스 설명', example: 'Engineering team documentation and knowledge base' }) description?: string; @ApiProperty({ description: '현재 사용자 ID (생성자)', example: '550e8400-e29b-41d4-a716-446655440000', format: 'uuid' }) userId: string; } export class UpdateSpaceDto { @ApiPropertyOptional({ description: '스페이스 이름', example: 'Updated Team Name', maxLength: 255 }) name?: string; @ApiPropertyOptional({ description: '스페이스 설명', example: 'Updated description' }) description?: string; } export class SpaceResponseDto { @ApiProperty({ description: '스페이스 ID', format: 'uuid' }) spaceId: string; @ApiProperty({ description: '스페이스 키 (고유 식별자)' }) spaceKey: string; @ApiProperty({ description: '스페이스 이름' }) name: string; @ApiProperty({ description: '스페이스 설명', nullable: true }) description: string | null; @ApiProperty({ description: '생성자 사용자 ID', format: 'uuid' }) createdByUserId: string; @ApiProperty({ description: '생성 시간', format: 'date-time' }) createdAt: string; } export class SpaceWithCreatorDto { @ApiProperty({ description: '스페이스 정보', type: SpaceResponseDto }) space: SpaceResponseDto; @ApiProperty({ description: '생성자 정보' }) creator: { userId: string; email: string; displayName: string; }; } export class SpaceStatisticsDto { @ApiProperty({ description: '스페이스 정보', type: SpaceResponseDto }) space: SpaceResponseDto; @ApiProperty({ description: '통계 정보' }) statistics: { totalPages: number; rootPages: number; }; } export class DeleteSpaceResponseDto { @ApiProperty({ description: '응답 메시지' }) message: string; @ApiProperty({ description: '삭제된 스페이스 ID', format: 'uuid' }) spaceId: string; } @Injectable() export class SpaceService { constructor( private readonly spaceRepository: SpaceRepository, private readonly pageRepository: PageRepository, private readonly userRepository: UserRepository, ) {} /** * 스페이스 생성 */ async createSpace(dto: CreateSpaceDto) { // 스페이스 키 중복 확인 const exists = await this.spaceRepository.existsByKey(dto.spaceKey); if (exists) { throw new ConflictException(`Space with key '${dto.spaceKey}' already exists`); } // 사용자 존재 확인 const user = await this.userRepository.findById(dto.userId); if (!user) { throw new NotFoundException(`User with id '${dto.userId}' not found`); } // 스페이스 키 형식 검증 (소문자, 숫자, 하이픈만 허용) const spaceKeyRegex = /^[a-z0-9-]+$/; if (!spaceKeyRegex.test(dto.spaceKey)) { throw new BadRequestException('Space key must contain only lowercase letters, numbers, and hyphens'); } const space = await this.spaceRepository.create({ spaceKey: dto.spaceKey, name: dto.name, description: dto.description, createdByUserId: dto.userId, }); return space; } /** * 스페이스 ID로 조회 */ async getSpaceById(spaceId: string) { const space = await this.spaceRepository.findById(spaceId); if (!space) { throw new NotFoundException(`Space with id '${spaceId}' not found`); } return space; } /** * 스페이스 키로 조회 */ async getSpaceByKey(spaceKey: string) { const space = await this.spaceRepository.findByKey(spaceKey); if (!space) { throw new NotFoundException(`Space with key '${spaceKey}' not found`); } return space; } /** * 모든 스페이스 조회 */ async getAllSpaces() { return await this.spaceRepository.findAll(); } /** * 생성자 정보와 함께 스페이스 조회 */ async getSpaceWithCreator(spaceId: string) { const result = await this.spaceRepository.findByIdWithCreator(spaceId); if (!result) { throw new NotFoundException(`Space with id '${spaceId}' not found`); } return result; } /** * 모든 스페이스를 생성자 정보와 함께 조회 */ async getAllSpacesWithCreator() { return await this.spaceRepository.findAllWithCreator(); } /** * 스페이스 통계 조회 */ async getSpaceStatistics(spaceId: string) { const space = await this.spaceRepository.findById(spaceId); if (!space) { throw new NotFoundException(`Space with id '${spaceId}' not found`); } const allPages = await this.pageRepository.findBySpaceId(spaceId); const rootPages = await this.pageRepository.findRootPagesBySpaceId(spaceId); return { space, statistics: { totalPages: allPages.length, rootPages: rootPages.length, }, }; } /** * 사용자가 생성한 스페이스 조회 */ async getSpacesByCreator(userId: string) { const user = await this.userRepository.findById(userId); if (!user) { throw new NotFoundException(`User with id '${userId}' not found`); } return await this.spaceRepository.findByCreator(userId); } /** * 스페이스 업데이트 */ async updateSpace(spaceId: string, dto: UpdateSpaceDto) { const space = await this.spaceRepository.findById(spaceId); if (!space) { throw new NotFoundException(`Space with id '${spaceId}' not found`); } const updatedSpace = await this.spaceRepository.update(spaceId, dto); return updatedSpace; } /** * 스페이스 삭제 (cascade로 페이지도 함께 삭제됨) */ async deleteSpace(spaceId: string) { const space = await this.spaceRepository.findById(spaceId); if (!space) { throw new NotFoundException(`Space with id '${spaceId}' not found`); } await this.spaceRepository.delete(spaceId); return { message: 'Space deleted successfully', spaceId }; } }