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:
@@ -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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user