카테고리 관련 DB 테이블 추가

This commit is contained in:
static
2025-01-13 09:10:56 +09:00
parent 9ab107794a
commit e1262506c4
6 changed files with 244 additions and 3 deletions

View File

@@ -1,7 +1,8 @@
import { SqliteError } from "better-sqlite3";
import { and, eq, isNull } from "drizzle-orm";
import db from "./drizzle";
import { IntegrityError } from "./error";
import { directory, directoryLog, file, fileLog, hsk, mek } from "./schema";
import { directory, directoryLog, file, fileLog, fileCategory, hsk, mek } from "./schema";
type DirectoryId = "root" | number;
@@ -237,6 +238,14 @@ export const getAllFilesByParent = async (userId: number, parentId: DirectoryId)
);
};
export const getAllFilesByCategory = async (userId: number, categoryId: number) => {
return await db
.select()
.from(file)
.innerJoin(fileCategory, eq(file.id, fileCategory.fileId))
.where(and(eq(file.userId, userId), eq(fileCategory.categoryId, categoryId)));
};
export const getAllFileIdsByContentHmac = async (
userId: number,
hskVersion: number,
@@ -308,3 +317,46 @@ export const unregisterFile = async (userId: number, fileId: number) => {
}
return files[0].path;
};
export const addFileToCategory = async (fileId: number, categoryId: number) => {
await db.transaction(
async (tx) => {
try {
await tx.insert(fileCategory).values({ fileId, categoryId });
await tx.insert(fileLog).values({
fileId,
timestamp: new Date(),
action: "addToCategory",
categoryId,
});
} catch (e) {
if (e instanceof SqliteError && e.code === "SQLITE_CONSTRAINT_PRIMARYKEY") {
throw new IntegrityError("File already added to category");
}
throw e;
}
},
{ behavior: "exclusive" },
);
};
export const removeFileFromCategory = async (fileId: number, categoryId: number) => {
await db.transaction(
async (tx) => {
const res = await tx
.delete(fileCategory)
.where(and(eq(fileCategory.fileId, fileId), eq(fileCategory.categoryId, categoryId)));
if (res.changes === 0) {
throw new IntegrityError("File not found in category");
}
await tx.insert(fileLog).values({
fileId,
timestamp: new Date(),
action: "removeFromCategory",
categoryId,
});
},
{ behavior: "exclusive" },
);
};