Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Unit Tests & Some Restful refactors #29

Merged
merged 8 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ yarn-error.log
/.fleet
/.idea
/.vscode
.DS_Store
23 changes: 23 additions & 0 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers;

use App\Models\Ranking;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ProfileController extends Controller
{
public function show(string $id): View
{
$user = User::where('spotify_id', $id)->first();

return view('profile.show', [
'user' => $user,
'rankings' => Ranking::query()
->forProfilePage($user)
->paginate(5)
]);
}
}
53 changes: 16 additions & 37 deletions app/Http/Controllers/RankingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
namespace App\Http\Controllers;

use App\Http\Requests\Rankings\CreateRankingRequest;
use App\Http\Requests\Rankings\DeleteRankingRequest;
use App\Http\Requests\Rankings\DestroyRankingRequest;
use App\Http\Requests\Rankings\FinishRankingRequest;
use App\Http\Requests\Rankings\UpdateRankingRequest;
use App\Jobs\DownloadDataJob;
use App\Models\Ranking;
use App\Models\Song;
use App\Models\User;
use App\Models\Ranking;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;

class RankingController extends Controller
{
//TODO change from pk -> slug
public function show($id): View
{
$ranking = Ranking::query()
Expand Down Expand Up @@ -45,36 +44,32 @@ public function create(CreateRankingRequest $request): JsonResponse

public function edit($id): View
{
$ranking = Ranking::query()
->with('songs')
->findOrFail($id);

if ($ranking->user_id !== auth()->id()) {
abort(403, "You are not allowed to edit this ranking.");
}

return view('rank.edit', [
'ranking' => Ranking::query()
->with('songs')
->findOrFail($id),
'ranking' => $ranking,
]);
}

public function update(UpdateRankingRequest $request, $id): JsonResponse
{
Ranking::findOrFail($id)->update($request->validated());
$ranking = Ranking::findOrFail($id);
$ranking->update($request->validated());

session()->flash('success', 'Ranking was succesfully updated!');

return response()->json([
'redirect' => route('profile.index') . '?user=' . auth()->user()->spotify_id,
], 200);
}

public function finish(FinishRankingRequest $request): JsonResponse
{
Ranking::complete(collect($request->songs), $request->rankingId);

session()->flash('success', 'Ranking was succesfully saved!');

return response()->json([
'redirect' => route('home'),
'redirect' => route('profile.show', ['id' => $ranking->getKey()]),
], 200);
}

public function delete(DeleteRankingRequest $request): JsonResponse
public function destroy(DestroyRankingRequest $request): JsonResponse
{
Ranking::findOrFail($request->rankingId)->delete();
Song::where('ranking_id', $request->rankingId)->delete();
Expand All @@ -93,8 +88,7 @@ public function delete(DeleteRankingRequest $request): JsonResponse

public function pages(): JsonResponse
{
$spotify_id = request()->user;
$user = User::where('spotify_id', $spotify_id)->first();
$user = User::where('spotify_id', request()->spotify_id)->first();

$response = [
'success' => true,
Expand All @@ -109,19 +103,4 @@ public function pages(): JsonResponse

return response()->json($response, 200);
}

public function export(): JsonResponse
{
$rankings = Ranking::query()
->where('user_id', auth()->id())
->with('songs', 'artist')
->get();

DownloadDataJob::dispatch($rankings, auth()->user());

return response()->json([
'success' => true,
'message' => "Your download has started and will be emailed to you when completed!"
], 200);
}
}
26 changes: 26 additions & 0 deletions app/Http/Controllers/RankingDownloadController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Controllers;

use App\Jobs\DownloadDataJob;
use App\Models\Ranking;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class RankingDownloadController extends Controller
{
public function index(): JsonResponse
{
$rankings = Ranking::query()
->where('user_id', auth()->id())
->with('songs', 'artist')
->get();

DownloadDataJob::dispatch($rankings, auth()->user());

return response()->json([
'success' => true,
'message' => "Your download has started and will be emailed to you when completed!"
], 200);
}
}
22 changes: 22 additions & 0 deletions app/Http/Controllers/SongPlacementController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\Song\Placement\StoreSongPlacementRequest;
use App\Models\Ranking;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SongPlacementController extends Controller
{
public function store(StoreSongPlacementRequest $request): JsonResponse
{
Ranking::complete(collect($request->songs), $request->rankingId);

session()->flash('success', 'Song rankings were successfuly placed!');

return response()->json([
'redirect' => route('home'),
], 200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace App\Http\Requests\Rankings;

use App\Models\Ranking;
use Illuminate\Foundation\Http\FormRequest;

class DeleteRankingRequest extends FormRequest
class DestroyRankingRequest extends FormRequest
{
public function authorize(): bool
{
return auth()->check(); // && Ranking::findOrFail(request()->rankingId)->user_id === auth()->id();
return auth()->check() && Ranking::findOrFail(request()->rankingId)->user_id == auth()->id();
}

public function rules(): array
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace App\Http\Requests\Rankings;
namespace App\Http\Requests\Song\Placement;

use App\Models\Ranking;
use Illuminate\Foundation\Http\FormRequest;

class FinishRankingRequest extends FormRequest
class StoreSongPlacementRequest extends FormRequest
{
public function authorize(): bool
{
Expand Down
1 change: 1 addition & 0 deletions app/Models/Artist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Artist extends Model
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Ranking.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Model;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -113,7 +114,7 @@ public static function complete($songs, $id): void
}

/* scopes */
public function scopeForExplorePage(Builder $query, ?string $search)
public function scopeForExplorePage(Builder $query, ?string $search = null)
{
$query->newQuery()
->where('is_ranked', true)
Expand Down
4 changes: 3 additions & 1 deletion app/Models/Song.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace App\Models;

use App\Models\Model;

class Song extends Model
{
protected $fillable = [
'ranking_id',
'song_spotify_id',
'spotify_song_id',
'title',
'rank',
'cover',
Expand Down
4 changes: 4 additions & 0 deletions app/global/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ function get_timezone() :string
{
$ip = 'http://ip-api.com/json/'.request()->ip();

if (env('APP_ENV') !== 'production') {
return "America/New_York";
}

return json_decode(file_get_contents($ip), true)['timezone'];
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"laravel/sail": "^1.26",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.0",
"phpunit/phpunit": "^10.5",
"pestphp/pest": "^3.0",
"spatie/laravel-ignition": "^2.4"
},
"autoload": {
Expand Down
Loading
Loading