import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards, Request, UnauthorizedException, } from "@nestjs/common"; import { KnowledgeService } from "./knowledge.service"; import { CreateEntryDto, UpdateEntryDto, EntryQueryDto } from "./dto"; import { AuthGuard } from "../auth/guards/auth.guard"; /** * Controller for knowledge entry endpoints * All endpoints require authentication and enforce workspace isolation */ @Controller("knowledge/entries") @UseGuards(AuthGuard) export class KnowledgeController { constructor(private readonly knowledgeService: KnowledgeService) {} /** * GET /api/knowledge/entries * List all entries in the workspace with pagination and filtering */ @Get() async findAll( @Request() req: any, @Query() query: EntryQueryDto ) { const workspaceId = req.user?.workspaceId; if (!workspaceId) { throw new UnauthorizedException("Workspace context required"); } return this.knowledgeService.findAll(workspaceId, query); } /** * GET /api/knowledge/entries/:slug * Get a single entry by slug */ @Get(":slug") async findOne( @Request() req: any, @Param("slug") slug: string ) { const workspaceId = req.user?.workspaceId; if (!workspaceId) { throw new UnauthorizedException("Workspace context required"); } return this.knowledgeService.findOne(workspaceId, slug); } /** * POST /api/knowledge/entries * Create a new knowledge entry */ @Post() async create( @Request() req: any, @Body() createDto: CreateEntryDto ) { const workspaceId = req.user?.workspaceId; const userId = req.user?.id; if (!workspaceId || !userId) { throw new UnauthorizedException("Authentication required"); } return this.knowledgeService.create(workspaceId, userId, createDto); } /** * PUT /api/knowledge/entries/:slug * Update an existing entry */ @Put(":slug") async update( @Request() req: any, @Param("slug") slug: string, @Body() updateDto: UpdateEntryDto ) { const workspaceId = req.user?.workspaceId; const userId = req.user?.id; if (!workspaceId || !userId) { throw new UnauthorizedException("Authentication required"); } return this.knowledgeService.update(workspaceId, slug, userId, updateDto); } /** * DELETE /api/knowledge/entries/:slug * Soft delete an entry (sets status to ARCHIVED) */ @Delete(":slug") async remove( @Request() req: any, @Param("slug") slug: string ) { const workspaceId = req.user?.workspaceId; const userId = req.user?.id; if (!workspaceId || !userId) { throw new UnauthorizedException("Authentication required"); } await this.knowledgeService.remove(workspaceId, slug, userId); return { message: "Entry archived successfully" }; } }