feat: migrate tag management to server actions
- Completed migration of tag management to server actions by creating `actions/tags.ts` for CRUD operations. - Removed obsolete API routes for tags (`/api/tags`, `/api/tags/[id]`) and updated related components to utilize new server actions. - Updated `TagForm` and `useTags` hook to handle tag creation, updating, and deletion through server actions, improving performance and simplifying client-side logic. - Cleaned up `tags-client.ts` by removing unused types and methods, focusing on validation only. - Marked related TODO items as complete in `TODO.md`.
This commit is contained in:
@@ -36,112 +36,4 @@ export async function GET(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH /api/tags/[id] - Met à jour un tag
|
||||
*/
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
const { name, color, isPinned } = body;
|
||||
|
||||
// Validation
|
||||
if (name !== undefined && (typeof name !== 'string' || !name.trim())) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Le nom du tag doit être une chaîne non vide' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (color !== undefined && (typeof color !== 'string' || !/^#[0-9A-F]{6}$/i.test(color))) {
|
||||
return NextResponse.json(
|
||||
{ error: 'La couleur doit être au format hexadécimal (#RRGGBB)' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const tag = await tagsService.updateTag(id, { name, color, isPinned });
|
||||
|
||||
if (!tag) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Tag non trouvé' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
data: tag,
|
||||
message: 'Tag mis à jour avec succès'
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la mise à jour du tag:', error);
|
||||
|
||||
if (error instanceof Error && error.message.includes('non trouvé')) {
|
||||
return NextResponse.json(
|
||||
{ error: error.message },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
if (error instanceof Error && error.message.includes('existe déjà')) {
|
||||
return NextResponse.json(
|
||||
{ error: error.message },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Erreur lors de la mise à jour du tag',
|
||||
message: error instanceof Error ? error.message : 'Erreur inconnue'
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE /api/tags/[id] - Supprime un tag
|
||||
*/
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
await tagsService.deleteTag(id);
|
||||
|
||||
return NextResponse.json({
|
||||
message: 'Tag supprimé avec succès'
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la suppression du tag:', error);
|
||||
|
||||
if (error instanceof Error && error.message.includes('non trouvé')) {
|
||||
return NextResponse.json(
|
||||
{ error: error.message },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
if (error instanceof Error && error.message.includes('utilisé par')) {
|
||||
return NextResponse.json(
|
||||
{ error: error.message },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Erreur lors de la suppression du tag',
|
||||
message: error instanceof Error ? error.message : 'Erreur inconnue'
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
// PATCH and DELETE routes removed - now handled by server actions in /actions/tags.ts
|
||||
|
||||
@@ -41,60 +41,4 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/tags - Crée un nouveau tag
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { name, color } = body;
|
||||
|
||||
// Validation
|
||||
if (!name || typeof name !== 'string') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Le nom du tag est requis' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!color || typeof color !== 'string') {
|
||||
return NextResponse.json(
|
||||
{ error: 'La couleur du tag est requise' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Validation du format couleur (hex)
|
||||
if (!/^#[0-9A-F]{6}$/i.test(color)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'La couleur doit être au format hexadécimal (#RRGGBB)' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const tag = await tagsService.createTag({ name, color });
|
||||
|
||||
return NextResponse.json({
|
||||
data: tag,
|
||||
message: 'Tag créé avec succès'
|
||||
}, { status: 201 });
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la création du tag:', error);
|
||||
|
||||
if (error instanceof Error && error.message.includes('existe déjà')) {
|
||||
return NextResponse.json(
|
||||
{ error: error.message },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Erreur lors de la création du tag',
|
||||
message: error instanceof Error ? error.message : 'Erreur inconnue'
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
// POST route removed - now handled by server actions in /actions/tags.ts
|
||||
|
||||
Reference in New Issue
Block a user