refactor: top level dir + moved route
create a root directory entry in table for each user and move file browser under /directories/$id
This commit is contained in:
@@ -19,6 +19,22 @@ type File = {
|
||||
export type DirectoryItem = Directory | File
|
||||
export type DirectoryItemKind = DirectoryItem["kind"]
|
||||
|
||||
export async function fetchRoot(ctx: AuthenticatedQueryCtx) {
|
||||
return await ctx.db
|
||||
.query("directories")
|
||||
.withIndex("byParentId", (q) =>
|
||||
q.eq("userId", ctx.user._id).eq("parentId", undefined),
|
||||
)
|
||||
.first()
|
||||
}
|
||||
|
||||
export async function fetch(
|
||||
ctx: AuthenticatedQueryCtx,
|
||||
{ directoryId }: { directoryId: Id<"directories"> },
|
||||
) {
|
||||
return await ctx.db.get(directoryId)
|
||||
}
|
||||
|
||||
export async function fetchContent(
|
||||
ctx: AuthenticatedQueryCtx,
|
||||
{
|
||||
@@ -76,17 +92,14 @@ export async function fetchContent(
|
||||
|
||||
export async function create(
|
||||
ctx: AuthenticatedMutationCtx,
|
||||
{ name, parentId }: { name: string; parentId?: Id<"directories"> },
|
||||
{ name, parentId }: { name: string; parentId: Id<"directories"> },
|
||||
): Promise<Id<"directories">> {
|
||||
let parentDir: Doc<"directories"> | null = null
|
||||
if (parentId) {
|
||||
parentDir = await ctx.db.get(parentId)
|
||||
if (!parentDir) {
|
||||
throw Err.create(
|
||||
Err.Code.DirectoryNotFound,
|
||||
`Parent directory ${parentId} not found`,
|
||||
)
|
||||
}
|
||||
const parentDir = await ctx.db.get(parentId)
|
||||
if (!parentDir) {
|
||||
throw Err.create(
|
||||
Err.Code.DirectoryNotFound,
|
||||
`Parent directory ${parentId} not found`,
|
||||
)
|
||||
}
|
||||
|
||||
const existing = await ctx.db
|
||||
|
@@ -1,4 +1,3 @@
|
||||
import type { Id } from "../_generated/dataModel"
|
||||
import type { MutationCtx, QueryCtx } from "../_generated/server"
|
||||
import type { AuthenticatedMutationCtx } from "../functions"
|
||||
import * as Err from "./error"
|
||||
@@ -40,7 +39,17 @@ export async function userOrThrow(ctx: QueryCtx | MutationCtx) {
|
||||
}
|
||||
|
||||
export async function register(ctx: AuthenticatedMutationCtx) {
|
||||
await ctx.db.insert("users", {
|
||||
jwtSubject: ctx.identity.subject,
|
||||
})
|
||||
const now = new Date().toISOString()
|
||||
await Promise.all([
|
||||
ctx.db.insert("users", {
|
||||
jwtSubject: ctx.identity.subject,
|
||||
}),
|
||||
ctx.db.insert("directories", {
|
||||
name: "",
|
||||
path: "",
|
||||
userId: ctx.user._id,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}),
|
||||
])
|
||||
}
|
||||
|
Reference in New Issue
Block a user