|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Delete, |
| 5 | + Get, |
| 6 | + HttpCode, |
| 7 | + HttpStatus, |
| 8 | + Param, |
| 9 | + ParseIntPipe, |
| 10 | + Patch, |
| 11 | + Post, |
| 12 | + Query, |
| 13 | + Req, |
| 14 | + UseGuards, |
| 15 | +} from "@nestjs/common" |
| 16 | +import { |
| 17 | + ApiBearerAuth, |
| 18 | + ApiConflictResponse, |
| 19 | + ApiCreatedResponse, |
| 20 | + ApiNoContentResponse, |
| 21 | + ApiNotFoundResponse, |
| 22 | + ApiOkResponse, |
| 23 | + ApiOperation, |
| 24 | + ApiTags, |
| 25 | +} from "@nestjs/swagger" |
| 26 | +import type { Request } from "express" |
| 27 | +import { AuthGuard } from "../common/guards/auth.guard" |
| 28 | +import { StreamOwnershipGuard } from "../common/guards/stream-ownership.guard" |
| 29 | +import { CreateStreamDto } from "./dto/create-stream.dto" |
| 30 | +import { ListStreamsQueryDto } from "./dto/list-streams.query.dto" |
| 31 | +import { UpdateStreamDto } from "./dto/update-stream.dto" |
| 32 | +import { StreamsService } from "./streams.service" |
| 33 | + |
| 34 | +/** |
| 35 | + * Full CRUD for streams. |
| 36 | + * |
| 37 | + * POST /streams Create a new stream (auth required) |
| 38 | + * GET /streams List streams (auth required, paginated) |
| 39 | + * GET /streams/:id Get a single stream (ownership required) |
| 40 | + * PATCH /streams/:id Update stream details (ownership required) |
| 41 | + * DELETE /streams/:id Delete a stream (ownership required) |
| 42 | + */ |
| 43 | +@ApiTags("streams") |
| 44 | +@ApiBearerAuth() |
| 45 | +@Controller("streams") |
| 46 | +export class StreamsController { |
| 47 | + constructor(private readonly streamsService: StreamsService) {} |
| 48 | + |
| 49 | + /** |
| 50 | + * Create a new stream. The authenticated user becomes the owner. |
| 51 | + */ |
| 52 | + @Post() |
| 53 | + @HttpCode(HttpStatus.CREATED) |
| 54 | + @UseGuards(AuthGuard) |
| 55 | + @ApiOperation({ |
| 56 | + summary: "Create a new stream", |
| 57 | + description: "Creates a new stream with the authenticated user as owner.", |
| 58 | + }) |
| 59 | + @ApiCreatedResponse({ description: "Stream created successfully." }) |
| 60 | + create( |
| 61 | + @Body() body: CreateStreamDto, |
| 62 | + @Req() req: Request & { auth?: { userId: number } }, |
| 63 | + ) { |
| 64 | + return this.streamsService.create({ |
| 65 | + userId: req.auth!.userId, |
| 66 | + name: body.name, |
| 67 | + description: body.description, |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * List all streams with optional status filter and pagination. |
| 73 | + */ |
| 74 | + @Get() |
| 75 | + @UseGuards(AuthGuard) |
| 76 | + @ApiOperation({ |
| 77 | + summary: "List streams", |
| 78 | + description: "Returns a paginated list of streams with optional status filter.", |
| 79 | + }) |
| 80 | + @ApiOkResponse({ description: "Paginated list of streams." }) |
| 81 | + list(@Query() query: ListStreamsQueryDto) { |
| 82 | + const page = query.page ?? 1 |
| 83 | + const limit = query.limit ?? 20 |
| 84 | + return this.streamsService.list(page, limit, { |
| 85 | + status: query.status, |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get a single stream by id. Requires stream ownership. |
| 91 | + */ |
| 92 | + @Get(":id") |
| 93 | + @UseGuards(StreamOwnershipGuard) |
| 94 | + @ApiOperation({ |
| 95 | + summary: "Get a stream", |
| 96 | + description: "Returns a single stream by id. Requires ownership.", |
| 97 | + }) |
| 98 | + @ApiOkResponse({ description: "Stream found." }) |
| 99 | + @ApiNotFoundResponse({ description: "Stream not found." }) |
| 100 | + findById(@Param("id", ParseIntPipe) id: number) { |
| 101 | + return this.streamsService.findById(id) |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Update stream details (name, description, status). |
| 106 | + * Requires stream ownership. |
| 107 | + */ |
| 108 | + @Patch(":id") |
| 109 | + @UseGuards(StreamOwnershipGuard) |
| 110 | + @ApiOperation({ |
| 111 | + summary: "Update a stream", |
| 112 | + description: "Partially updates a stream. Requires ownership.", |
| 113 | + }) |
| 114 | + @ApiOkResponse({ description: "Stream updated." }) |
| 115 | + @ApiNotFoundResponse({ description: "Stream not found." }) |
| 116 | + @ApiConflictResponse({ description: "Invalid status transition." }) |
| 117 | + update( |
| 118 | + @Param("id", ParseIntPipe) id: number, |
| 119 | + @Body() body: UpdateStreamDto, |
| 120 | + ) { |
| 121 | + return this.streamsService.update(id, body) |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Delete a stream. Requires stream ownership. |
| 126 | + */ |
| 127 | + @Delete(":id") |
| 128 | + @HttpCode(HttpStatus.NO_CONTENT) |
| 129 | + @UseGuards(StreamOwnershipGuard) |
| 130 | + @ApiOperation({ |
| 131 | + summary: "Delete a stream", |
| 132 | + description: "Deletes a stream by id. Requires ownership.", |
| 133 | + }) |
| 134 | + @ApiNoContentResponse({ description: "Stream deleted." }) |
| 135 | + @ApiNotFoundResponse({ description: "Stream not found." }) |
| 136 | + delete(@Param("id", ParseIntPipe) id: number): void { |
| 137 | + this.streamsService.delete(id) |
| 138 | + } |
| 139 | +} |
0 commit comments