feat: impl file rename

This commit is contained in:
2025-09-18 00:14:16 +00:00
parent 2f181cef45
commit 9fbd5e678a
15 changed files with 377 additions and 393 deletions

View File

@@ -0,0 +1,36 @@
import type { Id } from "../_generated/dataModel"
import type { AuthenticatedMutationCtx } from "../functions"
import * as Err from "./error"
export async function renameFile(
ctx: AuthenticatedMutationCtx,
{
directoryId,
itemId,
newName,
}: {
directoryId?: Id<"directories">
itemId: Id<"files">
newName: string
},
) {
const existing = await ctx.db
.query("files")
.withIndex("uniqueFileInDirectory", (q) =>
q
.eq("userId", ctx.user._id)
.eq("directoryId", directoryId)
.eq("name", newName)
.eq("deletedAt", undefined),
)
.first()
if (existing) {
throw Err.create(
Err.Code.FileExists,
`File with name ${newName} already exists in ${directoryId ? `directory ${directoryId}` : "root"}`,
)
}
await ctx.db.patch(itemId, { name: newName })
}