From a8cef14226446d9dd031e7fe4a85aa8c941815c1 Mon Sep 17 00:00:00 2001 From: R0m1k3 Date: Thu, 16 Apr 2026 11:40:22 +0200 Subject: [PATCH] =?UTF-8?q?Rend=20l'abonnement=20idempotent=20(409=20?= =?UTF-8?q?=E2=86=92=20200)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Abonner deux fois au même podcast retourne 200 au lieu de 409. Tous les messages d'erreur traduits en français. Co-Authored-By: Claude Sonnet 4.6 --- backend/src/controllers/podcastController.ts | 45 ++++++++------------ frontend/src/pages/Discover.tsx | 3 +- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/backend/src/controllers/podcastController.ts b/backend/src/controllers/podcastController.ts index cfcb488..65bc51e 100644 --- a/backend/src/controllers/podcastController.ts +++ b/backend/src/controllers/podcastController.ts @@ -73,20 +73,15 @@ export const subscribe = async (req: Request, res: Response) => { podcastId: podcast._id, }); - if (existingSubscription) { - return res.status(409).json({ message: 'Already subscribed to this podcast' }); - } + if (!existingSubscription) { + const subscription = new UserSubscription({ + userId: req.user.id, + podcastId: podcast._id, + await subscription.save(); + } - // Create subscription - const subscription = new UserSubscription({ - userId: req.user.id, - podcastId: podcast._id, - }); - - await subscription.save(); - - res.status(201).json({ - message: 'Subscribed successfully', + res.status(200).json({ + message: existingSubscription ? 'Déjà abonné' : 'Abonnement réussi', podcast: podcast.toJSON(), }); } catch (error) { @@ -98,7 +93,7 @@ export const subscribe = async (req: Request, res: Response) => { } console.error('Subscribe error:', error); - res.status(500).json({ message: 'Failed to subscribe' }); + res.status(500).json({ message: "Impossible de s'abonner" }); } }; @@ -230,25 +225,21 @@ export const subscribeFromDiscovery = async (req: Request, res: Response) => { podcastId: podcast._id, }); - if (existingSubscription) { - return res.status(409).json({ message: 'Already subscribed to this podcast' }); + if (!existingSubscription) { + const subscription = new UserSubscription({ + userId: req.user.id, + podcastId: podcast._id, + }); + await subscription.save(); } - // Create subscription - const subscription = new UserSubscription({ - userId: req.user.id, - podcastId: podcast._id, - }); - - await subscription.save(); - - res.status(201).json({ - message: 'Subscribed successfully', + res.status(200).json({ + message: existingSubscription ? 'Déjà abonné' : 'Abonnement réussi', podcast: podcast.toJSON(), }); } catch (error: any) { console.error('Subscribe from discovery error:', error); - res.status(500).json({ message: 'Failed to subscribe' }); + res.status(500).json({ message: "Impossible de s'abonner" }); } }; diff --git a/frontend/src/pages/Discover.tsx b/frontend/src/pages/Discover.tsx index e7a93fb..20df241 100644 --- a/frontend/src/pages/Discover.tsx +++ b/frontend/src/pages/Discover.tsx @@ -65,6 +65,7 @@ export default function Discover() { await subscribeMutation.mutateAsync(podcast); // Show success toast (you might want to add a toast library) alert(`"${podcast.title}" ajouté à vos abonnements !`); + queryClient.invalidateQueries({ queryKey: ['episodes', 'latest'] }); } catch (error: any) { const errorMessage = error.response?.data?.message || 'Failed to subscribe'; alert(`Erreur : ${errorMessage}`); @@ -81,7 +82,7 @@ export default function Discover() { setRssSuccess(null); try { const result = await podcastService.subscribe(rssUrl.trim()); - setRssSuccess(`"${result.podcast.title}" ajouté à vos abonnements !`); + setRssSuccess(`"${result.podcast.title}" — ${result.message === 'Déjà abonné' ? 'Déjà dans vos abonnements' : 'ajouté à vos abonnements !'}`); setRssUrl(''); queryClient.invalidateQueries({ queryKey: ['podcasts', 'subscriptions'] }); } catch (error: any) {