refacto(db): favorites on db

This commit is contained in:
Julien Froidefond
2025-02-14 15:50:06 +01:00
parent b71ccd6b0e
commit 313cd60e74
9 changed files with 224 additions and 44 deletions

View File

@@ -1,40 +1,111 @@
import { storageService } from "./storage.service";
import { cookies } from "next/headers";
import connectDB from "@/lib/mongodb";
import { FavoriteModel } from "@/lib/models/favorite.model";
interface User {
id: string;
email: string;
}
export class FavoriteService {
private static readonly FAVORITES_CHANGE_EVENT = "favoritesChanged";
private static dispatchFavoritesChanged() {
// Dispatch l'événement pour notifier les changements
window.dispatchEvent(new Event(FavoriteService.FAVORITES_CHANGE_EVENT));
if (typeof window !== "undefined") {
window.dispatchEvent(new Event(FavoriteService.FAVORITES_CHANGE_EVENT));
}
}
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é");
}
}
/**
* Vérifie si une série est dans les favoris
*/
static isFavorite(seriesId: string): boolean {
return storageService.isFavorite(seriesId);
static async isFavorite(seriesId: string): Promise<boolean> {
try {
const user = await this.getCurrentUser();
await connectDB();
const favorite = await FavoriteModel.findOne({
userId: user.id,
seriesId: seriesId,
});
return !!favorite;
} catch (error) {
console.error("Erreur lors de la vérification du favori:", error);
return false;
}
}
/**
* Ajoute une série aux favoris
*/
static addToFavorites(seriesId: string): void {
storageService.addFavorite(seriesId);
this.dispatchFavoritesChanged();
static async addToFavorites(seriesId: string): Promise<void> {
try {
const user = await this.getCurrentUser();
await connectDB();
await FavoriteModel.findOneAndUpdate(
{ userId: user.id, seriesId },
{ userId: user.id, seriesId },
{ upsert: true }
);
this.dispatchFavoritesChanged();
} catch (error) {
console.error("Erreur lors de l'ajout aux favoris:", error);
throw new Error("Erreur lors de l'ajout aux favoris");
}
}
/**
* Retire une série des favoris
*/
static removeFromFavorites(seriesId: string): void {
storageService.removeFavorite(seriesId);
this.dispatchFavoritesChanged();
static async removeFromFavorites(seriesId: string): Promise<void> {
try {
const user = await this.getCurrentUser();
await connectDB();
await FavoriteModel.findOneAndDelete({
userId: user.id,
seriesId,
});
this.dispatchFavoritesChanged();
} catch (error) {
console.error("Erreur lors de la suppression des favoris:", error);
throw new Error("Erreur lors de la suppression des favoris");
}
}
/**
* Récupère tous les IDs des séries favorites
*/
static getAllFavoriteIds(): string[] {
return storageService.getFavorites();
static async getAllFavoriteIds(): Promise<string[]> {
try {
const user = await this.getCurrentUser();
await connectDB();
const favorites = await FavoriteModel.find({ userId: user.id });
return favorites.map((favorite) => favorite.seriesId);
} catch (error) {
console.error("Erreur lors de la récupération des favoris:", error);
return [];
}
}
}