feat(m7): markdown notes with CRUD, preview, search, tests, e2e

- ProjectNoteRepository (CRUD, soft-delete, search title/body/tags,
  pinned-first ordering, index usage) + NoteService (membership permission,
  author/admin edit/delete, note_updated notification to other members,
  note_created/note_updated activity logs, pin/tags)
- APIs: notes list/create/detail/edit/delete
- Screens: notes list + NoteForm, note detail + NoteEditor + MarkdownBody
  preview, ProjectNav メモ link
- Unit tests (ProjectNoteRepository, NoteService) + e2e markdown-notes.spec
This commit is contained in:
Ken Yasue
2026-06-25 01:36:10 +02:00
parent fb61a7f592
commit e45aea8e27
13 changed files with 1116 additions and 1 deletions

View File

@ -0,0 +1,69 @@
import { NextRequest, NextResponse } from 'next/server';
import { getCurrentUser } from '@/lib/auth/getCurrentUser';
import { createNoteService } from '@/lib/api/services';
import { UnauthorizedError } from '@/lib/errors';
import { handleApiError, jsonError } from '@/lib/api/handleError';
export const runtime = 'nodejs';
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<{ projectId: string; noteId: string }> }
) {
const user = await getCurrentUser();
if (!user) return handleApiError(new UnauthorizedError());
const { noteId } = await params;
const service = createNoteService();
try {
const note = service.getNote(user.id, Number(noteId));
return NextResponse.json({ note });
} catch (error) {
return handleApiError(error);
}
}
export async function PATCH(
request: NextRequest,
{ params }: { params: Promise<{ projectId: string; noteId: string }> }
) {
const user = await getCurrentUser();
if (!user) return handleApiError(new UnauthorizedError());
const { noteId } = await params;
let body: Record<string, unknown>;
try {
body = (await request.json()) as Record<string, unknown>;
} catch {
return jsonError(400, 'リクエスト本文が不正です');
}
const service = createNoteService();
try {
const note = service.updateNote(user.id, Number(noteId), {
title: typeof body.title === 'string' ? body.title : undefined,
bodyMd: typeof body.bodyMd === 'string' ? body.bodyMd : undefined,
tags: typeof body.tags === 'string' ? body.tags : undefined,
isPinned: typeof body.isPinned === 'number' ? body.isPinned : undefined,
});
return NextResponse.json({ note });
} catch (error) {
return handleApiError(error);
}
}
export async function DELETE(
_request: NextRequest,
{ params }: { params: Promise<{ projectId: string; noteId: string }> }
) {
const user = await getCurrentUser();
if (!user) return handleApiError(new UnauthorizedError());
const { noteId } = await params;
const service = createNoteService();
try {
service.deleteNote(user.id, Number(noteId));
return NextResponse.json({ ok: true });
} catch (error) {
return handleApiError(error);
}
}

View File

@ -0,0 +1,54 @@
import { NextRequest, NextResponse } from 'next/server';
import { getCurrentUser } from '@/lib/auth/getCurrentUser';
import { createNoteService } from '@/lib/api/services';
import { UnauthorizedError } from '@/lib/errors';
import { handleApiError, jsonError } from '@/lib/api/handleError';
export const runtime = 'nodejs';
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ projectId: string }> }
) {
const user = await getCurrentUser();
if (!user) return handleApiError(new UnauthorizedError());
const { projectId } = await params;
const page = Number(request.nextUrl.searchParams.get('page') ?? '1') || 1;
const search = request.nextUrl.searchParams.get('q') ?? undefined;
const service = createNoteService();
try {
return NextResponse.json(
service.listNotes(user.id, Number(projectId), { page, search })
);
} catch (error) {
return handleApiError(error);
}
}
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ projectId: string }> }
) {
const user = await getCurrentUser();
if (!user) return handleApiError(new UnauthorizedError());
const { projectId } = await params;
let body: Record<string, unknown>;
try {
body = (await request.json()) as Record<string, unknown>;
} catch {
return jsonError(400, 'リクエスト本文が不正です');
}
const service = createNoteService();
try {
const note = service.createNote(user.id, Number(projectId), {
title: String(body.title ?? ''),
bodyMd: String(body.bodyMd ?? ''),
tags: typeof body.tags === 'string' ? body.tags : undefined,
});
return NextResponse.json({ note }, { status: 201 });
} catch (error) {
return handleApiError(error);
}
}

View File

