diff --git a/src/app/api/komga/ttl-config/route.ts b/src/app/api/komga/ttl-config/route.ts
new file mode 100644
index 0000000..2b0923d
--- /dev/null
+++ b/src/app/api/komga/ttl-config/route.ts
@@ -0,0 +1,47 @@
+import { NextResponse } from "next/server";
+import { ConfigDBService } from "@/lib/services/config-db.service";
+
+export async function GET() {
+ try {
+ const config = await ConfigDBService.getTTLConfig();
+ return NextResponse.json(config);
+ } catch (error) {
+ console.error("Erreur lors de la récupération de la configuration TTL:", error);
+ if (error instanceof Error) {
+ if (error.message === "Utilisateur non authentifié") {
+ return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
+ }
+ }
+ return NextResponse.json(
+ { error: "Erreur lors de la récupération de la configuration TTL" },
+ { status: 500 }
+ );
+ }
+}
+
+export async function POST(request: Request) {
+ try {
+ const data = await request.json();
+ const config = await ConfigDBService.saveTTLConfig(data);
+ return NextResponse.json({
+ message: "Configuration TTL sauvegardée avec succès",
+ config: {
+ defaultTTL: config.defaultTTL,
+ homeTTL: config.homeTTL,
+ librariesTTL: config.librariesTTL,
+ seriesTTL: config.seriesTTL,
+ booksTTL: config.booksTTL,
+ imagesTTL: config.imagesTTL,
+ },
+ });
+ } catch (error) {
+ console.error("Erreur lors de la sauvegarde de la configuration TTL:", error);
+ if (error instanceof Error && error.message === "Utilisateur non authentifié") {
+ return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
+ }
+ return NextResponse.json(
+ { error: "Erreur lors de la sauvegarde de la configuration TTL" },
+ { status: 500 }
+ );
+ }
+}
diff --git a/src/app/settings/page.tsx b/src/app/settings/page.tsx
index 2a9b0d4..ca930ca 100644
--- a/src/app/settings/page.tsx
+++ b/src/app/settings/page.tsx
@@ -3,10 +3,11 @@ import { ClientSettings } from "@/components/settings/ClientSettings";
export default async function SettingsPage() {
let config = null;
+ let ttlConfig = null;
try {
+ // Récupérer la configuration Komga
const mongoConfig = await ConfigDBService.getConfig();
- // Convertir le document Mongoose en objet simple
if (mongoConfig) {
config = {
url: mongoConfig.url,
@@ -15,10 +16,13 @@ export default async function SettingsPage() {
userId: mongoConfig.userId,
};
}
+
+ // Récupérer la configuration TTL
+ ttlConfig = await ConfigDBService.getTTLConfig();
} catch (error) {
console.error("Erreur lors de la récupération de la configuration:", error);
// On ne fait rien si la config n'existe pas, on laissera le composant client gérer l'état initial
}
- return ;
+ return ;
}
diff --git a/src/components/settings/ClientSettings.tsx b/src/components/settings/ClientSettings.tsx
index fb07180..c2ec58e 100644
--- a/src/components/settings/ClientSettings.tsx
+++ b/src/components/settings/ClientSettings.tsx
@@ -18,12 +18,21 @@ interface KomgaConfig {
userId: string;
}
-interface ClientSettingsProps {
- initialConfig: KomgaConfig | null;
+interface TTLConfigData {
+ defaultTTL: number;
+ homeTTL: number;
+ librariesTTL: number;
+ seriesTTL: number;
+ booksTTL: number;
+ imagesTTL: number;
}
-export function ClientSettings({ initialConfig }: ClientSettingsProps) {
- console.log("initialConfig", initialConfig);
+interface ClientSettingsProps {
+ initialConfig: KomgaConfig | null;
+ initialTTLConfig: TTLConfigData | null;
+}
+
+export function ClientSettings({ initialConfig, initialTTLConfig }: ClientSettingsProps) {
const router = useRouter();
const { toast } = useToast();
const [isLoading, setIsLoading] = useState(false);
@@ -36,22 +45,16 @@ export function ClientSettings({ initialConfig }: ClientSettingsProps) {
username: initialConfig?.username || "",
password: initialConfig?.password || "",
});
- const [ttlConfig, setTTLConfig] = useState({
- defaultTTL: 5,
- homeTTL: 5,
- librariesTTL: 1440,
- seriesTTL: 5,
- booksTTL: 5,
- imagesTTL: 1440,
- });
-
- useEffect(() => {
- // Charger la configuration des TTL
- const savedTTLConfig = storageService.getTTLConfig();
- if (savedTTLConfig) {
- setTTLConfig(savedTTLConfig);
+ const [ttlConfig, setTTLConfig] = useState(
+ initialTTLConfig || {
+ defaultTTL: 5,
+ homeTTL: 5,
+ librariesTTL: 1440,
+ seriesTTL: 5,
+ booksTTL: 5,
+ imagesTTL: 1440,
}
- }, []);
+ );
const handleClearCache = async () => {
setIsCacheClearing(true);
@@ -206,15 +209,37 @@ export function ClientSettings({ initialConfig }: ClientSettingsProps) {
}));
};
- const handleSaveTTL = (event: React.FormEvent) => {
+ const handleSaveTTL = async (event: React.FormEvent) => {
event.preventDefault();
setSuccess(null);
- storageService.setTTLConfig(ttlConfig);
- toast({
- title: "Configuration TTL sauvegardée",
- description: "La configuration des TTL a été sauvegardée avec succès",
- });
+ try {
+ const response = await fetch("/api/komga/ttl-config", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(ttlConfig),
+ });
+
+ if (!response.ok) {
+ const data = await response.json();
+ throw new Error(data.error || "Erreur lors de la sauvegarde de la configuration TTL");
+ }
+
+ toast({
+ title: "Configuration TTL sauvegardée",
+ description: "La configuration des TTL a été sauvegardée avec succès",
+ });
+ } catch (error) {
+ console.error("Erreur lors de la sauvegarde:", error);
+ toast({
+ variant: "destructive",
+ title: "Erreur",
+ description:
+ error instanceof Error ? error.message : "Une erreur est survenue lors de la sauvegarde",
+ });
+ }
};
return (
diff --git a/src/lib/models/ttl-config.model.ts b/src/lib/models/ttl-config.model.ts
new file mode 100644
index 0000000..a95d947
--- /dev/null
+++ b/src/lib/models/ttl-config.model.ts
@@ -0,0 +1,46 @@
+import mongoose from "mongoose";
+
+const ttlConfigSchema = new mongoose.Schema(
+ {
+ userId: {
+ type: String,
+ required: true,
+ unique: true,
+ },
+ defaultTTL: {
+ type: Number,
+ default: 5,
+ },
+ homeTTL: {
+ type: Number,
+ default: 5,
+ },
+ librariesTTL: {
+ type: Number,
+ default: 1440,
+ },
+ seriesTTL: {
+ type: Number,
+ default: 5,
+ },
+ booksTTL: {
+ type: Number,
+ default: 5,
+ },
+ imagesTTL: {
+ type: Number,
+ default: 1440,
+ },
+ },
+ {
+ timestamps: true,
+ }
+);
+
+// Middleware pour mettre à jour le champ updatedAt avant la sauvegarde
+ttlConfigSchema.pre("save", function (next) {
+ this.updatedAt = new Date();
+ next();
+});
+
+export const TTLConfig = mongoose.models.TTLConfig || mongoose.model("TTLConfig", ttlConfigSchema);
diff --git a/src/lib/services/config-db.service.ts b/src/lib/services/config-db.service.ts
index a35b137..cb66977 100644
--- a/src/lib/services/config-db.service.ts
+++ b/src/lib/services/config-db.service.ts
@@ -1,6 +1,7 @@
import { cookies } from "next/headers";
import connectDB from "@/lib/mongodb";
import { KomgaConfig } from "@/lib/models/config.model";
+import { TTLConfig } from "@/lib/models/ttl-config.model";
interface User {
id: string;
@@ -13,6 +14,15 @@ interface KomgaConfigData {
password: string;
}
+interface TTLConfigData {
+ defaultTTL: number;
+ homeTTL: number;
+ librariesTTL: number;
+ seriesTTL: number;
+ booksTTL: number;
+ imagesTTL: number;
+}
+
export class ConfigDBService {
private static async getCurrentUser(): Promise {
const userCookie = cookies().get("stripUser");
@@ -59,4 +69,48 @@ export class ConfigDBService {
return config;
}
+
+ static async saveTTLConfig(data: TTLConfigData) {
+ const user = await this.getCurrentUser();
+ await connectDB();
+
+ const config = await TTLConfig.findOneAndUpdate(
+ { userId: user.id },
+ {
+ userId: user.id,
+ ...data,
+ },
+ { upsert: true, new: true }
+ );
+
+ return config;
+ }
+
+ static async getTTLConfig() {
+ const user = await this.getCurrentUser();
+ await connectDB();
+
+ const config = await TTLConfig.findOne({ userId: user.id });
+
+ if (!config) {
+ // Retourner la configuration par défaut si aucune configuration n'existe
+ return {
+ defaultTTL: 5,
+ homeTTL: 5,
+ librariesTTL: 1440,
+ seriesTTL: 5,
+ booksTTL: 5,
+ imagesTTL: 1440,
+ };
+ }
+
+ return {
+ defaultTTL: config.defaultTTL,
+ homeTTL: config.homeTTL,
+ librariesTTL: config.librariesTTL,
+ seriesTTL: config.seriesTTL,
+ booksTTL: config.booksTTL,
+ imagesTTL: config.imagesTTL,
+ };
+ }
}