Compare commits

...

6 Commits

Author SHA1 Message Date
18a5999334 Implement file type removal functionality
Add logic to remove file types by ID, ensuring they are not in use by any files or machines. Handle errors appropriately, throwing specific exceptions for cases where the file type cannot be deleted or an internal error occurs.
2024-10-15 14:09:15 +02:00
877e13043d Implement getAllFilesTypes in files controller
Added return statement to getTypes endpoint in files controller, utilizing the newly updated getAllFilesTypes method from the files service. Also fixed minor formatting issues.
2024-10-15 13:51:53 +02:00
6bb9510356 Add retrieval method for all file types in files service
Implemented the `getAllFilesTypes` method to retrieve all file types from the database. This method uses the database connection to select and return an array of file types, completing the previously marked TODO item.
2024-10-15 13:49:45 +02:00
a5f54e165b Implement file type creation endpoint
Completed the `newType` method in `files.controller.ts` to call `filesService.createFileType` with the provided name and mime type. This enables the creation of new file types through the backend API.
2024-10-15 13:47:51 +02:00
b29b188912 Add createFileType method and DTO validations
Introduced createFileType method to handle file type creation, including format validation and error handling for database operations. Updated CreateFileTypeDto with length constraints on name and mime properties.
2024-10-15 13:47:11 +02:00
c7e1a949a2 Update file inclusion pattern in biome.json
Changed the file inclusion pattern to ensure only TypeScript files in src directories are included. This helps in focusing solely on source files and avoids unintended files from being processed.
2024-10-15 13:47:02 +02:00
4 changed files with 108 additions and 4 deletions

View File

@@ -136,14 +136,14 @@ export class FilesController {
@HttpCode(HttpStatus.FOUND)
@Get("types")
async getTypes() {
//TODO
return await this.filesService.getAllFilesTypes();
}
@HttpCode(HttpStatus.CREATED)
@UseGuards(AdminGuard)
@Post("types/new")
async newType(@Body() body: CreateFileTypeDto) {
//TODO
return await this.filesService.createFileType(body.name, body.mime);
}
@HttpCode(HttpStatus.ACCEPTED)

View File

@@ -18,5 +18,11 @@ export class CreateFilesDto {
}
export class CreateFileTypeDto {
//TODO
@MaxLength(128)
@MinLength(3)
name: string;
@MaxLength(64)
@MinLength(4)
mime: string;
}

View File

@@ -1,4 +1,5 @@
import {
BadRequestException,
Injectable,
InternalServerErrorException,
NotFoundException,
@@ -246,4 +247,101 @@ export class FilesService {
}
return inserted[0];
}
/**
* Creates a new file type in the database.
*
* @param {string} name - The name of the file type.
* @param {string} mime - The MIME type for the file type, expected in `type/subtype` format.
* @return {Promise<Object>} A promise that resolves to the created file type object.
* @throws {BadRequestException} If the MIME type format is invalid.
* @throws {InternalServerErrorException} If an error occurs during the database operation.
*/
public async createFileType(name: string, mime: string) {
if (!/^[\w-]+\/[\w-]+$/.test(mime)) {
throw new BadRequestException("Invalid MIME type format");
}
try {
return await this.database
.use()
.insert(FilesTypesTable)
.values({
typeName: name,
mime: mime,
})
.returning()
.prepare("createFileType")
.execute();
} catch (e) {
console.error(e);
throw new InternalServerErrorException(
"An error occured while creating the file type",
);
}
}
/**
* Retrieves all file types from the database.
*
* @return {Promise<Array>} Promise that resolves to an array of file types.
*/
public async getAllFilesTypes(): Promise<Array<object>> {
return await this.database
.use()
.select()
.from(FilesTypesTable)
.prepare("getAllFilesTypes")
.execute();
}
/**
* Removes a file type by its ID if it is not in use by any files or machines.
*
* @param {string} fileTypeId - The ID of the file type to be removed.
* @return {Promise<object>} - A promise that resolves to the result of the deletion operation.
* @throws {BadRequestException} - If the file type is in use by any files or machines.
* @throws {InternalServerErrorException} - If an error occurs during the deletion process.
*/
public async removeFileType(fileTypeId: string): Promise<object> {
const fileUsingFileType = await this.database
.use()
.select()
.from(FilesTable)
.where(eq(FilesTable.fileType, fileTypeId))
.prepare("checkFileTypeInUseInFiles")
.execute();
if (fileUsingFileType.length > 0) {
throw new BadRequestException(
"This file type is in use by some files and cannot be deleted.",
);
}
const machinesUsingFileType = await this.database
.use()
.select()
.from(FilesTypeForMachine)
.where(eq(FilesTypeForMachine.fileTypeId, fileTypeId))
.prepare("getMachineByFileTypeId")
.execute();
if (machinesUsingFileType.length > 0) {
throw new BadRequestException(
"This file type is in use by some machines and cannot be deleted until there is no machine using this fileType.",
);
}
try {
return await this.database
.use()
.delete(FilesTypesTable)
.where(eq(FilesTypesTable.id, fileTypeId))
.returning()
.prepare("deleteFileTypeById")
.execute();
} catch (e) {
throw new InternalServerErrorException(
"An error occurred while deleting a file type",
);
}
}
}

View File

@@ -5,7 +5,7 @@
},
"files": {
"include": [
"./apps/**/*.ts"
"./apps/**/src/**/*.ts"
]
},
"vcs": {