@ -0,0 +1,63 @@
import { redirect } from 'next/navigation';
import { getCurrentUser, toPublicUser } from '@/lib/auth/getCurrentUser';
import { createNoteService } from '@/lib/api/services';
import { Header } from '@/components/layout/Header';
import { ProjectNav } from '@/components/layout/ProjectNav';
import { MarkdownBody } from '@/components/board/MarkdownBody';
import { NoteEditor } from '@/components/notes/NoteEditor';
import { ForbiddenError, NotFoundError } from '@/lib/errors';
import type { ProjectNote } from '@/lib/types';
export const dynamic = 'force-dynamic';
export default async function NoteDetailPage({
params,
}: {
params: Promise<{ projectId: string; noteId: string }>;
}) {
const user = await getCurrentUser();
if (!user) redirect('/login');
const { projectId, noteId } = await params;
const noteService = createNoteService();
let note: ProjectNote;
try {
note = noteService.getNote(user.id, Number(noteId));
} catch (error) {
if (error instanceof ForbiddenError || error instanceof NotFoundError) {
redirect(`/projects/${projectId}/notes`);
}
throw error;
}
return (
<div className="min-h-screen bg-gray-50">
<Header user={toPublicUser(user)} />
<ProjectNav projectId={Number(projectId)} active="notes" />
<main className="mx-auto max-w-3xl space-y-6 p-6">
<a
href={`/projects/${projectId}/notes`}
className="text-sm text-blue-600 hover:underline"
>
</a>
<div className="rounded-lg border bg-white p-6 shadow-sm">
<h1 className="text-2xl font-bold">
{note.isPinned === 1 && '📌 '}
{note.title}
</h1>
{note.tags && (
<p className="mt-1 text-xs text-gray-500">{note.tags}</p>
)}
<div className="mt-4">
<MarkdownBody bodyMd={note.bodyMd} />
</div>
<p className="mt-4 text-xs text-gray-400">
: {note.createdAt} / : {note.updatedAt}
</p>
</div>
<NoteEditor projectId={Number(projectId)} note={note} />
</main>
</div>
);
}

View File

@ -0,0 +1,74 @@
import { redirect } from 'next/navigation';
import { getCurrentUser, toPublicUser } from '@/lib/auth/getCurrentUser';
import { createNoteService, createProjectService } from '@/lib/api/services';
import { Header } from '@/components/layout/Header';
import { ProjectNav } from '@/components/layout/ProjectNav';
import { NoteForm } from '@/components/notes/NoteForm';
import { ForbiddenError, NotFoundError } from '@/lib/errors';
export const dynamic = 'force-dynamic';
export default async function NotesPage({
params,
searchParams,
}: {
params: Promise<{ projectId: string }>;
searchParams: Promise<{ q?: string; page?: string }>;
}) {
const user = await getCurrentUser();
if (!user) redirect('/login');
const { projectId } = await params;
const { q, page } = await searchParams;
const projectService = createProjectService();
let project: Awaited<ReturnType<typeof projectService.getProject>>;
try {
project = projectService.getProject(user.id, Number(projectId));
} catch (error) {
if (error instanceof ForbiddenError || error instanceof NotFoundError) {
redirect('/dashboard');
}
throw error;
}
const noteService = createNoteService();
const { items } = noteService.listNotes(user.id, project.id, {
page: Number(page ?? '1') || 1,
search: q,
});
return (
<div className="min-h-screen bg-gray-50">
<Header user={toPublicUser(user)} />
<ProjectNav projectId={project.id} active="notes" />
<main className="mx-auto max-w-3xl space-y-6 p-6">
<h1 className="text-2xl font-bold">Markdownメモ</h1>
<NoteForm projectId={project.id} />
<section className="space-y-3">
{items.length === 0 ? (
<p className="text-sm text-gray-400"></p>
) : (
items.map((note) => (
<a
key={note.id}
href={`/projects/${project.id}/notes/${note.id}`}
className="block rounded-lg border bg-white p-4 shadow-sm hover:shadow-md"
>
<div className="flex items-center justify-between">
<h3 className="font-semibold text-gray-800">
{note.isPinned === 1 && '📌 '}
{note.title}
</h3>
{note.tags && (
<span className="text-xs text-gray-500">{note.tags}</span>
)}
</div>
<p className="mt-1 text-xs text-gray-400">{note.updatedAt}</p>
</a>
))
)}
</section>
</main>
</div>
);
}