diff --git a/app/Http/Controllers/SpotifyController.php b/app/Http/Controllers/SpotifyController.php index e9bb950..a2ea6eb 100644 --- a/app/Http/Controllers/SpotifyController.php +++ b/app/Http/Controllers/SpotifyController.php @@ -89,35 +89,33 @@ public function index(): Renderable /** * Show the users top tracks * - * @param String $term expect 'long_term', 'medium_term' or 'short_term' + * @param Request $request * @return Renderable */ - public function topTracks(string $term = 'long_term'): Renderable + public function topTracks(Request $request): Renderable { $socialProfile = auth()->user()->socialProfile()->first() ?: new SocialLoginProfile; if ($socialProfile->spotify_accessToken == null) return view('spotify.notconnected'); - $dataCount = User::find(auth()->user()->id)->spotifyActivity->count(); - if ($dataCount == 0) - return view('spotify.nodata'); - - if (!in_array($term, ['long_term', 'short_term', 'medium_term'])) - $term = 'short_term'; - - $access_token = auth()->user()->socialProfile->spotify_accessToken; + $validated = $request->validate([ + 'from' => ['nullable', 'date'], + 'to' => ['nullable', 'date'], + ]); - try { - $top_tracks = SpotifyAPIController::getTopTracks($access_token, $term); - } catch (SpotifyTokenExpiredException $e) { - return view('spotify.notconnected'); - } catch (SpotifyAPIException $e) { - report($e); - return view('spotify.nodata'); + $topTracks = auth()->user()->spotifyActivity(); + if (isset($validated['from'])) { + $topTracks->where('timestamp_start', '>=', $validated['from']); + } + if (isset($validated['to'])) { + $topTracks->where('timestamp_start', '<=', $validated['to']); } + $topTracks->groupBy('track_id') + ->select(['track_id', DB::raw('COUNT(track_id) as minutes')]) + ->orderByDesc('minutes'); return view('spotify.top_tracks', [ - 'top_tracks' => $top_tracks + 'top_tracks' => $topTracks->paginate(16) ]); } diff --git a/resources/views/spotify/card_topTracks.blade.php b/resources/views/spotify/card_topTracks.blade.php index 4bd695f..6e4f34c 100644 --- a/resources/views/spotify/card_topTracks.blade.php +++ b/resources/views/spotify/card_topTracks.blade.php @@ -1,38 +1,5 @@ @foreach($topTracks as $playActivity) -
{{__('spotify.place')}} | -- | {{__('spotify.track')}} | -{{__('spotify.preview')}} | -
---|---|---|---|
#{{$loop->index + 1}} | -- @isset($track->album->images[0]->url) - - @endisset - | -
- {{$track->name}} - - @foreach($track->artists as $artist) - @if($loop->first)von @endif - {{$artist->name}} - @if(!$loop->last) und @endif - @endforeach - - - @if($track->popularity > 80) - {{__('spotify.popular_track')}} - @endif - |
- - - | -