2022-09-15 09:07:25 +00:00
|
|
|
import { cache, http } from '@/services'
|
2022-07-16 22:42:29 +00:00
|
|
|
import { albumStore, artistStore, songStore } from '@/stores'
|
2022-07-08 14:53:04 +00:00
|
|
|
|
|
|
|
export const mediaInfoService = {
|
|
|
|
async fetchForArtist (artist: Artist) {
|
2022-07-25 13:44:17 +00:00
|
|
|
artist = artistStore.syncWithVault(artist)[0]
|
2022-07-08 14:53:04 +00:00
|
|
|
const cacheKey = ['artist.info', artist.id]
|
2022-07-25 13:25:27 +00:00
|
|
|
if (cache.has(cacheKey)) return cache.get<ArtistInfo>(cacheKey)
|
2022-07-08 14:53:04 +00:00
|
|
|
|
2022-09-15 09:07:25 +00:00
|
|
|
const info = await http.get<ArtistInfo | null>(`artists/${artist.id}/information`)
|
2022-07-08 14:53:04 +00:00
|
|
|
|
2022-07-25 13:44:17 +00:00
|
|
|
info && cache.set(cacheKey, info)
|
|
|
|
info?.image && (artist.image = info.image)
|
2022-07-16 22:42:29 +00:00
|
|
|
|
2022-07-08 14:53:04 +00:00
|
|
|
return info
|
|
|
|
},
|
|
|
|
|
|
|
|
async fetchForAlbum (album: Album) {
|
2022-07-25 13:44:17 +00:00
|
|
|
album = albumStore.syncWithVault(album)[0]
|
2022-07-08 14:53:04 +00:00
|
|
|
const cacheKey = ['album.info', album.id]
|
2022-07-25 13:25:27 +00:00
|
|
|
if (cache.has(cacheKey)) return cache.get<AlbumInfo>(cacheKey)
|
2022-07-08 14:53:04 +00:00
|
|
|
|
2022-09-15 09:07:25 +00:00
|
|
|
const info = await http.get<AlbumInfo | null>(`albums/${album.id}/information`)
|
2022-07-25 13:25:27 +00:00
|
|
|
info && cache.set(cacheKey, info)
|
2022-07-08 14:53:04 +00:00
|
|
|
|
2022-07-16 22:42:29 +00:00
|
|
|
if (info?.cover) {
|
2022-07-25 13:44:17 +00:00
|
|
|
album.cover = info.cover
|
|
|
|
songStore.byAlbum(album).forEach(song => (song.album_cover = info.cover!))
|
2022-07-16 22:42:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 14:53:04 +00:00
|
|
|
return info
|
|
|
|
}
|
|
|
|
}
|