Skip to content

Commit

Permalink
Improved TopTracks
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu committed Nov 11, 2020
1 parent eb26ade commit 599c461
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 13 deletions.
8 changes: 5 additions & 3 deletions app/Http/Controllers/SpotifyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,19 @@ public function topTracks(Request $request): Renderable

$topTracks = auth()->user()->spotifyActivity();
if (isset($validated['from'])) {
$topTracks->where('timestamp_start', '>=', $validated['from']);
$topTracks->where('created_at', '>=', $validated['from']);
}
if (isset($validated['to'])) {
$topTracks->where('timestamp_start', '<=', $validated['to']);
$topTracks->where('created_at', '<=', $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' => $topTracks->paginate(16)
'top_tracks' => $topTracks->paginate(16),
'from' => isset($validated['from']) ? Carbon::parse($validated['from']) : auth()->user()->spotifyActivity()->orderBy('created_at')->limit(1)->select('created_at')->first()->created_at,
'to' => isset($validated['to']) ? Carbon::parse($validated['to']) : auth()->user()->spotifyActivity()->orderBy('created_at', 'DESC')->limit(1)->select('created_at')->first()->created_at
]);
}

Expand Down
84 changes: 74 additions & 10 deletions resources/views/spotify/top_tracks.blade.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,86 @@
@extends('layout.app')

@section('title')Deine Top Tracks @endsection
@section('title')Deine Top Tracks zwischen {{$from->format('d.m.Y')}} und {{$to->format('d.m.Y')}} @endsection

@section('content')
<div class="row">
@foreach($top_tracks as $activity)
<div class="col-md-6">
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h2>Zeitraum wählen</h2>
<a href="?from={{\Carbon\Carbon::now()->subMonths(2)->firstOfMonth()->toDateString()}}&to={{\Carbon\Carbon::now()->subMonths(2)->lastOfMonth()->toDateString()}}"
class="btn btn-primary">
{{\Carbon\Carbon::now()->subMonths(2)->isoFormat('MMMM YYYY')}}
</a>
<a href="?from={{\Carbon\Carbon::now()->subMonth()->firstOfMonth()->toDateString()}}&to={{\Carbon\Carbon::now()->subMonth()->lastOfMonth()->toDateString()}}"
class="btn btn-primary">
{{\Carbon\Carbon::now()->subMonth()->isoFormat('MMMM YYYY')}}
</a>
<a href="?from={{\Carbon\Carbon::now()->firstOfMonth()->toDateString()}}" class="btn btn-primary">
{{\Carbon\Carbon::now()->isoFormat('MMMM YYYY')}}
</a>
<hr/>
<a href="?from={{\Carbon\Carbon::now()->subYear()->format('Y')}}-01-01&to={{\Carbon\Carbon::now()->subYear()->format('Y')}}-12-31"
class="btn btn-primary">
Letztes Jahr ({{\Carbon\Carbon::now()->subYear()->format('Y')}})
</a>
<a href="?from={{\Carbon\Carbon::now()->format('Y')}}-01-01" class="btn btn-primary">
Dieses Jahr ({{\Carbon\Carbon::now()->format('Y')}})
</a>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h2>Eigenen Zeitraum wählen</h2>
<form method="GET">
<input type="hidden" name="page" value="1"/>
<div class="form-group">
<label>Von (Datum)</label>
<input type="date" name="from" class="form-control"/>
</div>
<div class="form-group">
<label>Bis (Datum)</label>
<input type="date" name="to" class="form-control"/>
</div>
<button type="submit" class="btn btn-primary">Öffnen</button>
</form>
</div>
</div>
</div>
</div>
<hr/>
@if(count($top_tracks) > 0)
<div class="row">
<div class="col-md-12">
{{$top_tracks->withQueryString()->links()}}
</div>
@foreach($top_tracks as $activity)
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h2>
Platz {{$loop->index + 1 + ($top_tracks->perPage() * ($top_tracks->currentPage() - 1))}}</h2>
@include('spotify.components.track', ['track' => $activity->track, 'minutes' => $activity->minutes])
</div>
</div>
</div>
@endforeach
<div class="col-md-12">
{{$top_tracks->withQueryString()->links()}}
</div>
</div>
@else
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<h2>
Platz {{$loop->index + 1 + ($top_tracks->perPage() * ($top_tracks->currentPage() - 1))}}</h2>
@include('spotify.components.track', ['track' => $activity->track, 'minutes' => $activity->minutes])
<p class="text-danger font-weight-bold">Es sind keine TopTracks in diesem Zeitraum
vorhanden.</p>
</div>
</div>
</div>
@endforeach
<div class="col-md-12">
{{$top_tracks->withQueryString()->links()}}
</div>
</div>
@endif
@endsection

0 comments on commit 599c461

Please sign in to comment.