Files
stripstream/src/app/api/komga/cache/mode/route.ts
2025-02-27 14:26:48 +01:00

59 lines
1.7 KiB
TypeScript

import { NextResponse } from "next/server";
import {
CacheMode,
getServerCacheService,
ServerCacheService,
} from "@/lib/services/server-cache.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { getErrorMessage } from "@/utils/errors";
export async function GET() {
try {
const cacheService: ServerCacheService = await getServerCacheService();
return NextResponse.json({ mode: cacheService.getCacheMode() });
} catch (error) {
console.error("Erreur lors de la récupération du mode de cache:", error);
return NextResponse.json(
{
error: {
code: ERROR_CODES.CACHE.MODE_FETCH_ERROR,
message: getErrorMessage(ERROR_CODES.CACHE.MODE_FETCH_ERROR),
},
},
{ status: 500 }
);
}
}
export async function POST(request: Request) {
try {
const { mode }: { mode: CacheMode } = await request.json();
if (mode !== "file" && mode !== "memory") {
return NextResponse.json(
{
error: {
code: ERROR_CODES.CACHE.INVALID_MODE,
message: getErrorMessage(ERROR_CODES.CACHE.INVALID_MODE),
},
},
{ status: 400 }
);
}
const cacheService: ServerCacheService = await getServerCacheService();
cacheService.setCacheMode(mode);
return NextResponse.json({ mode: cacheService.getCacheMode() });
} catch (error) {
console.error("Erreur lors de la mise à jour du mode de cache:", error);
return NextResponse.json(
{
error: {
code: ERROR_CODES.CACHE.MODE_UPDATE_ERROR,
message: getErrorMessage(ERROR_CODES.CACHE.MODE_UPDATE_ERROR),
},
},
{ status: 500 }
);
}
}