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.
This commit is contained in:
Mathis H (Avnyr) 2024-10-15 13:47:11 +02:00
parent c7e1a949a2
commit b29b188912
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
2 changed files with 48 additions and 1 deletions

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,44 @@ 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",
);
}
}
public async getAllFilesTypes() {
//TODO
}
public async removeFileType() {
//TODO
}
}