File size: 5,861 Bytes
b0984a9
 
 
 
 
 
 
 
 
 
 
 
 
e6677f0
 
 
 
 
 
 
 
b0984a9
 
 
 
 
 
 
 
e6677f0
b0984a9
 
e6677f0
b0984a9
 
 
 
 
e6677f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0984a9
 
 
 
 
 
 
 
e6677f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0984a9
 
 
 
 
 
 
e6677f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0984a9
 
 
 
 
 
 
e6677f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0984a9
 
 
 
 
 
 
e6677f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0984a9
 
 
 
 
 
 
 
 
e6677f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0984a9
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  UseGuards,
  HttpCode,
  HttpStatus,
  ParseIntPipe,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiParam,
  ApiBody,
} from '@nestjs/swagger';
import { FolderService } from './folder.service';
import { CreateFolderDto } from './dto/create-folder.dto';
import { UpdateFolderDto } from './dto/update-folder.dto';
import { FolderResponseDto } from './dto/folder-response.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { User } from '../auth/entities/user.entity';

@ApiTags('πŸ“‚ Folders')
@Controller('api/files/folders')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth('JWT-auth')
export class FolderController {
  constructor(private readonly folderService: FolderService) {}

  @Post()
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({
    summary: 'Create folder',
    description: `
## Create New Folder

Creates a new folder for organizing files.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: Any authenticated user

### Notes
- Folders can be nested inside other folders
- Root folders have no parent
    `,
  })
  @ApiBody({ type: CreateFolderDto })
  @ApiResponse({ status: 201, description: 'Folder created successfully' })
  @ApiResponse({ status: 400, description: 'Invalid input data' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  async create(
    @Body() createFolderDto: CreateFolderDto,
    @CurrentUser() user: User,
  ): Promise<FolderResponseDto> {
    return this.folderService.create(createFolderDto, user.userId);
  }

  @Get(':folderId')
  @ApiOperation({
    summary: 'Get folder by ID',
    description: `
## Get Folder Details

Retrieves information about a specific folder.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: Any authenticated user with access
    `,
  })
  @ApiParam({ name: 'folderId', description: 'Folder ID', type: Number })
  @ApiResponse({ status: 200, description: 'Folder details' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 404, description: 'Folder not found' })
  async findById(
    @Param('folderId', ParseIntPipe) folderId: number,
  ): Promise<FolderResponseDto> {
    return this.folderService.findById(folderId);
  }

  @Get(':folderId/children')
  @ApiOperation({
    summary: 'Get folder contents',
    description: `
## Get Folder Children

Retrieves all folders and files within a folder.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: Any authenticated user with access

### Response
Returns separate arrays for subfolders and files.
    `,
  })
  @ApiParam({ name: 'folderId', description: 'Folder ID', type: Number })
  @ApiResponse({ status: 200, description: 'Folder contents' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 404, description: 'Folder not found' })
  async getChildren(
    @Param('folderId', ParseIntPipe) folderId: number,
  ): Promise<{ folders: FolderResponseDto[]; files: any[] }> {
    return this.folderService.getChildren(folderId);
  }

  @Get(':folderId/tree')
  @ApiOperation({
    summary: 'Get folder tree',
    description: `
## Get Folder Tree Structure

Retrieves the folder with its complete nested hierarchy.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: Any authenticated user with access

### Response
Returns folder with nested children recursively.
    `,
  })
  @ApiParam({ name: 'folderId', description: 'Folder ID', type: Number })
  @ApiResponse({ status: 200, description: 'Folder tree' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 404, description: 'Folder not found' })
  async getFolderTree(
    @Param('folderId', ParseIntPipe) folderId: number,
  ): Promise<FolderResponseDto> {
    return this.folderService.getFolderTree(folderId);
  }

  @Put(':folderId')
  @ApiOperation({
    summary: 'Update folder',
    description: `
## Update Folder

Updates folder name or moves it to a different parent.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: Folder owner or admin
    `,
  })
  @ApiParam({ name: 'folderId', description: 'Folder ID', type: Number })
  @ApiBody({ type: UpdateFolderDto })
  @ApiResponse({ status: 200, description: 'Folder updated' })
  @ApiResponse({ status: 400, description: 'Invalid input data' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 404, description: 'Folder not found' })
  async update(
    @Param('folderId', ParseIntPipe) folderId: number,
    @Body() updateFolderDto: UpdateFolderDto,
  ): Promise<FolderResponseDto> {
    return this.folderService.update(folderId, updateFolderDto);
  }

  @Delete(':folderId')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({
    summary: 'Delete folder',
    description: `
## Delete Folder

Deletes a folder and all its contents.

### Access Control
- **Authentication Required**: βœ… Yes (Bearer Token)
- **Roles Required**: Folder owner or admin

### ⚠️ Warning
This recursively deletes all subfolders and files within!
    `,
  })
  @ApiParam({ name: 'folderId', description: 'Folder ID', type: Number })
  @ApiResponse({ status: 204, description: 'Folder deleted' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden' })
  @ApiResponse({ status: 404, description: 'Folder not found' })
  async delete(
    @Param('folderId', ParseIntPipe) folderId: number,
  ): Promise<void> {
    return this.folderService.delete(folderId);
  }
}