/** * API Endpoint Template for Backend Agent (Node.js / NestJS) * * Demonstrates the Router -> Service -> Repository pattern * using NestJS, Prisma, and Zod validation. * * File layout (split into real modules in production): * src/modules/resources/ * resources.controller.ts <- this file (controller + decorators) * resources.service.ts <- business logic * resources.repository.ts <- Prisma data access * dto/resource.dto.ts <- Zod schemas + inferred types * resources.module.ts <- DI wiring * src/common/ * errors/app-error.ts <- domain error type * filters/app-error.filter.ts <- maps domain errors to HTTP responses */ // --------------------------------------------------------------------------- // common/errors/app-error.ts (services throw this, never HTTP exceptions) // --------------------------------------------------------------------------- export type AppErrorCode = 'not_found' | 'forbidden' | 'conflict'; export class AppError extends Error { constructor( readonly code: AppErrorCode, message: string, ) { super(message); } } // --------------------------------------------------------------------------- // common/filters/app-error.filter.ts // Register once in AppModule: { provide: APP_FILTER, useClass: AppErrorFilter } // --------------------------------------------------------------------------- import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common'; import type { Response } from 'express'; const STATUS_BY_CODE: Record = { not_found: 404, forbidden: 403, conflict: 409, }; @Catch(AppError) export class AppErrorFilter implements ExceptionFilter { catch(error: AppError, host: ArgumentsHost): void { host .switchToHttp() .getResponse() .status(STATUS_BY_CODE[error.code]) .json({ code: error.code, message: error.message }); } } // --------------------------------------------------------------------------- // dto/resource.dto.ts // --------------------------------------------------------------------------- import { z } from 'zod'; export const CreateResourceSchema = z.object({ title: z.string().min(1, 'Title is required').max(200), description: z.string().max(1000).optional(), status: z.enum(['active', 'archived']).default('active'), }); export const UpdateResourceSchema = CreateResourceSchema.partial(); export const ResourceQuerySchema = z.object({ page: z.coerce.number().int().min(1).default(1), limit: z.coerce.number().int().min(1).max(100).default(20), search: z.string().optional(), status: z.enum(['active', 'archived']).optional(), }); export type CreateResourceDto = z.infer; export type UpdateResourceDto = z.infer; export type ResourceQueryDto = z.infer; // --------------------------------------------------------------------------- // resources.repository.ts // --------------------------------------------------------------------------- import { Injectable } from '@nestjs/common'; import { PrismaService } from '../../prisma/prisma.service'; import { Prisma, Resource } from '../../generated/prisma/client'; export interface PaginatedResult { items: T[]; total: number; page: number; limit: number; totalPages: number; } @Injectable() export class ResourcesRepository { constructor(private readonly prisma: PrismaService) {} async findPaginated( userId: string, query: ResourceQueryDto, ): Promise> { const { page, limit, search, status } = query; const where: Prisma.ResourceWhereInput = { userId, deletedAt: null, ...(status && { status }), ...(search && { title: { contains: search, mode: 'insensitive' }, }), }; // Page and count in one transaction const [items, total] = await this.prisma.$transaction([ this.prisma.resource.findMany({ where, skip: (page - 1) * limit, take: limit, orderBy: { createdAt: 'desc' }, }), this.prisma.resource.count({ where }), ]); return { items, total, page, limit, totalPages: Math.ceil(total / limit), }; } async findOne(id: string, userId: string): Promise { return this.prisma.resource.findFirst({ where: { id, userId, deletedAt: null }, }); } async create( userId: string, data: CreateResourceDto, ): Promise { return this.prisma.resource.create({ data: { ...data, user: { connect: { id: userId } }, }, }); } async update(id: string, data: UpdateResourceDto): Promise { return this.prisma.resource.update({ where: { id }, data, }); } async softDelete(id: string): Promise { return this.prisma.resource.update({ where: { id }, data: { deletedAt: new Date() }, }); } } // --------------------------------------------------------------------------- // resources.service.ts // --------------------------------------------------------------------------- @Injectable() export class ResourcesService { constructor(private readonly resourcesRepo: ResourcesRepository) {} async findAll( userId: string, query: ResourceQueryDto, ): Promise> { return this.resourcesRepo.findPaginated(userId, query); } async findOne(id: string, userId: string): Promise { const resource = await this.resourcesRepo.findOne(id, userId); if (!resource) { throw new AppError('not_found', `Resource ${id} not found`); } // Guard against cross-user access (belt-and-suspenders; repo already filters) if (resource.userId !== userId) { throw new AppError('forbidden', 'Access denied'); } return resource; } async create(dto: CreateResourceDto, userId: string): Promise { return this.resourcesRepo.create(userId, dto); } async update( id: string, dto: UpdateResourceDto, userId: string, ): Promise { await this.findOne(id, userId); // ownership check throws not_found/forbidden return this.resourcesRepo.update(id, dto); } async remove(id: string, userId: string): Promise { await this.findOne(id, userId); // ownership check await this.resourcesRepo.softDelete(id); } } // --------------------------------------------------------------------------- // common/decorators/current-user.decorator.ts // --------------------------------------------------------------------------- import { createParamDecorator, ExecutionContext } from '@nestjs/common'; import { User } from '../../generated/prisma/client'; export const CurrentUser = createParamDecorator( (_data: unknown, ctx: ExecutionContext): User => { const request = ctx.switchToHttp().getRequest<{ user: User }>(); return request.user; }, ); // --------------------------------------------------------------------------- // resources.controller.ts (Router layer: NO business logic here) // --------------------------------------------------------------------------- import { Controller, Get, Post, Patch, Delete, Body, Param, Query, ParseUUIDPipe, HttpCode, HttpStatus, UseGuards, } from '@nestjs/common'; import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'; import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe'; @Controller('api/resources') @UseGuards(JwtAuthGuard) export class ResourcesController { constructor(private readonly resourcesService: ResourcesService) {} /** * GET /api/resources?page=1&limit=20&search=foo&status=active */ @Get() findAll( @Query(new ZodValidationPipe(ResourceQuerySchema)) query: ResourceQueryDto, @CurrentUser() user: User, ): Promise> { return this.resourcesService.findAll(user.id, query); } /** * GET /api/resources/:id */ @Get(':id') findOne( @Param('id', ParseUUIDPipe) id: string, @CurrentUser() user: User, ): Promise { return this.resourcesService.findOne(id, user.id); } /** * POST /api/resources */ @Post() @HttpCode(HttpStatus.CREATED) create( @Body(new ZodValidationPipe(CreateResourceSchema)) dto: CreateResourceDto, @CurrentUser() user: User, ): Promise { return this.resourcesService.create(dto, user.id); } /** * PATCH /api/resources/:id */ @Patch(':id') update( @Param('id', ParseUUIDPipe) id: string, @Body(new ZodValidationPipe(UpdateResourceSchema)) dto: UpdateResourceDto, @CurrentUser() user: User, ): Promise { return this.resourcesService.update(id, dto, user.id); } /** * DELETE /api/resources/:id (soft delete) */ @Delete(':id') @HttpCode(HttpStatus.NO_CONTENT) remove( @Param('id', ParseUUIDPipe) id: string, @CurrentUser() user: User, ): Promise { return this.resourcesService.remove(id, user.id); } } // --------------------------------------------------------------------------- // resources.module.ts // --------------------------------------------------------------------------- import { Module } from '@nestjs/common'; import { PrismaModule } from '../../prisma/prisma.module'; @Module({ imports: [PrismaModule], controllers: [ResourcesController], providers: [ResourcesService, ResourcesRepository], exports: [ResourcesService], }) export class ResourcesModule {} // --------------------------------------------------------------------------- // common/pipes/zod-validation.pipe.ts // --------------------------------------------------------------------------- import { PipeTransform, BadRequestException } from '@nestjs/common'; import { z, ZodType } from 'zod'; export class ZodValidationPipe implements PipeTransform { constructor(private readonly schema: ZodType) {} transform(value: unknown): unknown { const result = this.schema.safeParse(value); if (!result.success) { throw new BadRequestException({ message: 'Validation failed', errors: z.flattenError(result.error), }); } return result.data; } }