Skip to content

Commit 0f37552

Browse files
authored
Merge pull request #355 from boostcampwm2023/BE-feature/spaces
ProfileSpace관련 동작 관련 모듈로 API 이동
2 parents bc80aed + 7a7fe07 commit 0f37552

22 files changed

+1276
-493
lines changed

nestjs-BE/server/src/profile-space/dto/create-profile-space.dto.ts

-14
This file was deleted.

nestjs-BE/server/src/profile-space/dto/update-profile-space.dto.ts

-6
This file was deleted.

nestjs-BE/server/src/profile-space/profile-space.controller.ts

-126
This file was deleted.

nestjs-BE/server/src/profile-space/profile-space.module.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import { Module, forwardRef } from '@nestjs/common';
1+
import { Module } from '@nestjs/common';
22
import { ProfileSpaceService } from './profile-space.service';
3-
import { ProfileSpaceController } from './profile-space.controller';
43
import { ProfilesModule } from '../profiles/profiles.module';
5-
import { SpacesModule } from '../spaces/spaces.module';
64

75
@Module({
8-
imports: [ProfilesModule, forwardRef(() => SpacesModule)],
9-
controllers: [ProfileSpaceController],
6+
imports: [ProfilesModule],
107
providers: [ProfileSpaceService],
118
exports: [ProfileSpaceService],
129
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { ProfileSpaceService } from './profile-space.service';
3+
import { PrismaService } from '../prisma/prisma.service';
4+
import { ProfileSpace } from '@prisma/client';
5+
6+
describe('ProfileSpaceService', () => {
7+
let profileSpaceService: ProfileSpaceService;
8+
let prisma: PrismaService;
9+
10+
beforeEach(async () => {
11+
const module: TestingModule = await Test.createTestingModule({
12+
providers: [
13+
ProfileSpaceService,
14+
{
15+
provide: PrismaService,
16+
useValue: {
17+
profileSpace: {
18+
findFirst: jest.fn(),
19+
findUnique: jest.fn(),
20+
},
21+
},
22+
},
23+
],
24+
}).compile();
25+
26+
profileSpaceService = module.get<ProfileSpaceService>(ProfileSpaceService);
27+
prisma = module.get<PrismaService>(PrismaService);
28+
});
29+
30+
it('isSpaceEmpty empty', async () => {
31+
const spaceUuid = 'space uuid';
32+
33+
(prisma.profileSpace.findFirst as jest.Mock).mockResolvedValue(null);
34+
35+
const isSpaceEmpty = profileSpaceService.isSpaceEmpty(spaceUuid);
36+
37+
await expect(isSpaceEmpty).resolves.toBeTruthy();
38+
});
39+
40+
it('isSpaceEmpty not empty', async () => {
41+
const spaceUuid = 'space uuid';
42+
const profileSpace = 'profile space';
43+
44+
(prisma.profileSpace.findFirst as jest.Mock).mockResolvedValue(
45+
profileSpace,
46+
);
47+
48+
const isSpaceEmpty = profileSpaceService.isSpaceEmpty(spaceUuid);
49+
50+
await expect(isSpaceEmpty).resolves.toBeFalsy();
51+
});
52+
53+
it('isProfileInSpace joined', async () => {
54+
const spaceUuid = 'space uuid';
55+
const profileUuid = 'profile uuid';
56+
const profileSpaceMock = { profileUuid, spaceUuid } as ProfileSpace;
57+
58+
(prisma.profileSpace.findUnique as jest.Mock).mockResolvedValue(
59+
profileSpaceMock,
60+
);
61+
62+
const isProfileInSpace = profileSpaceService.isProfileInSpace(
63+
profileUuid,
64+
spaceUuid,
65+
);
66+
67+
await expect(isProfileInSpace).resolves.toBeTruthy();
68+
});
69+
70+
it('isProfileInSpace not joined', async () => {
71+
const spaceUuid = 'space uuid';
72+
const profileUuid = 'profile uuid';
73+
74+
(prisma.profileSpace.findUnique as jest.Mock).mockResolvedValue(null);
75+
76+
const isProfileInSpace = profileSpaceService.isProfileInSpace(
77+
profileUuid,
78+
spaceUuid,
79+
);
80+
81+
await expect(isProfileInSpace).resolves.toBeFalsy();
82+
});
83+
});

nestjs-BE/server/src/profile-space/profile-space.service.ts

+20-30
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ import { Prisma, ProfileSpace } from '@prisma/client';
66
export class ProfileSpaceService {
77
constructor(private readonly prisma: PrismaService) {}
88

9-
async findProfileSpacesByProfileUuid(
9+
async createProfileSpace(
1010
profileUuid: string,
11-
): Promise<ProfileSpace[]> {
12-
return this.prisma.profileSpace.findMany({
13-
where: { profileUuid: profileUuid },
11+
spaceUuid: string,
12+
): Promise<ProfileSpace | null> {
13+
return this.prisma.profileSpace.create({
14+
data: { spaceUuid, profileUuid },
1415
});
1516
}
1617

17-
async findProfileSpacesBySpaceUuid(
18+
async deleteProfileSpace(
19+
profileUuid: string,
1820
spaceUuid: string,
19-
): Promise<ProfileSpace[]> {
20-
return this.prisma.profileSpace.findMany({
21-
where: { spaceUuid: spaceUuid },
21+
): Promise<ProfileSpace | null> {
22+
return this.prisma.profileSpace.delete({
23+
where: { spaceUuid_profileUuid: { spaceUuid, profileUuid } },
2224
});
2325
}
2426

@@ -48,28 +50,6 @@ export class ProfileSpaceService {
4850
}
4951
}
5052

51-
async leaveSpace(
52-
profileUuid: string,
53-
spaceUuid: string,
54-
): Promise<ProfileSpace | null> {
55-
try {
56-
return await this.prisma.profileSpace.delete({
57-
where: {
58-
spaceUuid_profileUuid: {
59-
spaceUuid: spaceUuid,
60-
profileUuid: profileUuid,
61-
},
62-
},
63-
});
64-
} catch (err) {
65-
if (err instanceof Prisma.PrismaClientKnownRequestError) {
66-
return null;
67-
} else {
68-
throw err;
69-
}
70-
}
71-
}
72-
7353
async isSpaceEmpty(spaceUuid: string) {
7454
const first = await this.prisma.profileSpace.findFirst({
7555
where: {
@@ -78,4 +58,14 @@ export class ProfileSpaceService {
7858
});
7959
return first ? false : true;
8060
}
61+
62+
async isProfileInSpace(
63+
profileUuid: string,
64+
spaceUuid: string,
65+
): Promise<boolean> {
66+
const profileSpace = await this.prisma.profileSpace.findUnique({
67+
where: { spaceUuid_profileUuid: { spaceUuid, profileUuid } },
68+
});
69+
return profileSpace ? true : false;
70+
}
8171
}

nestjs-BE/server/src/profiles/dto/profile-space.dto.ts

-12
This file was deleted.

nestjs-BE/server/src/spaces/dto/create-space.dto.ts

+1-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MAX_NAME_LENGTH } from '../../config/magic-number';
44
import { Expose } from 'class-transformer';
55
import { v4 as uuid } from 'uuid';
66

7-
export class CreateSpaceRequestV2Dto {
7+
export class CreateSpaceRequestDto {
88
@IsString()
99
@IsNotEmpty()
1010
@MaxLength(MAX_NAME_LENGTH)
@@ -24,20 +24,6 @@ export class CreateSpaceRequestV2Dto {
2424
icon: string;
2525
}
2626

27-
export class CreateSpaceRequestDto {
28-
@IsString()
29-
@IsNotEmpty()
30-
@MaxLength(MAX_NAME_LENGTH)
31-
@ApiProperty({ example: 'Sample Space', description: 'Name of the space' })
32-
name: string;
33-
34-
@ApiProperty({
35-
example: 'space-icon.png',
36-
description: 'Profile icon for the space',
37-
})
38-
icon: string;
39-
}
40-
4127
export class CreateSpacePrismaDto {
4228
name: string;
4329
icon: string;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { Expose } from 'class-transformer';
3+
import { IsNotEmpty, IsString } from 'class-validator';
4+
import { v4 as uuid } from 'uuid';
5+
6+
export class JoinSpaceRequestDto {
7+
@IsString()
8+
@IsNotEmpty()
9+
@Expose({ name: 'profile_uuid' })
10+
@ApiProperty({ example: uuid(), description: 'Profile uuid' })
11+
profileUuid: string;
12+
}

0 commit comments

Comments
 (0)