From b2d084af4aebfb2bc8332f22872a8cc52e7cbc18 Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 3 Oct 2024 13:52:31 +0200 Subject: [PATCH] Implement search functionality and DTO for file operations Added a search method in FilesService and corresponding endpoint in FilesController to search files with a limit, offset, and search keyword. Introduced CreateFilesDto with validation constraints to structure file creation inputs. --- .../backend/src/app/files/files.controller.ts | 12 ++++++++++-- apps/backend/src/app/files/files.dto.ts | 19 +++++++++++++++++++ apps/backend/src/app/files/files.service.ts | 16 +++++++++++++--- 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 apps/backend/src/app/files/files.dto.ts diff --git a/apps/backend/src/app/files/files.controller.ts b/apps/backend/src/app/files/files.controller.ts index 2f8b5b3..faac19d 100644 --- a/apps/backend/src/app/files/files.controller.ts +++ b/apps/backend/src/app/files/files.controller.ts @@ -1,14 +1,22 @@ -import { Controller, Get, Param, Post, StreamableFile } from '@nestjs/common'; +import { Controller, DefaultValuePipe, Get, Param, ParseIntPipe, Post, Query, StreamableFile } from '@nestjs/common'; import { FilesService } from "./files.service"; @Controller("files") export class FilesController { constructor(private readonly filesService: FilesService) {} - //TODO POST FILE @Post('new') async saveFile() { + } + + @Get('find') + async findMany( + @Query("limit", new DefaultValuePipe(20), ParseIntPipe) limit: number, + @Query("offset", new DefaultValuePipe(0), ParseIntPipe) offset: number, + @Query("search", new DefaultValuePipe("")) search: string, + ) { + } @Get(':fileId') diff --git a/apps/backend/src/app/files/files.dto.ts b/apps/backend/src/app/files/files.dto.ts new file mode 100644 index 0000000..8a1c616 --- /dev/null +++ b/apps/backend/src/app/files/files.dto.ts @@ -0,0 +1,19 @@ +import { IsUUID, MaxLength, MinLength } from 'class-validator'; +import { DefaultValuePipe } from '@nestjs/common'; + + +export class CreateFilesDto { + @MaxLength(128) + @MinLength(4) + fileName: string; + + @MaxLength(64) + @MinLength(2) + uploadedBy: string; + + isDocumentation?: boolean; + isRestricted?: boolean; + + @IsUUID() + groupId: string; +} \ No newline at end of file diff --git a/apps/backend/src/app/files/files.service.ts b/apps/backend/src/app/files/files.service.ts index 25d7ca2..929c93f 100644 --- a/apps/backend/src/app/files/files.service.ts +++ b/apps/backend/src/app/files/files.service.ts @@ -2,7 +2,7 @@ import { Injectable, NotFoundException, StreamableFile } from '@nestjs/common'; import { DbService } from 'apps/backend/src/app/db/db.service'; import { FilesTable } from 'apps/backend/src/app/db/schema'; import { StorageService } from 'apps/backend/src/app/storage/storage.service'; -import { eq } from 'drizzle-orm'; +import { eq, ilike } from 'drizzle-orm'; @Injectable() @@ -55,10 +55,20 @@ export class FilesService { }); } - //TODO list the files + public async search(limit: number, offset: number, searchField: string) { + return await this.database + .use() + .select() + .from(FilesTable) + .where(ilike(FilesTable.fileName, String(searchField))) + .limit(limit) + .offset(offset) + .prepare("searchFiles") + .execute(); + } //TODO save a file - public async set() { + public async save() { } }