Files
towercontrol/src/app/api/notes/route.ts
Julien Froidefond 7811453e02 feat(Notes): associate notes with tasks and enhance note management
- Added taskId field to Note model for associating notes with tasks.
- Updated API routes to handle taskId in note creation and updates.
- Enhanced NotesPageClient to manage task associations within notes.
- Integrated task selection in MarkdownEditor for better user experience.
- Updated NotesService to map task data correctly when retrieving notes.
2025-10-10 08:05:32 +02:00

75 lines
1.8 KiB
TypeScript

import { NextResponse } from 'next/server';
import { notesService } from '@/services/notes';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
/**
* API route pour récupérer toutes les notes de l'utilisateur connecté
*/
export async function GET(request: Request) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { searchParams } = new URL(request.url);
const search = searchParams.get('search');
let notes;
if (search) {
notes = await notesService.searchNotes(session.user.id, search);
} else {
notes = await notesService.getNotes(session.user.id);
}
return NextResponse.json({ notes });
} catch (error) {
console.error('Error fetching notes:', error);
return NextResponse.json(
{ error: 'Failed to fetch notes' },
{ status: 500 }
);
}
}
/**
* API route pour créer une nouvelle note
*/
export async function POST(request: Request) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const body = await request.json();
const { title, content, taskId, tags } = body;
if (!title || !content) {
return NextResponse.json(
{ error: 'Title and content are required' },
{ status: 400 }
);
}
const note = await notesService.createNote({
title,
content,
userId: session.user.id,
taskId,
tags,
});
return NextResponse.json({ note }, { status: 201 });
} catch (error) {
console.error('Error creating note:', error);
return NextResponse.json(
{ error: 'Failed to create note' },
{ status: 500 }
);
}
}