From e9731d2ede8ef93c4f1d8daa3b6a33feb36cc921 Mon Sep 17 00:00:00 2001
From: Jonas Heinrich <onny@project-insanity.org>
Date: Mon, 4 Jan 2021 17:52:33 +0100
Subject: [PATCH] further using api class, remove code dublication

---
 src/services/FyydApi.js | 26 +++++++++++++++++++++++++
 src/services/ShowApi.js | 16 ++++++++++++++++
 src/views/Browse.vue    | 42 ++++++++++++-----------------------------
 src/views/BrowseAll.vue | 18 ++++--------------
 src/views/Episode.vue   | 38 ++++---------------------------------
 src/views/Show.vue      | 17 +++++++++--------
 6 files changed, 71 insertions(+), 86 deletions(-)

diff --git a/src/services/FyydApi.js b/src/services/FyydApi.js
index 7db2eb4..a9e13cb 100644
--- a/src/services/FyydApi.js
+++ b/src/services/FyydApi.js
@@ -97,4 +97,30 @@ export class FyydApi {
 
 	}
 
+	queryList(listName) {
+
+		delete axios.defaults.headers.requesttoken
+		let queryURL = ''
+
+		if (listName === 'hot') {
+			queryURL = this.url() + '/feature/podcast/hot'
+		} else if (listName === 'latest') {
+			queryURL = this.url() + '/podcast/latest'
+		}
+
+		return axios.get(queryURL)
+			.then(
+				(response) => {
+					return Promise.resolve(response.data)
+				},
+				(err) => {
+					return Promise.reject(err)
+				}
+			)
+			.catch((err) => {
+				return Promise.reject(err)
+			})
+
+	}
+
 }
diff --git a/src/services/ShowApi.js b/src/services/ShowApi.js
index 65d5f1b..500918d 100644
--- a/src/services/ShowApi.js
+++ b/src/services/ShowApi.js
@@ -54,4 +54,20 @@ export class ShowApi {
 			})
 	}
 
+	removeShow(show) {
+		axios.defaults.headers.requesttoken = requesttoken
+		return axios.delete(this.url('/shows/' + show.id))
+			.then(
+				(response) => {
+					return Promise.resolve(response.data)
+				},
+				(err) => {
+					return Promise.reject(err)
+				}
+			)
+			.catch((err) => {
+				return Promise.reject(err)
+			})
+	}
+
 }
diff --git a/src/views/Browse.vue b/src/views/Browse.vue
index e48c10d..11833a6 100644
--- a/src/views/Browse.vue
+++ b/src/views/Browse.vue
@@ -21,9 +21,9 @@
   -->
 <template>
 	<div>
-		<BrowseEmpty v-show="pageLoading" />
+		<BrowseEmpty v-show="loading" />
 		<div
-			v-show="!pageLoading"
+			v-show="!loading"
 			class="mainContent">
 			<div class="podcastSection">
 				<div class="podcastSectionHeader">
@@ -103,8 +103,8 @@
 <script>
 import BrowseEmpty from './placeholder/Browse'
 
