1import { Inject, Body, Controller, Delete, Get, Param, Patch, Post, Query, Req } from "@nestjs/common"; 2import type { Request } from "express"; 3import { BlockService } from "./block.service.js"; 4import { ZodValidate } from "../../common/pipes/zod-validation.pipe.js"; 5import { requireActor } from "../../common/types.js"; 6import { 7 BatchBlocksDto, 8 type BatchBlocksInput, 9 CreateBlockDto, 10 type CreateBlockInput, 11 DuplicateBlockDto, 12 type DuplicateBlockInput, 13 ListBlocksQuery, 14 type ListBlocksInput, 15 MoveBlockDto, 16 type MoveBlockInput, 17 UpdateBlockDto, 18 type UpdateBlockInput 19} from "./dto.js"; 20 21@Controller() 22export class BlocksController { 23 constructor(@Inject(BlockService) private readonly blocks: BlockService) {} 24 25 @Get("pages/:pageId/blocks") 26 list( 27 @Param("pageId") pageId: string, 28 @Query(ZodValidate(ListBlocksQuery)) query: ListBlocksInput, 29 @Req() req: Request 30 ) { 31 return this.blocks.list(pageId, requireActor(req), query); 32 } 33 34 @Post("pages/:pageId/blocks") 35 create( 36 @Param("pageId") pageId: string, 37 @Body(ZodValidate(CreateBlockDto)) body: CreateBlockInput, 38 @Req() req: Request 39 ) { 40 return this.blocks.create(pageId, requireActor(req), body, req); 41 } 42 43 @Get("pages/:pageId/content") 44 content(@Param("pageId") pageId: string, @Req() req: Request) { 45 return this.blocks.content(pageId, requireActor(req)); 46 } 47 48 @Patch("blocks/:id") 49 update( 50 @Param("id") id: string, 51 @Body(ZodValidate(UpdateBlockDto)) body: UpdateBlockInput, 52 @Req() req: Request 53 ) { 54 return this.blocks.update(id, requireActor(req), body, req); 55 } 56 57 @Post("blocks/:id/move") 58 move( 59 @Param("id") id: string, 60 @Body(ZodValidate(MoveBlockDto)) body: MoveBlockInput, 61 @Req() req: Request 62 ) { 63 return this.blocks.move(id, requireActor(req), body, req); 64 } 65 66 @Delete("blocks/:id") 67 delete(@Param("id") id: string, @Req() req: Request) { 68 return this.blocks.delete(id, requireActor(req), req); 69 } 70 71 @Post("blocks/:id/duplicate") 72 duplicate( 73 @Param("id") id: string, 74 @Body(ZodValidate(DuplicateBlockDto)) body: DuplicateBlockInput, 75 @Req() req: Request 76 ) { 77 return this.blocks.duplicate(id, requireActor(req), req, body); 78 } 79 80 @Post("blocks/batch") 81 batch( 82 @Body(ZodValidate(BatchBlocksDto)) body: BatchBlocksInput, 83 @Req() req: Request 84 ) { 85 return this.blocks.batch(body.pageId, requireActor(req), body, req); 86 } 87} 88