feat: refactor skills API and database schema

- Replaced file-based skill category loading with API calls in the GET and POST methods of the skills route.
- Added new `SkillsService` for handling skill category operations.
- Updated SQL initialization script to create `skill_categories`, `skills`, and `skill_links` tables with appropriate relationships.
- Enhanced `ApiClient` with methods for loading skill categories and creating new skills, improving API interaction.
- Introduced error handling for skill category creation and loading processes.
This commit is contained in:
Julien Froidefond
2025-08-21 09:55:35 +02:00
parent 345ff5baa0
commit 72b653de19
10 changed files with 715 additions and 28 deletions

View File

@@ -0,0 +1,55 @@
import { NextResponse } from "next/server";
import { SkillsService } from "@/services";
interface RouteParams {
params: {
categoryId: string;
};
}
export async function GET(request: Request, { params }: RouteParams) {
try {
const skills = await SkillsService.getSkillsByCategory(params.categoryId);
return NextResponse.json(skills);
} catch (error) {
console.error("Error loading skills by category:", error);
return NextResponse.json(
{ error: "Failed to load skills by category" },
{ status: 500 }
);
}
}
export async function POST(request: Request, { params }: RouteParams) {
try {
const skillData: {
id: string;
name: string;
description: string;
icon?: string;
links: string[];
} = await request.json();
// Validate required fields
if (!skillData.id || !skillData.name || !skillData.description) {
return NextResponse.json(
{ error: "Missing required fields: id, name, description" },
{ status: 400 }
);
}
await SkillsService.createSkill({
...skillData,
categoryId: params.categoryId,
links: skillData.links || [],
});
return NextResponse.json({ success: true }, { status: 201 });
} catch (error) {
console.error("Error creating skill:", error);
return NextResponse.json(
{ error: "Failed to create skill" },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,52 @@
import { NextResponse } from "next/server";
import { SkillsService } from "@/services";
import { loadSkillCategoriesFromFiles } from "@/lib/skill-file-loader";
export async function POST() {
try {
console.log("🚀 Starting skills migration via API...");
// Load all skill categories from JSON files
const skillCategories = loadSkillCategoriesFromFiles();
console.log(`📊 Found ${skillCategories.length} categories`);
const totalSkills = skillCategories.reduce(
(sum, cat) => sum + cat.skills.length,
0
);
console.log(`🎯 Total skills to migrate: ${totalSkills}`);
// Bulk insert into database
await SkillsService.bulkInsertSkillsFromJSON(skillCategories);
console.log("✅ Skills migration completed successfully!");
// Verify the migration
const categoriesFromDb = await SkillsService.getSkillCategories();
const totalSkillsInDb = categoriesFromDb.reduce(
(sum, cat) => sum + cat.skills.length,
0
);
return NextResponse.json({
success: true,
message: "Skills migration completed successfully",
stats: {
categoriesMigrated: skillCategories.length,
skillsMigrated: totalSkills,
categoriesInDb: categoriesFromDb.length,
skillsInDb: totalSkillsInDb,
},
});
} catch (error) {
console.error("❌ Migration failed:", error);
return NextResponse.json(
{
error: "Failed to migrate skills",
details: error instanceof Error ? error.message : "Unknown error",
},
{ status: 500 }
);
}
}

View File

@@ -1,35 +1,10 @@
import { NextResponse } from "next/server";
import fs from "fs";
import path from "path";
import { SkillsService } from "@/services";
import { SkillCategory } from "@/lib/types";
export async function GET() {
try {
const dataDir = path.join(process.cwd(), "data", "skills");
const categories = [
"frontend",
"backend",
"devops",
"mobile",
"data",
"cloud",
"security",
"design",
];
const skillCategories: SkillCategory[] = [];
for (const category of categories) {
const filePath = path.join(dataDir, `${category}.json`);
if (fs.existsSync(filePath)) {
const fileContent = fs.readFileSync(filePath, "utf-8");
const data = JSON.parse(fileContent);
skillCategories.push(data);
}
}
const skillCategories = await SkillsService.getSkillCategories();
return NextResponse.json(skillCategories);
} catch (error) {
console.error("Error loading skills:", error);
@@ -39,3 +14,27 @@ export async function GET() {
);
}
}
export async function POST(request: Request) {
try {
const categoryData: { id: string; name: string; icon: string } =
await request.json();
// Validate required fields
if (!categoryData.id || !categoryData.name || !categoryData.icon) {
return NextResponse.json(
{ error: "Missing required fields: id, name, icon" },
{ status: 400 }
);
}
await SkillsService.createSkillCategory(categoryData);
return NextResponse.json({ success: true }, { status: 201 });
} catch (error) {
console.error("Error creating skill category:", error);
return NextResponse.json(
{ error: "Failed to create skill category" },
{ status: 500 }
);
}
}