refactor file model to use UUID for userId and update file service methods accordingly
This commit is contained in:
@@ -2,7 +2,7 @@ import mongoose from "mongoose";
|
||||
|
||||
export interface File {
|
||||
_id: mongoose.Types.ObjectId;
|
||||
userId: mongoose.Types.ObjectId;
|
||||
userId: string; // UUID des Benutzers statt MongoDB ObjectId
|
||||
objectKey: string;
|
||||
originalName: string;
|
||||
mimeType: string;
|
||||
@@ -13,8 +13,7 @@ export interface File {
|
||||
const fileSchema = new mongoose.Schema<File>(
|
||||
{
|
||||
userId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "User",
|
||||
type: String,
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import mongoose from "mongoose";
|
||||
import { FileModel, File } from "../../db/models/file";
|
||||
|
||||
export class FileService {
|
||||
@@ -21,7 +20,7 @@ export class FileService {
|
||||
size: number
|
||||
): Promise<File> {
|
||||
const fileRecord = new FileModel({
|
||||
userId: new mongoose.Types.ObjectId(userId),
|
||||
userId,
|
||||
objectKey,
|
||||
originalName,
|
||||
mimeType,
|
||||
@@ -33,9 +32,7 @@ export class FileService {
|
||||
}
|
||||
|
||||
async getFilesByUserId(userId: string): Promise<File[]> {
|
||||
return FileModel.find({ userId: new mongoose.Types.ObjectId(userId) })
|
||||
.sort({ uploadedAt: -1 })
|
||||
.exec();
|
||||
return FileModel.find({ userId }).sort({ uploadedAt: -1 }).exec();
|
||||
}
|
||||
|
||||
async getFileByObjectKey(objectKey: string): Promise<File | null> {
|
||||
@@ -48,12 +45,17 @@ export class FileService {
|
||||
}
|
||||
|
||||
async isFileDuplicate(originalName: string, userId: string): Promise<boolean> {
|
||||
try {
|
||||
const count = await FileModel.countDocuments({
|
||||
userId: new mongoose.Types.ObjectId(userId),
|
||||
userId,
|
||||
originalName: originalName,
|
||||
});
|
||||
|
||||
return count > 0;
|
||||
} catch (error) {
|
||||
console.error(`Error in isFileDuplicate: ${error}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async updateObjectKey(fileId: string, objectKey: string): Promise<File | null> {
|
||||
|
||||
Reference in New Issue
Block a user