feat: add lastEdited field update

This commit is contained in:
Kirill Siukhin 2025-07-16 19:36:32 +05:00
parent b3d7eb99eb
commit 0e1ff2b415
2 changed files with 124 additions and 51 deletions

View File

@ -3,7 +3,7 @@
import { revalidatePath } from "next/cache";
import { eq } from "drizzle-orm";
import { db } from "@/lib/db";
import { blocksTable, IBlock } from "@/lib/db/schema";
import { blocksTable, IBlock, notesTable } from "@/lib/db/schema";
import { requireAuth } from "./auth";
import { assertNoteOwner } from "./notes";
@ -14,24 +14,31 @@ export async function assertBlockOwner(blockId: string): Promise<boolean> {
where: eq(blocksTable.id, blockId),
with: { note: true },
});
if (!block) {
return false;
}
if (!block) return false;
return block.note.authorId === user.id;
}
export async function createBlock(formData: FormData) {
const noteId = formData.get("noteId") as string;
const isAllowed = await assertNoteOwner(noteId);
if (!isAllowed) return;
const blocks = await getBlocks(noteId);
const lastBlock = blocks.pop();
const order = lastBlock === undefined ? 1 : lastBlock.order + 1;
await db
.insert(blocksTable)
.values({ noteId, order });
await db.transaction(async (tx) => {
await tx
.insert(blocksTable)
.values({ noteId, order });
await tx
.update(notesTable)
.set({ lastEdited: new Date() })
.where(eq(notesTable.id, noteId));
});
revalidatePath("/notes/[id]", "page");
}
@ -43,11 +50,8 @@ async function getBlock(blockId: string): Promise<IBlock | null> {
.select()
.from(blocksTable)
.where(eq(blocksTable.id, blockId));
if (blocks.length === 0) {
return null;
} else {
return blocks[0];
}
return blocks.length === 0 ? null : blocks[0];
}
export async function getBlocks(noteId: string): Promise<IBlock[]> {
@ -62,57 +66,93 @@ export async function getBlocks(noteId: string): Promise<IBlock[]> {
export async function deleteBlock(formData: FormData) {
const blockId = formData.get("blockId") as string;
const isAllowed = await assertBlockOwner(blockId);
if (!isAllowed) return;
await db
.delete(blocksTable)
.where(eq(blocksTable.id, blockId));
const block = await getBlock(blockId);
if (!block) return;
await db.transaction(async (tx) => {
await tx
.delete(blocksTable)
.where(eq(blocksTable.id, blockId));
await tx
.update(notesTable)
.set({ lastEdited: new Date() })
.where(eq(notesTable.id, block.noteId));
});
revalidatePath("/notes/[id]", "page");
}
export async function changeLock(formData: FormData) {
const blockId = formData.get("blockId") as string;
const isLocked = formData.get("isLocked") === null ? false : true;
const isAllowed = await assertBlockOwner(blockId);
if (!isAllowed) return;
await db
.update(blocksTable)
.set({ isLocked })
.where(eq(blocksTable.id, blockId));
const block = await getBlock(blockId);
if (!block) return;
await db.transaction(async (tx) => {
await tx
.update(blocksTable)
.set({ isLocked })
.where(eq(blocksTable.id, blockId));
await tx
.update(notesTable)
.set({ lastEdited: new Date() })
.where(eq(notesTable.id, block.noteId));
});
revalidatePath("/notes/[id]", "page");
}
export async function addLine(formData: FormData) {
const blockId = formData.get("blockId") as string;
const isAllowed = await assertBlockOwner(blockId);
if (!isAllowed) return;
const blocks = await db
.select()
.from(blocksTable)
.where(eq(blocksTable.id, blockId));
if (blocks.length === 0) {
return;
}
const block = blocks[0];
await db
.update(blocksTable)
.set({ lines: [...block.lines, ""] })
.where(eq(blocksTable.id, blockId));
const block = await getBlock(blockId);
if (!block) return;
await db.transaction(async (tx) => {
await tx
.update(blocksTable)
.set({ lines: [...block.lines, ""] })
.where(eq(blocksTable.id, blockId));
await tx
.update(notesTable)
.set({ lastEdited: new Date() })
.where(eq(notesTable.id, block.noteId));
});
revalidatePath("/notes/[id]", "page");
}
export async function deleteLine(formData: FormData) {
const blockId = formData.get("blockId") as string;
const block = await getBlock(blockId);
const isAllowed = await assertBlockOwner(blockId);
if (!isAllowed) return;
if (!block) {
return;
}
await db
.update(blocksTable)
.set({ lines: block.lines.slice(0, -1) })
.where(eq(blocksTable.id, blockId));
const block = await getBlock(blockId);
if (!block) return;
await db.transaction(async (tx) => {
await tx
.update(blocksTable)
.set({ lines: block.lines.slice(0, -1) })
.where(eq(blocksTable.id, blockId));
await tx
.update(notesTable)
.set({ lastEdited: new Date() })
.where(eq(notesTable.id, block.noteId));
});
revalidatePath("/notes/[id]", "page");
}
@ -120,6 +160,7 @@ export async function moveUp(formData: FormData) {
const blockId = formData.get("blockId") as string;
const isAllowed = await assertBlockOwner(blockId);
if (!isAllowed) return;
const block = await getBlock(blockId);
if (!block) return;
@ -139,6 +180,10 @@ export async function moveUp(formData: FormData) {
.update(blocksTable)
.set({ order: block.order })
.where(eq(blocksTable.id, prevBlock.id));
await tx
.update(notesTable)
.set({ lastEdited: new Date() })
.where(eq(notesTable.id, block.noteId));
});
revalidatePath("/notes/[id]", "page");
@ -148,6 +193,7 @@ export async function moveDown(formData: FormData) {
const blockId = formData.get("blockId") as string;
const isAllowed = await assertBlockOwner(blockId);
if (!isAllowed) return;
const block = await getBlock(blockId);
if (!block) return;
@ -167,6 +213,10 @@ export async function moveDown(formData: FormData) {
.update(blocksTable)
.set({ order: block.order })
.where(eq(blocksTable.id, nextBlock.id));
await tx
.update(notesTable)
.set({ lastEdited: new Date() })
.where(eq(notesTable.id, block.noteId));
});
revalidatePath("/notes/[id]", "page");
@ -175,17 +225,37 @@ export async function moveDown(formData: FormData) {
export async function setLines(blockId: string, lines: string[]) {
const isAllowed = await assertBlockOwner(blockId);
if (!isAllowed) return;
await db
.update(blocksTable)
.set({ lines })
.where(eq(blocksTable.id, blockId));
const block = await getBlock(blockId);
if (!block) return;
await db.transaction(async (tx) => {
await tx
.update(blocksTable)
.set({ lines })
.where(eq(blocksTable.id, blockId));
await tx
.update(notesTable)
.set({ lastEdited: new Date() })
.where(eq(notesTable.id, block.noteId));
});
}
export async function setTag(blockId: string, tag: string) {
const isAllowed = await assertBlockOwner(blockId);
if (!isAllowed) return;
await db
.update(blocksTable)
.set({ tag })
.where(eq(blocksTable.id, blockId));
const block = await getBlock(blockId);
if (!block) return;
await db.transaction(async (tx) => {
await tx
.update(blocksTable)
.set({ tag })
.where(eq(blocksTable.id, blockId));
await tx
.update(notesTable)
.set({ lastEdited: new Date() })
.where(eq(notesTable.id, block.noteId));
});
}

View File

@ -13,9 +13,7 @@ export async function assertNoteOwner(noteId: string): Promise<boolean> {
const note = await db.query.notesTable.findFirst({
where: eq(notesTable.id, noteId),
});
if (!note) {
return false;
}
if (!note) return false;
return note.authorId === user.id;
}
@ -61,9 +59,14 @@ export async function deleteNote(formData: FormData) {
export async function setTitle(noteId: string, title: string) {
if (title === "") return;
const isAllowed = await assertNoteOwner(noteId);
if (!isAllowed) return;
await db
.update(notesTable)
.set({ title })
.set({ title, lastEdited: new Date() })
.where(eq(notesTable.id, noteId));
revalidatePath("/notes/[id]", "page");
}