Compare commits
6 Commits
16487e5985
...
18a5999334
| Author | SHA1 | Date | |
|---|---|---|---|
|
18a5999334
|
|||
|
877e13043d
|
|||
|
6bb9510356
|
|||
|
a5f54e165b
|
|||
|
b29b188912
|
|||
|
c7e1a949a2
|
@@ -136,14 +136,14 @@ export class FilesController {
|
|||||||
@HttpCode(HttpStatus.FOUND)
|
@HttpCode(HttpStatus.FOUND)
|
||||||
@Get("types")
|
@Get("types")
|
||||||
async getTypes() {
|
async getTypes() {
|
||||||
//TODO
|
return await this.filesService.getAllFilesTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
@HttpCode(HttpStatus.CREATED)
|
@HttpCode(HttpStatus.CREATED)
|
||||||
@UseGuards(AdminGuard)
|
@UseGuards(AdminGuard)
|
||||||
@Post("types/new")
|
@Post("types/new")
|
||||||
async newType(@Body() body: CreateFileTypeDto) {
|
async newType(@Body() body: CreateFileTypeDto) {
|
||||||
//TODO
|
return await this.filesService.createFileType(body.name, body.mime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HttpCode(HttpStatus.ACCEPTED)
|
@HttpCode(HttpStatus.ACCEPTED)
|
||||||
|
|||||||
@@ -18,5 +18,11 @@ export class CreateFilesDto {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class CreateFileTypeDto {
|
export class CreateFileTypeDto {
|
||||||
//TODO
|
@MaxLength(128)
|
||||||
|
@MinLength(3)
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
@MaxLength(64)
|
||||||
|
@MinLength(4)
|
||||||
|
mime: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
BadRequestException,
|
||||||
Injectable,
|
Injectable,
|
||||||
InternalServerErrorException,
|
InternalServerErrorException,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
@@ -246,4 +247,101 @@ export class FilesService {
|
|||||||
}
|
}
|
||||||
return inserted[0];
|
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",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
},
|
},
|
||||||
"files": {
|
"files": {
|
||||||
"include": [
|
"include": [
|
||||||
"./apps/**/*.ts"
|
"./apps/**/src/**/*.ts"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"vcs": {
|
"vcs": {
|
||||||
|
|||||||
Reference in New Issue
Block a user