feat(db): init mongo and passing komga conf
This commit is contained in:
35
src/lib/models/config.model.ts
Normal file
35
src/lib/models/config.model.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
const configSchema = new mongoose.Schema(
|
||||
{
|
||||
userId: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
},
|
||||
url: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
username: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
password: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
// Middleware pour mettre à jour le champ updatedAt avant la sauvegarde
|
||||
configSchema.pre("save", function (next) {
|
||||
this.updatedAt = new Date();
|
||||
next();
|
||||
});
|
||||
|
||||
export const KomgaConfig =
|
||||
mongoose.models.KomgaConfig || mongoose.model("KomgaConfig", configSchema);
|
||||
51
src/lib/mongodb.ts
Normal file
51
src/lib/mongodb.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
const MONGODB_URI = process.env.MONGODB_URI;
|
||||
|
||||
if (!MONGODB_URI) {
|
||||
throw new Error(
|
||||
"Veuillez définir la variable d'environnement MONGODB_URI dans votre fichier .env"
|
||||
);
|
||||
}
|
||||
|
||||
interface MongooseCache {
|
||||
conn: typeof mongoose | null;
|
||||
promise: Promise<typeof mongoose> | null;
|
||||
}
|
||||
|
||||
declare global {
|
||||
var mongoose: MongooseCache | undefined;
|
||||
}
|
||||
|
||||
let cached: MongooseCache = global.mongoose || { conn: null, promise: null };
|
||||
|
||||
if (!global.mongoose) {
|
||||
global.mongoose = { conn: null, promise: null };
|
||||
}
|
||||
|
||||
async function connectDB(): Promise<typeof mongoose> {
|
||||
if (cached.conn) {
|
||||
return cached.conn;
|
||||
}
|
||||
|
||||
if (!cached.promise) {
|
||||
const opts = {
|
||||
bufferCommands: false,
|
||||
};
|
||||
|
||||
cached.promise = mongoose.connect(MONGODB_URI!, opts).then((mongoose) => {
|
||||
return mongoose;
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
cached.conn = await cached.promise;
|
||||
} catch (e) {
|
||||
cached.promise = null;
|
||||
throw e;
|
||||
}
|
||||
|
||||
return cached.conn;
|
||||
}
|
||||
|
||||
export default connectDB;
|
||||
62
src/lib/services/config-db.service.ts
Normal file
62
src/lib/services/config-db.service.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { cookies } from "next/headers";
|
||||
import connectDB from "@/lib/mongodb";
|
||||
import { KomgaConfig } from "@/lib/models/config.model";
|
||||
|
||||
interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
interface KomgaConfigData {
|
||||
url: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export class ConfigDBService {
|
||||
private static async getCurrentUser(): Promise<User> {
|
||||
const userCookie = cookies().get("stripUser");
|
||||
|
||||
if (!userCookie) {
|
||||
throw new Error("Utilisateur non authentifié");
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(atob(userCookie.value));
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la récupération de l'utilisateur depuis le cookie:", error);
|
||||
throw new Error("Utilisateur non authentifié");
|
||||
}
|
||||
}
|
||||
|
||||
static async saveConfig(data: KomgaConfigData) {
|
||||
const user = await this.getCurrentUser();
|
||||
await connectDB();
|
||||
|
||||
const config = await KomgaConfig.findOneAndUpdate(
|
||||
{ userId: user.id },
|
||||
{
|
||||
userId: user.id,
|
||||
url: data.url,
|
||||
username: data.username,
|
||||
password: data.password,
|
||||
},
|
||||
{ upsert: true, new: true }
|
||||
);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
static async getConfig() {
|
||||
const user = await this.getCurrentUser();
|
||||
await connectDB();
|
||||
|
||||
const config = await KomgaConfig.findOne({ userId: user.id });
|
||||
|
||||
if (!config) {
|
||||
throw new Error("Configuration non trouvée");
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user