refactor: standardize string quotes and improve code readability across multiple files

- Updated string quotes from single to double in useRecommendationProfile.ts, client.ts, recommendationService.ts, and recommendations.ts for consistency.
- Enhanced error handling and logging in API client and recommendation service.
- Improved code structure and formatting for better readability and maintainability.
- Ensured consistent use of semicolons and spacing throughout the codebase.
This commit is contained in:
ersaayan
2025-11-22 16:21:15 +03:00
parent 73f9248e98
commit bcf8652728
10 changed files with 1129 additions and 1054 deletions
+98 -72
View File
@@ -1,70 +1,82 @@
import { NextRequest, NextResponse } from 'next/server'
import { RecommendationService } from '@/services/recommendationService'
import { RecommendationRequest } from '@/types/recommendations'
import { NextRequest, NextResponse } from "next/server";
import { RecommendationService } from "@/services/recommendationService";
import { RecommendationRequest } from "@/types/recommendations";
export async function POST(request: NextRequest) {
try {
const body: RecommendationRequest = await request.json()
const body: RecommendationRequest = await request.json();
// Validate required fields
if (!body.platform_id) {
return NextResponse.json(
{ error: 'platform_id is required' },
{ error: "platform_id is required" },
{ status: 400 }
)
);
}
if (!body.categories || body.categories.length === 0) {
return NextResponse.json(
{ error: 'At least one category is required' },
{ error: "At least one category is required" },
{ status: 400 }
)
);
}
// Validate platform_id
const validPlatforms = ['windows', 'macos', 'ubuntu', 'debian', 'arch', 'fedora']
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(', ')}` },
{
error: `Invalid platform_id. Must be one of: ${validPlatforms.join(
", "
)}`,
},
{ status: 400 }
)
);
}
// Validate categories
const validCategories = [
'development',
'design',
'multimedia',
'system-tools',
'gaming',
'productivity',
'education'
]
"development",
"design",
"multimedia",
"system-tools",
"gaming",
"productivity",
"education",
];
const invalidCategories = body.categories.filter(
cat => !validCategories.includes(cat)
)
(cat) => !validCategories.includes(cat)
);
if (invalidCategories.length > 0) {
return NextResponse.json(
{
error: `Invalid categories: ${invalidCategories.join(', ')}`,
validCategories
{
error: `Invalid categories: ${invalidCategories.join(", ")}`,
validCategories,
},
{ status: 400 }
)
);
}
// Set default limit
const limit = body.limit && body.limit > 0 && body.limit <= 50
? body.limit
: 20
const limit =
body.limit && body.limit > 0 && body.limit <= 50 ? body.limit : 20;
// Generate recommendations
const recommendations = await RecommendationService.generateRecommendations({
platform_id: body.platform_id,
categories: body.categories,
experienceLevel: body.experienceLevel,
limit
})
const recommendations = await RecommendationService.generateRecommendations(
{
platform_id: body.platform_id,
categories: body.categories,
experienceLevel: body.experienceLevel,
limit,
}
);
return NextResponse.json({
recommendations,
@@ -72,68 +84,82 @@ export async function POST(request: NextRequest) {
userProfile: {
categories: body.categories,
platform: body.platform_id,
experienceLevel: body.experienceLevel
}
})
experienceLevel: body.experienceLevel,
},
});
} catch (error) {
console.error('Error generating recommendations:', error)
console.error("Error generating recommendations:", error);
return NextResponse.json(
{
error: 'Failed to generate recommendations',
details: error instanceof Error ? error.message : 'Unknown error'
{
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 experienceLevel = searchParams.get('experience_level')
const limit = searchParams.get('limit')
const { searchParams } = new URL(request.url);
const platformId = searchParams.get("platform_id");
const categoriesParam = searchParams.get("categories");
const experienceLevel = searchParams.get("experience_level");
const limit = searchParams.get("limit");
// Validate required fields
if (!platformId) {
return NextResponse.json(
{ error: 'platform_id query parameter is required' },
{ error: "platform_id query parameter is required" },
{ status: 400 }
)
);
}
if (!categoriesParam) {
return NextResponse.json(
{ error: 'categories query parameter is required (comma-separated)' },
{ error: "categories query parameter is required (comma-separated)" },
{ status: 400 }
)
);
}
// Parse categories
const categories = categoriesParam.split(',').map(c => c.trim())
const categories = categoriesParam.split(",").map((c) => c.trim());
// Validate platform_id
const validPlatforms = ['windows', 'macos', 'ubuntu', 'debian', 'arch', 'fedora']
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(', ')}` },
{
error: `Invalid platform_id. Must be one of: ${validPlatforms.join(
", "
)}`,
},
{ status: 400 }
)
);
}
// Set default limit
const parsedLimit = limit && parseInt(limit) > 0 && parseInt(limit) <= 50
? parseInt(limit)
: 20
const parsedLimit =
limit && parseInt(limit) > 0 && parseInt(limit) <= 50
? parseInt(limit)
: 20;
// Generate recommendations
const recommendations = await RecommendationService.generateRecommendations({
platform_id: platformId,
categories: categories as any,
experienceLevel: experienceLevel as any,
limit: parsedLimit
})
const recommendations = await RecommendationService.generateRecommendations(
{
platform_id: platformId,
categories: categories as any,
experienceLevel: experienceLevel as any,
limit: parsedLimit,
}
);
return NextResponse.json({
recommendations,
@@ -141,17 +167,17 @@ export async function GET(request: NextRequest) {
userProfile: {
categories,
platform: platformId,
experienceLevel
}
})
experienceLevel,
},
});
} catch (error) {
console.error('Error generating recommendations:', error)
console.error("Error generating recommendations:", error);
return NextResponse.json(
{
error: 'Failed to generate recommendations',
details: error instanceof Error ? error.message : 'Unknown error'
{
error: "Failed to generate recommendations",
details: error instanceof Error ? error.message : "Unknown error",
},
{ status: 500 }
)
);
}
}