Refactor getGroupsByName to streamline database call

Replaced the variable assignment and logging with a streamlined return statement for better code readability and performance. This change improves the method's efficiency by reducing unnecessary steps and directly returning the result.
This commit is contained in:
Mathis H (Avnyr) 2024-10-03 13:52:45 +02:00
parent b2d084af4a
commit 221410dfb0
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -1,24 +1,23 @@
import { Injectable } from "@nestjs/common"; import { Injectable } from '@nestjs/common';
import { DbService } from 'apps/backend/src/app/db/db.service'; import { DbService } from 'apps/backend/src/app/db/db.service';
import { FilesGroupTable } from 'apps/backend/src/app/db/schema'; import { FilesGroupTable } from 'apps/backend/src/app/db/schema';
import { ilike } from 'drizzle-orm'; import { ilike } from 'drizzle-orm';
@Injectable() @Injectable()
export class GroupsService { export class GroupsService {
constructor(private readonly database: DbService) {} constructor(private readonly database: DbService) {}
//TODO a method to fetch groups in the database by a specific search with limit, offset and a search field (can be blank) //TODO a method to fetch groups in the database by a specific search with limit, offset and a search field (can be blank)
async getGroupsByName(limit: number, offset: number, search: string) { async getGroupsByName(limit: number, offset: number, search: string) {
const result = await this.database.use() return await this.database.use()
.select() .select()
.from(FilesGroupTable) .from(FilesGroupTable)
.where(ilike(FilesGroupTable.groupName, search)) .where(ilike(FilesGroupTable.groupName, search))
.limit(limit) .limit(limit)
.offset(offset) .offset(offset)
.prepare("getGroupsByName") .prepare("getGroupsByName")
.execute() .execute();
console.log(`Found ${result.length} groups for search :\n > "${search}"`)
return result;
} }
//TODO The method to create a group //TODO The method to create a group