Compare commits

...

2 Commits

Author SHA1 Message Date
578b529199
Add CredentialsModule to Groups and Authors modules
This ensures both modules have access to the necessary credentials functionality, improving their capability to handle secure operations. The inclusion of DbModule in AuthorsModule aligns it with the design pattern used in GroupsModule.
2024-10-08 15:41:32 +02:00
2fb2a16c56
Add machine creation endpoint
Implemented a new endpoint to create machines in the system. Added the required DTO for machine creation data validation and integrated it into the controller. Updated service methods to handle machine creation and improved the `findFilesForMachine` method for clarity and functionality.
2024-10-08 15:39:12 +02:00
5 changed files with 59 additions and 11 deletions

View File

@ -1,8 +1,11 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { AuthorsController } from "apps/backend/src/app/authors/authors.controller"; import { AuthorsController } from "apps/backend/src/app/authors/authors.controller";
import { AuthorsService } from "apps/backend/src/app/authors/authors.service"; import { AuthorsService } from "apps/backend/src/app/authors/authors.service";
import { CredentialsModule } from "apps/backend/src/app/credentials/credentials.module";
import { DbModule } from "apps/backend/src/app/db/db.module";
@Module({ @Module({
imports: [DbModule, CredentialsModule],
controllers: [AuthorsController], controllers: [AuthorsController],
providers: [AuthorsService], providers: [AuthorsService],
}) })

View File

@ -1,10 +1,11 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { CredentialsModule } from "apps/backend/src/app/credentials/credentials.module";
import { DbModule } from "../db/db.module"; import { DbModule } from "../db/db.module";
import { GroupsController } from "./groups.controller"; import { GroupsController } from "./groups.controller";
import { GroupsService } from "./groups.service"; import { GroupsService } from "./groups.service";
@Module({ @Module({
imports: [DbModule], imports: [DbModule, CredentialsModule],
controllers: [GroupsController], controllers: [GroupsController],
providers: [GroupsService], providers: [GroupsService],
}) })

View File

@ -1,4 +1,5 @@
import { import {
Body,
Controller, Controller,
DefaultValuePipe, DefaultValuePipe,
Delete, Delete,
@ -13,6 +14,7 @@ import {
UseGuards, UseGuards,
} from "@nestjs/common"; } from "@nestjs/common";
import { AdminGuard } from "apps/backend/src/app/auth/auth.guard"; import { AdminGuard } from "apps/backend/src/app/auth/auth.guard";
import { CreateMachineDto } from "apps/backend/src/app/machines/machines.dto";
import { MachinesService } from "apps/backend/src/app/machines/machines.service"; import { MachinesService } from "apps/backend/src/app/machines/machines.service";
@Controller("machines") @Controller("machines")
@ -29,7 +31,9 @@ export class MachinesController {
//TODO DTO //TODO DTO
@UseGuards(AdminGuard) @UseGuards(AdminGuard)
@Post("new") @Post("new")
async newMachine() {} async newMachine(@Body() body: CreateMachineDto) {
return await this.machineService.create(body.machineName, body.machineType);
}
@UseGuards(AdminGuard) @UseGuards(AdminGuard)
@Delete(":machineId") @Delete(":machineId")

View File

@ -0,0 +1,11 @@
import { MaxLength, MinLength } from "class-validator";
export class CreateMachineDto {
@MaxLength(128)
@MinLength(4)
machineName: string;
@MaxLength(64)
@MinLength(2)
machineType: string;
}

View File

@ -1,7 +1,12 @@
import { Injectable, NotFoundException } 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 { import {
FilesForMachinesTable, FilesForMachinesTable,
FilesTable,
MachinesTable, MachinesTable,
} from "apps/backend/src/app/db/schema"; } from "apps/backend/src/app/db/schema";
import { eq, ilike } from "drizzle-orm"; import { eq, ilike } from "drizzle-orm";
@ -35,18 +40,38 @@ export class MachinesService {
} }
//TODO The method to create a machine //TODO The method to create a machine
async create(machineName: string, machineType: string) {
try {
const newMachine = await this.database
.use()
.insert(MachinesTable)
.values({
machineName,
machineType,
})
.prepare("createMachine")
.execute();
console.log("Created a new machine.", newMachine);
return newMachine;
} catch (e) {
console.error(e);
throw new InternalServerErrorException(
"Insertion of the new machine failed, see the server console for more information.",
);
}
}
//TODO a method to delete a machine and delete the associated FilesTypeForMachine row //TODO a method to delete a machine and delete the associated FilesTypeForMachine row
/** /**
* Retrieves a list of files associated with a specific machine from the database. * Finds files associated with a specific machine.
* *
* @param {number} limit - The maximum number of files to retrieve. * @param {number} limit - The maximum number of files to return.
* @param {number} offset - The offset from which to start retrieving files. * @param {number} offset - The number of files to skip before starting to return results.
* @param {string} searchField - The specific field to search within the files. * @param {string} searchField - The field to search within for files.
* @param {string} machineId - The unique identifier of the machine. * @param {string} machineId - The ID of the machine to find files for.
* @return {Promise<Array>} A promise that resolves to an array of file records for the machine. * @returns {Promise<Array<Object>>} A promise that resolves to an array of files associated with the specified machine.
* @throws {NotFoundException} If the machine with the given id is not found. * @throws {NotFoundException} If the machine ID is not found.
*/ */
async findFilesForMachine( async findFilesForMachine(
limit: number, limit: number,
@ -66,11 +91,15 @@ export class MachinesService {
return await this.database return await this.database
.use() .use()
.select() .select({
associatedMachine: FilesForMachinesTable.machineId,
data: FilesTable,
})
.from(FilesForMachinesTable) .from(FilesForMachinesTable)
.where(eq(FilesForMachinesTable.machineId, machineId)) .where(eq(FilesForMachinesTable.machineId, machineId))
.limit(limit) .limit(limit)
.offset(offset) .offset(offset)
.leftJoin(FilesTable, eq(FilesTable.uuid, FilesForMachinesTable.fileId))
.prepare("findFilesForMachineId") .prepare("findFilesForMachineId")
.execute(); .execute();
} }