Implement group deletion and add group files retrieval
Added logic to delete a group and update the associated files to have a null group reference. Also implemented a method to retrieve files associated with a group using pagination and search functionality.
This commit is contained in:
parent
1fc9185afc
commit
fc2f437556
@ -1,13 +1,12 @@
|
|||||||
import { Injectable } from "@nestjs/common";
|
import { Injectable, InternalServerErrorException, NotFoundException } 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, FilesTable } from 'apps/backend/src/app/db/schema';
|
||||||
import { ilike } from "drizzle-orm";
|
import { and, eq, 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)
|
|
||||||
async getGroupsByName(limit: number, offset: number, search: string) {
|
async getGroupsByName(limit: number, offset: number, search: string) {
|
||||||
return await this.database
|
return await this.database
|
||||||
.use()
|
.use()
|
||||||
@ -34,13 +33,48 @@ export class GroupsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//TODO a method to delete a group and place the associated file at a null group reference
|
//TODO a method to delete a group and place the associated file at a null group reference
|
||||||
async deleteGroup(groupId: string) {}
|
async deleteGroup(groupId: string) {
|
||||||
|
const groupInDb = await this.database.use()
|
||||||
//TODO a method to get the files of a group in the database by a specific search with limit, offset and a search field (can be blank)
|
.select()
|
||||||
|
.from(FilesGroupTable)
|
||||||
|
.where(eq(FilesGroupTable.uuid, groupId))
|
||||||
|
.prepare("findGroupById")
|
||||||
|
.execute();
|
||||||
|
if (groupInDb.length === 0) throw new NotFoundException("Group not found");
|
||||||
|
// Replace entry by null
|
||||||
|
await this.database
|
||||||
|
.use()
|
||||||
|
.update(FilesTable)
|
||||||
|
// @ts-ignore
|
||||||
|
.set({ groupId: null })
|
||||||
|
.where(eq(FilesTable.groupId, groupId))
|
||||||
|
.prepare("updateFilesGroupReference")
|
||||||
|
.execute();
|
||||||
|
try {
|
||||||
|
await this.database
|
||||||
|
.use()
|
||||||
|
.delete(FilesGroupTable)
|
||||||
|
.where(eq(FilesGroupTable.uuid, groupId))
|
||||||
|
return true
|
||||||
|
} catch (e) {
|
||||||
|
throw new InternalServerErrorException("Error while deleting group");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async findFilesForGroup(
|
async findFilesForGroup(
|
||||||
limit: number,
|
limit: number,
|
||||||
offset: number,
|
offset: number,
|
||||||
searchField: string,
|
searchField: string,
|
||||||
groupId: string,
|
groupId: string,
|
||||||
) {}
|
) {
|
||||||
|
return await this.database
|
||||||
|
.use()
|
||||||
|
.select()
|
||||||
|
.from(FilesTable)
|
||||||
|
.where(and(eq(FilesTable.groupId, groupId), ilike(FilesTable.fileName, String(`%${searchField}%`)) ) )
|
||||||
|
.limit(limit)
|
||||||
|
.offset(offset)
|
||||||
|
.prepare('findFilesInGroup')
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user