-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprofile-space.controller.ts
126 lines (121 loc) ยท 3.95 KB
/
profile-space.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import {
Controller,
Get,
Post,
Body,
Delete,
Param,
Request as Req,
NotFoundException,
HttpException,
HttpStatus,
ConflictException,
} from '@nestjs/common';
import { ProfileSpaceService } from './profile-space.service';
import { CreateProfileSpaceDto } from './dto/create-profile-space.dto';
import { RequestWithUser } from '../utils/interface';
import { SpacesService } from '../spaces/spaces.service';
import { ApiTags, ApiResponse, ApiOperation } from '@nestjs/swagger';
import { ProfilesService } from '../profiles/profiles.service';
@Controller('profileSpace')
@ApiTags('profileSpace')
export class ProfileSpaceController {
constructor(
private readonly profileSpaceService: ProfileSpaceService,
private readonly spacesService: SpacesService,
private readonly profilesService: ProfilesService,
) {}
@Post('join')
@ApiOperation({ summary: 'Join space' })
@ApiResponse({
status: 201,
description: 'Join data has been successfully created.',
})
@ApiResponse({
status: 409,
description: 'Conflict. You have already joined the space.',
})
async create(
@Body() createProfileSpaceDto: CreateProfileSpaceDto,
@Req() req: RequestWithUser,
) {
const profile = await this.profilesService.findProfile(req.user.uuid);
if (!profile) throw new NotFoundException();
const profileSpace = await this.profileSpaceService.joinSpace(
profile.uuid,
createProfileSpaceDto.space_uuid,
);
if (!profileSpace) {
throw new HttpException('Data already exists.', HttpStatus.CONFLICT);
}
return { statusCode: 201, message: 'Created', data: profileSpace };
}
@Delete('leave/:space_uuid')
@ApiResponse({
status: 204,
description: 'Successfully left the space.',
})
@ApiResponse({
status: 404,
description: 'Space not found.',
})
async delete(
@Param('space_uuid') spaceUuid: string,
@Req() req: RequestWithUser,
) {
const profile = await this.profilesService.findProfile(req.user.uuid);
if (!profile) throw new NotFoundException();
const space = await this.spacesService.findSpace(spaceUuid);
if (!space) throw new NotFoundException();
const profileSpace = await this.profileSpaceService.leaveSpace(
profile.uuid,
spaceUuid,
);
if (!profileSpace) throw new ConflictException();
const isSpaceEmpty = await this.profileSpaceService.isSpaceEmpty(spaceUuid);
if (isSpaceEmpty) {
await this.spacesService.deleteSpace(spaceUuid);
}
return { statusCode: 204, message: 'No Content' };
}
@Get('spaces')
@ApiOperation({ summary: "Get user's spaces" })
@ApiResponse({
status: 200,
description: 'Returns a list of spaces.',
})
async getSpaces(@Req() req: RequestWithUser) {
const profile = await this.profilesService.findProfile(req.user.uuid);
if (!profile) throw new NotFoundException();
const profileSpaces =
await this.profileSpaceService.findProfileSpacesByProfileUuid(
profile.uuid,
);
const spaceUuids = profileSpaces.map(
(profileSpace) => profileSpace.space_uuid,
);
const spaces = await this.spacesService.findSpaces(spaceUuids);
return { statusCode: 200, message: 'Success', data: spaces };
}
@Get('users/:space_uuid')
@ApiOperation({ summary: 'Get users in the space' })
@ApiResponse({
status: 200,
description: 'Returns a list of users.',
})
@ApiResponse({
status: 404,
description: 'Space not found.',
})
async getProfiles(@Param('space_uuid') spaceUuid: string) {
const space = await this.spacesService.findSpace(spaceUuid);
if (!space) throw new NotFoundException();
const profileSpaces =
await this.profileSpaceService.findProfileSpacesBySpaceUuid(space.uuid);
const profileUuids = profileSpaces.map(
(profileSpace) => profileSpace.profile_uuid,
);
const profiles = await this.profilesService.findProfiles(profileUuids);
return { statusCode: 200, message: 'Success', data: profiles };
}
}