-import { showError } from '@nextcloud/dialogs'
-import axios from '@nextcloud/axios'
+import { FyydApi } from './../services/FyydApi'
+const fyydClient = new FyydApi()
 
 export default {
 	name: 'Browse',
@@ -114,39 +114,21 @@ export default {
 	data: () => ({
 		podcastsHot: {},
 		podcastsLatest: {},
-		pageLoading: true,
+		loading: true,
 	}),
 	mounted() {
-		this.queryTopPodcasts()
+		this.queryPodcastLists()
 	},
 	methods: {
 
-		async queryTopPodcasts() {
+		async queryPodcastLists() {
 
-			const vm = this
+			const hotList = await fyydClient.queryList('hot')
+			this.podcastsHot = hotList.data
+			const latestList = await fyydClient.queryList('latest')
+			this.podcastsLatest = latestList.data
+			this.loading = false
 
-			let queryURI = this.$apiUrl + '/feature/podcast/hot'
-			try {
-				delete axios.defaults.headers.requesttoken
-				await axios.get(queryURI)
-					.then(function(response) {
-						vm.podcastsHot = response.data.data
-					})
-			} catch (error) {
-				showError(t('podcast', 'Could not fetch podcasts from remote API'))
-			}
-
-			queryURI = this.$apiUrl + '/podcast/latest'
-			try {
-				delete axios.defaults.headers.requesttoken
-				await axios.get(queryURI)
-					.then(function(response) {
-						vm.podcastsLatest = response.data.data
-						vm.pageLoading = false
-					})
-			} catch (error) {
-				showError(t('podcast', 'Could not fetch podcasts from remote API'))
-			}
 		},
 
 	},
diff --git a/src/views/BrowseAll.vue b/src/views/BrowseAll.vue
index 3b06a9d..fc02827 100644
--- a/src/views/BrowseAll.vue
+++ b/src/views/BrowseAll.vue
@@ -48,8 +48,8 @@
 </template>
 
 <script>
-import { showError } from '@nextcloud/dialogs'
-import axios from '@nextcloud/axios'
+import { FyydApi } from './../services/FyydApi'
+const fyydClient = new FyydApi()
 
 export default {
 	name: 'BrowseAll',
@@ -63,18 +63,8 @@ export default {
 
 		async queryPodcasts() {
 
-			const vm = this
-
-			const queryURI = this.$apiUrl + '/feature/podcast/hot'
-			try {
-				delete axios.defaults.headers.requesttoken
-				await axios.get(queryURI)
-					.then(function(response) {
-						vm.podcasts = response.data.data
-					})
-			} catch (error) {
-				showError(t('podcast', 'Could not fetch podcasts from remote API'))
-			}
+			const hotList = await fyydClient.queryList('hot')
+			this.podcasts = hotList.data
 
 		},
 
diff --git a/src/views/Episode.vue b/src/views/Episode.vue
index 925b5fd..00df13b 100644
--- a/src/views/Episode.vue
+++ b/src/views/Episode.vue
@@ -122,23 +122,8 @@ export default {
 		},
 
 		async queryEpisode(episodeId) {
-
-			const vm = this
-
-			const queryURI = this.$apiUrl + '/episode'
-			try {
-				delete axios.defaults.headers.requesttoken
-				await axios.get(queryURI, {
-					params: {
-						episode_id: episodeId,
-					},
-				})
-					.then(function(response) {
-						vm.processEpisode(response.data)
-					})
-			} catch (error) {
-				showError(t('podcast', 'Could not fetch episode from remote API'))
-			}
+			const episode = await fyydClient.queryEpisode(episodeId)
+			this.processEpisode(episode)
 		},
 
 		processEpisode(episode) {
@@ -148,23 +133,8 @@ export default {
 		},
 
 		async queryPodcastName(podcastId) {
-			const vm = this
-
-			const queryURI = this.$apiUrl + '/podcast'
-			try {
-				delete axios.defaults.headers.requesttoken
-				await axios.get(queryURI, {
-					params: {
-						podcast_id: podcastId,
-						count: 0,
-					},
-				})
-					.then(function(response) {
-						vm.podcastName = response.data.data.title
-					})
-			} catch (error) {
-				showError(t('podcast', 'Could not fetch podcast from remote API'))
-			}
+			const podcast = await fyydClient.queryPodcast(podcastId)
+			this.podcastName = podcast.data.title
 		},
 
 	},
diff --git a/src/views/Show.vue b/src/views/Show.vue
index 858bdb9..5d931bc 100644
--- a/src/views/Show.vue
+++ b/src/views/Show.vue
@@ -51,13 +51,9 @@
 <script>
 import Table from '../components/Table'
 import MediaHeader from '../components/MediaHeader'
-
 import EmptyContent from '@nextcloud/vue/dist/Components/EmptyContent'
-
-import { audioPlayer, doPlay } from '../services/player'
-
 import ShowEmpty from './placeholder/Show'
-
+import { audioPlayer, doPlay } from '../services/player'
 import { ShowApi } from './../services/ShowApi'
 import { FyydApi } from './../services/FyydApi'
 const fyydClient = new FyydApi()
@@ -80,7 +76,7 @@ export default {
 	}),
 	computed: {
 		loading() {
-			return this.episodes.length === 0
+			return this.episodes.length === 0 // FIXME: also consider podcast object
 		},
 		player() {
 			return this.$store.state.player
@@ -140,8 +136,13 @@ export default {
 		},
 
 		async doSubscribe() {
-			await apiClient.addShow(this.podcast)
-			this.issubscribed = true
+			if (this.issubscribed) {
+				await apiClient.removeShow(this.podcast)
+				this.issubscribed = false
+			} else {
+				await apiClient.addShow(this.podcast)
+				this.issubscribed = true
+			}
 		},
 
 		preFill() {
-- 
GitLab