From 289ec098688a0e0ff84e93e5800d853208c3a234 Mon Sep 17 00:00:00 2001 From: Mathis Date: Tue, 8 Oct 2024 13:49:06 +0200 Subject: [PATCH] Add method to search machines with limit and offset Implemented `findMany` method to fetch machines based on a search field, applying limit and offset for pagination. Includes console logs for tracking search parameters and results. --- .../src/app/machines/machines.service.ts | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/backend/src/app/machines/machines.service.ts b/apps/backend/src/app/machines/machines.service.ts index cbbc259..fd7fead 100644 --- a/apps/backend/src/app/machines/machines.service.ts +++ b/apps/backend/src/app/machines/machines.service.ts @@ -1,11 +1,35 @@ import { Injectable } from "@nestjs/common"; import { DbService } from "apps/backend/src/app/db/db.service"; +import { MachinesTable } from "apps/backend/src/app/db/schema"; +import { ilike } from "drizzle-orm"; @Injectable() export class MachinesService { constructor(private readonly database: DbService) {} - //TODO a method to fetch machines in the database by a specific search with limit, offset and a search field (can be blank) + /** + * Finds and returns a list of machines based on the given search parameters. + * + * @param {number} limit - The maximum number of machines to return. + * @param {number} offset - The number of machines to skip before starting to collect the result set. + * @param {string} searchField - The field to search within the machine names. + * + * @return {Promise} A promise that resolves to an array of found machines. + */ + async findMany(limit: number, offset: number, searchField: string) { + console.log(`Searching machines. \nSearch : ${searchField}`); + const machines = await this.database + .use() + .select() + .from(MachinesTable) + .where(ilike(MachinesTable.machineName, String(`%${searchField}%`))) + .limit(limit) + .offset(offset) + .prepare("findMachineByName") + .execute(); + console.log(`Found ${machines.length} machines.`); + return machines; + } //TODO The method to create a machine