Files
RepoHub/src/app/api/recommendations/route.ts
T
Yusuf İpek 95ad5e32fd refactor: Remove experience level from recommendation system
- Remove experienceLevel from UserProfile, RecommendationRequest, and PackagePreset types
- Remove experience level validation and handling from GET/POST endpoints
- Disable localStorage persistence in useRecommendationProfile hook (session-only storage)
- Disable automatic onboarding modal on first visit
- Set recommendations section to expanded by default
- Add empty state card for recommendations with wizard button
- Add translations
2025-11-23 14:55:49 +03:00

208 lines
5.2 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { RecommendationService } from "@/services/recommendationService";
import { RecommendationRequest, UserCategory } from "@/types/recommendations";
export async function POST(request: NextRequest) {
try {
const body: RecommendationRequest = await request.json();
// Validate required fields
if (!body.platform_id) {
return NextResponse.json(
{ error: "platform_id is required" },
{ status: 400 }
);
}
if (!body.categories || body.categories.length === 0) {
return NextResponse.json(
{ error: "At least one category is required" },
{ status: 400 }
);
}
// Validate platform_id
const validPlatforms = [
"windows",
"macos",
"ubuntu",
"debian",
"arch",
"fedora",
];
if (!validPlatforms.includes(body.platform_id)) {
return NextResponse.json(
{
error: `Invalid platform_id. Must be one of: ${validPlatforms.join(
", "
)}`,
},
{ status: 400 }
);
}
// Validate categories
const validCategories = [
"development",
"design",
"multimedia",
"system-tools",
"gaming",
"productivity",
"education",
];
const invalidCategories = body.categories.filter(
(cat) => !validCategories.includes(cat)
);
if (invalidCategories.length > 0) {
return NextResponse.json(
{
error: `Invalid categories: ${invalidCategories.join(", ")}`,
validCategories,
},
{ status: 400 }
);
}
// Validate and set limit with better error message
if (body.limit !== undefined && (body.limit < 1 || body.limit > 1000)) {
return NextResponse.json(
{ error: "Limit must be between 1 and 1000" },
{ status: 400 }
);
}
const limit =
body.limit && body.limit > 0 && body.limit <= 1000 ? body.limit : 50;
// Generate recommendations
const recommendations = await RecommendationService.generateRecommendations(
{
platform_id: body.platform_id,
categories: body.categories,
limit,
}
);
return NextResponse.json({
recommendations,
total: recommendations.length,
userProfile: {
categories: body.categories,
platform: body.platform_id,
},
});
} catch (error) {
console.error("Error generating recommendations:", error);
return NextResponse.json(
{
error: "Failed to generate recommendations",
details: error instanceof Error ? error.message : "Unknown error",
},
{ status: 500 }
);
}
}
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const platformId = searchParams.get("platform_id");
const categoriesParam = searchParams.get("categories");
const limit = searchParams.get("limit");
// Validate required fields
if (!platformId) {
return NextResponse.json(
{ error: "platform_id query parameter is required" },
{ status: 400 }
);
}
if (!categoriesParam) {
return NextResponse.json(
{ error: "categories query parameter is required (comma-separated)" },
{ status: 400 }
);
}
// Parse categories
const categories = categoriesParam.split(",").map((c) => c.trim());
// Validate platform_id
const validPlatforms = [
"windows",
"macos",
"ubuntu",
"debian",
"arch",
"fedora",
];
if (!validPlatforms.includes(platformId)) {
return NextResponse.json(
{
error: `Invalid platform_id. Must be one of: ${validPlatforms.join(
", "
)}`,
},
{ status: 400 }
);
}
// Validate categories
const validCategories: UserCategory[] = [
"development",
"design",
"multimedia",
"system-tools",
"gaming",
"productivity",
"education",
];
const invalidCategories = categories.filter(
(cat) => !validCategories.includes(cat as UserCategory)
);
if (invalidCategories.length > 0) {
return NextResponse.json(
{
error: `Invalid categories: ${invalidCategories.join(", ")}`,
validCategories,
},
{ status: 400 }
);
}
// Set default limit
const parsedLimit =
limit && parseInt(limit) > 0 && parseInt(limit) <= 50
? parseInt(limit)
: 20;
// Generate recommendations with validated types
const recommendations = await RecommendationService.generateRecommendations(
{
platform_id: platformId,
categories: categories as UserCategory[],
limit: parsedLimit,
}
);
return NextResponse.json({
recommendations,
total: recommendations.length,
userProfile: {
categories,
platform: platformId,
},
});
} catch (error) {
console.error("Error generating recommendations:", error);
return NextResponse.json(
{
error: "Failed to generate recommendations",
details: error instanceof Error ? error.message : "Unknown error",
},
{ status: 500 }
);
}
}