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,4 +1,11 @@
|
||||
import { UserEvaluation, UserProfile, SkillLevel, Team } from "../lib/types";
|
||||
import {
|
||||
UserEvaluation,
|
||||
UserProfile,
|
||||
SkillLevel,
|
||||
Team,
|
||||
SkillCategory,
|
||||
Skill,
|
||||
} from "../lib/types";
|
||||
|
||||
export class ApiClient {
|
||||
private baseUrl: string;
|
||||
@@ -321,6 +328,131 @@ export class ApiClient {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge toutes les catégories de skills
|
||||
*/
|
||||
async loadSkillCategories(): Promise<SkillCategory[]> {
|
||||
try {
|
||||
const response = await fetch(`${this.baseUrl}/api/skills`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Erreur lors du chargement des catégories de skills");
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Erreur lors du chargement des catégories de skills:",
|
||||
error
|
||||
);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge les skills d'une catégorie
|
||||
*/
|
||||
async loadSkillsByCategory(categoryId: string): Promise<Skill[]> {
|
||||
try {
|
||||
const response = await fetch(`${this.baseUrl}/api/skills/${categoryId}`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Erreur lors du chargement des skills par catégorie");
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Erreur lors du chargement des skills par catégorie:",
|
||||
error
|
||||
);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Migre les skills depuis JSON vers PostgreSQL
|
||||
*/
|
||||
async migrateSkills(): Promise<{
|
||||
success: boolean;
|
||||
stats?: any;
|
||||
error?: string;
|
||||
}> {
|
||||
try {
|
||||
const response = await fetch(`${this.baseUrl}/api/skills/migrate`, {
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Erreur lors de la migration des skills");
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la migration des skills:", error);
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : "Unknown error",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée une nouvelle catégorie de skill
|
||||
*/
|
||||
async createSkillCategory(category: {
|
||||
id: string;
|
||||
name: string;
|
||||
icon: string;
|
||||
}): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(`${this.baseUrl}/api/skills`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(category),
|
||||
});
|
||||
|
||||
return response.ok;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Erreur lors de la création de la catégorie de skill:",
|
||||
error
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée une nouvelle skill
|
||||
*/
|
||||
async createSkill(
|
||||
categoryId: string,
|
||||
skill: {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
icon?: string;
|
||||
links: string[];
|
||||
}
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(`${this.baseUrl}/api/skills/${categoryId}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(skill),
|
||||
});
|
||||
|
||||
return response.ok;
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la création de la skill:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Instance singleton
|
||||
|
||||
@@ -11,5 +11,8 @@ export { EvaluationService, evaluationService } from "./evaluation-service";
|
||||
// Teams services (server-only)
|
||||
export { TeamsService } from "./teams-service";
|
||||
|
||||
// Skills services (server-only)
|
||||
export { SkillsService } from "./skills-service";
|
||||
|
||||
// API client (can be used client-side)
|
||||
export { ApiClient, apiClient } from "./api-client";
|
||||
|
||||
253
services/skills-service.ts
Normal file
253
services/skills-service.ts
Normal file
@@ -0,0 +1,253 @@
|
||||
import { getPool } from "./database";
|
||||
import { SkillCategory, Skill } from "@/lib/types";
|
||||
|
||||
export class SkillsService {
|
||||
/**
|
||||
* Get all skill categories with their skills
|
||||
*/
|
||||
static async getSkillCategories(): Promise<SkillCategory[]> {
|
||||
const pool = getPool();
|
||||
const query = `
|
||||
SELECT
|
||||
sc.id as category_id,
|
||||
sc.name as category_name,
|
||||
sc.icon as category_icon,
|
||||
s.id as skill_id,
|
||||
s.name as skill_name,
|
||||
s.description as skill_description,
|
||||
s.icon as skill_icon,
|
||||
COALESCE(
|
||||
json_agg(
|
||||
sl.url ORDER BY sl.id
|
||||
) FILTER (WHERE sl.url IS NOT NULL),
|
||||
'[]'::json
|
||||
) as skill_links
|
||||
FROM skill_categories sc
|
||||
LEFT JOIN skills s ON sc.id = s.category_id
|
||||
LEFT JOIN skill_links sl ON s.id = sl.skill_id
|
||||
GROUP BY sc.id, sc.name, sc.icon, s.id, s.name, s.description, s.icon
|
||||
ORDER BY sc.name, s.name
|
||||
`;
|
||||
|
||||
try {
|
||||
const result = await pool.query(query);
|
||||
|
||||
// Group by category
|
||||
const categoriesMap = new Map<string, SkillCategory>();
|
||||
|
||||
for (const row of result.rows) {
|
||||
const categoryId = row.category_id;
|
||||
|
||||
if (!categoriesMap.has(categoryId)) {
|
||||
categoriesMap.set(categoryId, {
|
||||
category: row.category_name,
|
||||
icon: row.category_icon,
|
||||
skills: [],
|
||||
});
|
||||
}
|
||||
|
||||
if (row.skill_id) {
|
||||
const category = categoriesMap.get(categoryId)!;
|
||||
category.skills.push({
|
||||
id: row.skill_id,
|
||||
name: row.skill_name,
|
||||
description: row.skill_description,
|
||||
icon: row.skill_icon,
|
||||
links: row.skill_links,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(categoriesMap.values());
|
||||
} catch (error) {
|
||||
console.error("Error fetching skill categories:", error);
|
||||
throw new Error("Failed to fetch skill categories");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get skills by category
|
||||
*/
|
||||
static async getSkillsByCategory(categoryId: string): Promise<Skill[]> {
|
||||
const pool = getPool();
|
||||
const query = `
|
||||
SELECT
|
||||
s.id,
|
||||
s.name,
|
||||
s.description,
|
||||
s.icon,
|
||||
COALESCE(
|
||||
json_agg(sl.url ORDER BY sl.id) FILTER (WHERE sl.url IS NOT NULL),
|
||||
'[]'::json
|
||||
) as links
|
||||
FROM skills s
|
||||
LEFT JOIN skill_links sl ON s.id = sl.skill_id
|
||||
WHERE s.category_id = $1
|
||||
GROUP BY s.id, s.name, s.description, s.icon
|
||||
ORDER BY s.name
|
||||
`;
|
||||
|
||||
try {
|
||||
const result = await pool.query(query, [categoryId]);
|
||||
return result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
description: row.description,
|
||||
icon: row.icon,
|
||||
links: row.links,
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error("Error fetching skills by category:", error);
|
||||
throw new Error("Failed to fetch skills by category");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new skill category
|
||||
*/
|
||||
static async createSkillCategory(category: {
|
||||
id: string;
|
||||
name: string;
|
||||
icon: string;
|
||||
}): Promise<void> {
|
||||
const pool = getPool();
|
||||
const query = `
|
||||
INSERT INTO skill_categories (id, name, icon)
|
||||
VALUES ($1, $2, $3)
|
||||
`;
|
||||
|
||||
try {
|
||||
await pool.query(query, [category.id, category.name, category.icon]);
|
||||
} catch (error) {
|
||||
console.error("Error creating skill category:", error);
|
||||
throw new Error("Failed to create skill category");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new skill
|
||||
*/
|
||||
static async createSkill(skill: {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
icon?: string;
|
||||
categoryId: string;
|
||||
links: string[];
|
||||
}): Promise<void> {
|
||||
const pool = getPool();
|
||||
const client = await pool.connect();
|
||||
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
|
||||
// Insert skill
|
||||
const skillQuery = `
|
||||
INSERT INTO skills (id, name, description, icon, category_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`;
|
||||
await client.query(skillQuery, [
|
||||
skill.id,
|
||||
skill.name,
|
||||
skill.description,
|
||||
skill.icon,
|
||||
skill.categoryId,
|
||||
]);
|
||||
|
||||
// Insert links
|
||||
if (skill.links.length > 0) {
|
||||
const linkQuery = `
|
||||
INSERT INTO skill_links (skill_id, url)
|
||||
VALUES ($1, $2)
|
||||
`;
|
||||
for (const link of skill.links) {
|
||||
await client.query(linkQuery, [skill.id, link]);
|
||||
}
|
||||
}
|
||||
|
||||
await client.query("COMMIT");
|
||||
} catch (error) {
|
||||
await client.query("ROLLBACK");
|
||||
console.error("Error creating skill:", error);
|
||||
throw new Error("Failed to create skill");
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk insert skills from JSON data
|
||||
*/
|
||||
static async bulkInsertSkillsFromJSON(
|
||||
skillCategoriesData: SkillCategory[]
|
||||
): Promise<void> {
|
||||
const pool = getPool();
|
||||
const client = await pool.connect();
|
||||
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
|
||||
for (const categoryData of skillCategoriesData) {
|
||||
const categoryId = categoryData.category.toLowerCase();
|
||||
|
||||
// Insert category
|
||||
const categoryQuery = `
|
||||
INSERT INTO skill_categories (id, name, icon)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
name = EXCLUDED.name,
|
||||
icon = EXCLUDED.icon
|
||||
`;
|
||||
await client.query(categoryQuery, [
|
||||
categoryId,
|
||||
categoryData.category,
|
||||
categoryData.icon,
|
||||
]);
|
||||
|
||||
// Insert skills
|
||||
for (const skill of categoryData.skills) {
|
||||
const skillQuery = `
|
||||
INSERT INTO skills (id, name, description, icon, category_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
name = EXCLUDED.name,
|
||||
description = EXCLUDED.description,
|
||||
icon = EXCLUDED.icon,
|
||||
category_id = EXCLUDED.category_id
|
||||
`;
|
||||
await client.query(skillQuery, [
|
||||
skill.id,
|
||||
skill.name,
|
||||
skill.description,
|
||||
skill.icon,
|
||||
categoryId,
|
||||
]);
|
||||
|
||||
// Delete existing links
|
||||
await client.query("DELETE FROM skill_links WHERE skill_id = $1", [
|
||||
skill.id,
|
||||
]);
|
||||
|
||||
// Insert new links
|
||||
if (skill.links && skill.links.length > 0) {
|
||||
const linkQuery = `
|
||||
INSERT INTO skill_links (skill_id, url)
|
||||
VALUES ($1, $2)
|
||||
`;
|
||||
for (const link of skill.links) {
|
||||
await client.query(linkQuery, [skill.id, link]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await client.query("COMMIT");
|
||||
} catch (error) {
|
||||
await client.query("ROLLBACK");
|
||||
console.error("Error bulk inserting skills:", error);
|
||||
throw new Error("Failed to bulk insert skills");
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user