Skip to content

Commit

Permalink
Merge pull request #355 from boostcampwm2023/BE-feature/spaces
Browse files Browse the repository at this point in the history
ProfileSpace관련 동작 관련 모듈로 API 이동
  • Loading branch information
Conut-1 authored Oct 7, 2024
2 parents bc80aed + 7a7fe07 commit 0f37552
Show file tree
Hide file tree
Showing 22 changed files with 1,276 additions and 493 deletions.
14 changes: 0 additions & 14 deletions nestjs-BE/server/src/profile-space/dto/create-profile-space.dto.ts

This file was deleted.

This file was deleted.

126 changes: 0 additions & 126 deletions nestjs-BE/server/src/profile-space/profile-space.controller.ts

This file was deleted.

7 changes: 2 additions & 5 deletions nestjs-BE/server/src/profile-space/profile-space.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Module, forwardRef } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { ProfileSpaceService } from './profile-space.service';
import { ProfileSpaceController } from './profile-space.controller';
import { ProfilesModule } from '../profiles/profiles.module';
import { SpacesModule } from '../spaces/spaces.module';

@Module({
imports: [ProfilesModule, forwardRef(() => SpacesModule)],
controllers: [ProfileSpaceController],
imports: [ProfilesModule],
providers: [ProfileSpaceService],
exports: [ProfileSpaceService],
})
Expand Down
83 changes: 83 additions & 0 deletions nestjs-BE/server/src/profile-space/profile-space.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ProfileSpaceService } from './profile-space.service';
import { PrismaService } from '../prisma/prisma.service';
import { ProfileSpace } from '@prisma/client';

describe('ProfileSpaceService', () => {
let profileSpaceService: ProfileSpaceService;
let prisma: PrismaService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ProfileSpaceService,
{
provide: PrismaService,
useValue: {
profileSpace: {
findFirst: jest.fn(),
findUnique: jest.fn(),
},
},
},
],
}).compile();

profileSpaceService = module.get<ProfileSpaceService>(ProfileSpaceService);
prisma = module.get<PrismaService>(PrismaService);
});

it('isSpaceEmpty empty', async () => {
const spaceUuid = 'space uuid';

(prisma.profileSpace.findFirst as jest.Mock).mockResolvedValue(null);

const isSpaceEmpty = profileSpaceService.isSpaceEmpty(spaceUuid);

await expect(isSpaceEmpty).resolves.toBeTruthy();
});

it('isSpaceEmpty not empty', async () => {
const spaceUuid = 'space uuid';
const profileSpace = 'profile space';

(prisma.profileSpace.findFirst as jest.Mock).mockResolvedValue(
profileSpace,
);

const isSpaceEmpty = profileSpaceService.isSpaceEmpty(spaceUuid);

await expect(isSpaceEmpty).resolves.toBeFalsy();
});

it('isProfileInSpace joined', async () => {
const spaceUuid = 'space uuid';
const profileUuid = 'profile uuid';
const profileSpaceMock = { profileUuid, spaceUuid } as ProfileSpace;

(prisma.profileSpace.findUnique as jest.Mock).mockResolvedValue(
profileSpaceMock,
);

const isProfileInSpace = profileSpaceService.isProfileInSpace(
profileUuid,
spaceUuid,
);

await expect(isProfileInSpace).resolves.toBeTruthy();
});

it('isProfileInSpace not joined', async () => {
const spaceUuid = 'space uuid';
const profileUuid = 'profile uuid';

(prisma.profileSpace.findUnique as jest.Mock).mockResolvedValue(null);

const isProfileInSpace = profileSpaceService.isProfileInSpace(
profileUuid,
spaceUuid,
);

await expect(isProfileInSpace).resolves.toBeFalsy();
});
});
50 changes: 20 additions & 30 deletions nestjs-BE/server/src/profile-space/profile-space.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import { Prisma, ProfileSpace } from '@prisma/client';
export class ProfileSpaceService {
constructor(private readonly prisma: PrismaService) {}

async findProfileSpacesByProfileUuid(
async createProfileSpace(
profileUuid: string,
): Promise<ProfileSpace[]> {
return this.prisma.profileSpace.findMany({
where: { profileUuid: profileUuid },
spaceUuid: string,
): Promise<ProfileSpace | null> {
return this.prisma.profileSpace.create({
data: { spaceUuid, profileUuid },
});
}

async findProfileSpacesBySpaceUuid(
async deleteProfileSpace(
profileUuid: string,
spaceUuid: string,
): Promise<ProfileSpace[]> {
return this.prisma.profileSpace.findMany({
where: { spaceUuid: spaceUuid },
): Promise<ProfileSpace | null> {
return this.prisma.profileSpace.delete({
where: { spaceUuid_profileUuid: { spaceUuid, profileUuid } },
});
}

Expand Down Expand Up @@ -48,28 +50,6 @@ export class ProfileSpaceService {
}
}

async leaveSpace(
profileUuid: string,
spaceUuid: string,
): Promise<ProfileSpace | null> {
try {
return await this.prisma.profileSpace.delete({
where: {
spaceUuid_profileUuid: {
spaceUuid: spaceUuid,
profileUuid: profileUuid,
},
},
});
} catch (err) {
if (err instanceof Prisma.PrismaClientKnownRequestError) {
return null;
} else {
throw err;
}
}
}

async isSpaceEmpty(spaceUuid: string) {
const first = await this.prisma.profileSpace.findFirst({
where: {
Expand All @@ -78,4 +58,14 @@ export class ProfileSpaceService {
});
return first ? false : true;
}

async isProfileInSpace(
profileUuid: string,
spaceUuid: string,
): Promise<boolean> {
const profileSpace = await this.prisma.profileSpace.findUnique({
where: { spaceUuid_profileUuid: { spaceUuid, profileUuid } },
});
return profileSpace ? true : false;
}
}
12 changes: 0 additions & 12 deletions nestjs-BE/server/src/profiles/dto/profile-space.dto.ts

This file was deleted.

16 changes: 1 addition & 15 deletions nestjs-BE/server/src/spaces/dto/create-space.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MAX_NAME_LENGTH } from '../../config/magic-number';
import { Expose } from 'class-transformer';
import { v4 as uuid } from 'uuid';

export class CreateSpaceRequestV2Dto {
export class CreateSpaceRequestDto {
@IsString()
@IsNotEmpty()
@MaxLength(MAX_NAME_LENGTH)
Expand All @@ -24,20 +24,6 @@ export class CreateSpaceRequestV2Dto {
icon: string;
}

export class CreateSpaceRequestDto {
@IsString()
@IsNotEmpty()
@MaxLength(MAX_NAME_LENGTH)
@ApiProperty({ example: 'Sample Space', description: 'Name of the space' })
name: string;

@ApiProperty({
example: 'space-icon.png',
description: 'Profile icon for the space',
})
icon: string;
}

export class CreateSpacePrismaDto {
name: string;
icon: string;
Expand Down
12 changes: 12 additions & 0 deletions nestjs-BE/server/src/spaces/dto/join-space.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsString } from 'class-validator';
import { v4 as uuid } from 'uuid';

export class JoinSpaceRequestDto {
@IsString()
@IsNotEmpty()
@Expose({ name: 'profile_uuid' })
@ApiProperty({ example: uuid(), description: 'Profile uuid' })
profileUuid: string;
}
Loading

0 comments on commit 0f37552

Please sign in to comment.