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.
This commit is contained in:
Mathis H (Avnyr) 2024-10-03 13:52:31 +02:00
parent e03d16bdb4
commit b2d084af4a
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
3 changed files with 42 additions and 5 deletions

View File

@ -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"; import { FilesService } from "./files.service";
@Controller("files") @Controller("files")
export class FilesController { export class FilesController {
constructor(private readonly filesService: FilesService) {} constructor(private readonly filesService: FilesService) {}
//TODO POST FILE
@Post('new') @Post('new')
async saveFile() { 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') @Get(':fileId')

View File

@ -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;
}

View File

@ -2,7 +2,7 @@ import { Injectable, NotFoundException, StreamableFile } from '@nestjs/common';
import { DbService } from 'apps/backend/src/app/db/db.service'; import { DbService } from 'apps/backend/src/app/db/db.service';
import { FilesTable } from 'apps/backend/src/app/db/schema'; import { FilesTable } from 'apps/backend/src/app/db/schema';
import { StorageService } from 'apps/backend/src/app/storage/storage.service'; import { StorageService } from 'apps/backend/src/app/storage/storage.service';
import { eq } from 'drizzle-orm'; import { eq, ilike } from 'drizzle-orm';
@Injectable() @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 //TODO save a file
public async set() { public async save() {
} }
} }