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) -
-
- @isset($playActivity->track->album->imageUrl) - - - - @endisset -
-
- - {{$playActivity->track->name}} - -
- @isset($playActivity->track->artists) - von - @foreach($playActivity->track->artists as $artist) - {{$artist->name}} - @if(!$loop->last) und @endif - @endforeach - -
- @endisset - {{$playActivity->minutes}} Minuten gehört - - @isset($playActivity->track->preview_url) - - @endif -
-
- @include('spotify.track-attributes', ['track' => $playActivity->track]) -
+ @include('spotify.components.track', ['track' => $playActivity->track, 'minutes' => $playActivity->minutes]) @endforeach {{$topTracksTotal->fragment($fragment)->onEachSide(1)->links()}} \ No newline at end of file diff --git a/resources/views/spotify/components/track.blade.php b/resources/views/spotify/components/track.blade.php new file mode 100644 index 0000000..a7f1a63 --- /dev/null +++ b/resources/views/spotify/components/track.blade.php @@ -0,0 +1,36 @@ +
+
+ @isset($track->album->imageUrl) + + + + @endisset +
+
+ + {{$track->name}} + +
+ @isset($track->artists) + von + @foreach($track->artists as $artist) + {{$artist->name}} + @if(!$loop->last) und @endif + @endforeach + +
+ @endisset + @isset($minutes) + {{$minutes}} Minuten gehört + @endisset + + @isset($track->preview_url) + + @endif +
+
+@include('spotify.track-attributes', ['track' => $track]) +
\ No newline at end of file diff --git a/resources/views/spotify/top_tracks.blade.php b/resources/views/spotify/top_tracks.blade.php index 3af0432..31373a1 100644 --- a/resources/views/spotify/top_tracks.blade.php +++ b/resources/views/spotify/top_tracks.blade.php @@ -4,75 +4,19 @@ @section('content')
-
-
-
-
Zeitraum wählen
-
- - {{__('spotify.last_years')}} - - - {{__('spotify.last_months')}} - - - {{__('spotify.last_weeks')}} - + @foreach($top_tracks as $activity) +
+
+
+

+ Platz {{$loop->index + 1 + ($top_tracks->perPage() * ($top_tracks->currentPage() - 1))}}

+ @include('spotify.components.track', ['track' => $activity->track, 'minutes' => $activity->minutes])
-
-
- -
+ @endforeach
-
-
- - - - - - - - - - - @foreach($top_tracks->items as $track) - - - - - - - @endforeach - -
{{__('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 -
- -
-
-
+ {{$top_tracks->withQueryString()->links()}}
@endsection \ No newline at end of file