feat: more work on completing switchover
This commit is contained in:
@@ -3,7 +3,18 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Artwork;
|
||||
use App\Models\Podcast;
|
||||
use App\Models\Episode;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rules\File;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\Facades\Image;
|
||||
use ImageOptimizer;
|
||||
|
||||
class ArtworkController extends Controller
|
||||
{
|
||||
@@ -14,7 +25,19 @@ class ArtworkController extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
$user = auth()->user();
|
||||
$artworks = Artwork::whereNotNull('approved_by')
|
||||
->with('artist')
|
||||
->orderBy('episode_id', 'desc')
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate($perPage = 100, $columns = ['*'], $pageName = 'artworks');
|
||||
$podcasts = Podcast::where('published', true)->with('episodes')->get();
|
||||
return view('explore.artworks', [
|
||||
'user' => $user,
|
||||
'pageTitle' => 'Explore',
|
||||
'artworks' => $artworks,
|
||||
'podcasts' => $podcasts,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,7 +47,13 @@ class ArtworkController extends Controller
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
$user = auth()->user();
|
||||
$podcasts = Podcast::where('published', true)->with('episodes')->get();
|
||||
return view('artworks.submit', [
|
||||
'user' => $user,
|
||||
'pageTitle' => 'Submit New Artwork',
|
||||
'podcasts' => $podcasts,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,20 +62,79 @@ class ArtworkController extends Controller
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
//
|
||||
$validator = Validator::make($request->all(), [
|
||||
'title' => ['required', 'max:255',],
|
||||
'podcast' => ['required', 'exists:podcasts,id',],
|
||||
'description' => ['nullable',],
|
||||
'file' => ['required', 'image', Rule::dimensions()->ratio(1)->minWidth(512),],
|
||||
]);
|
||||
if ($validator->fails()) {
|
||||
return back()
|
||||
->withErrors($validator)
|
||||
->withInput();
|
||||
}
|
||||
Log::channel('artwork_import')->info('making new artwork model.');
|
||||
$podcast = Podcast::where('id', $request->podcast)->with(['episodes' => function($query) {
|
||||
$query->orderBy('episode_number', 'desc')->limit(1);
|
||||
}])->first();
|
||||
$episode = $podcast->episodes->first();
|
||||
$artist = auth()->user()->artists()->first();
|
||||
$rawFile = $request->file('file');
|
||||
$filename = now()->format('Y')
|
||||
. '/'
|
||||
. now()->format('m')
|
||||
. '/'
|
||||
. Str::slug($artist->name)
|
||||
. '-'
|
||||
. Str::slug($request->title)
|
||||
. '_'
|
||||
. Str::random(8)
|
||||
. '.jpg';
|
||||
$artwork = Artwork::factory()->state([
|
||||
'title' => $request->title,
|
||||
'artist_id' => $artist->id,
|
||||
'description' => $request->description,
|
||||
'overlay_id' => null,
|
||||
'podcast_id' => $podcast->id,
|
||||
'episode_id' => $episode->id,
|
||||
'filename' => $filename,
|
||||
])->create();
|
||||
$img = Image::make($rawFile)->resize(3000, null, function($constraint){
|
||||
$constraint->aspectRatio();
|
||||
})
|
||||
->encode('jpg', 100)
|
||||
->save(Storage::disk('static')->path('/artworks') . '/' . $artwork->filename);
|
||||
$thumbImg = Image::make($request->file('file'))->resize(512, null, function($constraint){
|
||||
$constraint->aspectRatio();
|
||||
})
|
||||
->encode('jpg', 100)
|
||||
->save(Storage::disk('static')->path('/thumbnails') . '/' . $artwork->filename);
|
||||
ImageOptimizer::optimize(Storage::disk('static')->path('/artworks/' . $artwork->filename));
|
||||
ImageOptimizer::optimize(Storage::disk('static')->path('/thumbnails/' . $artwork->filename));
|
||||
return redirect('/artworks/' . $artwork->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param \App\Models\Artwork $artwork
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param the id of the \App\Models\Artwork $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show(Artwork $artwork)
|
||||
public function show(Request $request, $id)
|
||||
{
|
||||
//
|
||||
$user = auth()->user();
|
||||
$artwork = Artwork::where('id', $id)
|
||||
->with('podcast')
|
||||
->with('episode')
|
||||
->with('artist')
|
||||
->first();
|
||||
return view('artworks.artwork', [
|
||||
'artwork' => $artwork,
|
||||
'user' => $user,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,28 +4,51 @@ namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Models\Artwork;
|
||||
use App\Models\Artist;
|
||||
use App\Models\Episode;
|
||||
|
||||
|
||||
class PageController extends Controller
|
||||
{
|
||||
public function landing()
|
||||
public function landing(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
$headerCounters = $this->getHeaderCounters();
|
||||
$recentEpisodes = $this->mostRecentEpisodes();
|
||||
$recentSubmissions = $this->mostRecentSubmissions();
|
||||
$leaderboard = $this->leaderboardTwelveMonths();
|
||||
|
||||
return view('home.page', [
|
||||
'user' => $user,
|
||||
'pageTitle' => 'Home',
|
||||
'headerCounters' => $headerCounters,
|
||||
'recentEpisodes' => $recentEpisodes,
|
||||
'recentSubmissions' => $recentSubmissions,
|
||||
'leaderboard' => $leaderboard,
|
||||
'preferredTheme' => $request->session()->get('preferred_theme') ?? 'dark',
|
||||
]);
|
||||
}
|
||||
|
||||
private function mostRecentSubmissions() {
|
||||
$artworks = Cache::remember('latestSubmissions', 30, function() {
|
||||
return Artwork::whereNotNull('approved_by')
|
||||
->with('artist')
|
||||
->with('episode')
|
||||
->with('podcast')
|
||||
->orderBy('created_at', 'desc')
|
||||
->limit(50)
|
||||
->get();
|
||||
});
|
||||
return $artworks;
|
||||
}
|
||||
|
||||
private function mostRecentEpisodes()
|
||||
{
|
||||
$episodes = Cache::remember('latestEpisodes', 30, function() {
|
||||
return Episode::where('published', true)
|
||||
->whereHas('artwork')
|
||||
->with('podcast')
|
||||
->with('artwork')
|
||||
->with('artwork.artist')
|
||||
@@ -39,7 +62,7 @@ class PageController extends Controller
|
||||
private function getHeaderCounters()
|
||||
{
|
||||
$headerCounters = [];
|
||||
$artworkCountNumber = Cache::remember('artworkCountNumber', 10, function() {
|
||||
$artworkCountNumber = Cache::remember('artworkCountNumber', 10, function() {
|
||||
return Artwork::all()->count();
|
||||
});
|
||||
$artistCountNumber = Cache::remember('artistCountNumber', 10, function() {
|
||||
@@ -60,7 +83,7 @@ class PageController extends Controller
|
||||
$number /= 1000;
|
||||
}
|
||||
return [
|
||||
'number' => $this->numberFormatPrecision($number, 1), //number_format(floatval($number), 1),
|
||||
'number' => $this->numberFormatPrecision($number, 1), //number_format(floatval($number), 1),
|
||||
'unit' => $units[$i],
|
||||
];
|
||||
}
|
||||
@@ -76,4 +99,46 @@ class PageController extends Controller
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function leaderboardTwelveMonths() {
|
||||
$endDate = now()->endOfDay()->subYear()->format('Y-m-d');
|
||||
$leaderboard = DB::table('episodes')
|
||||
->join('artworks', 'artworks.id', '=', 'episodes.artwork_id')
|
||||
->join('artists', 'artists.id', '=', 'artworks.artist_id')
|
||||
->select([
|
||||
DB::raw('artists.id as artistId'),
|
||||
DB::raw('artists.name as artistName'),
|
||||
DB::raw('count(artworks.id) as artworkCount')
|
||||
])
|
||||
->where('episodes.published', 1)
|
||||
->where('episodes.episode_date', '>=', $endDate)
|
||||
->groupBy('artistId')
|
||||
->orderByDesc('artworkCount')
|
||||
->limit(10)
|
||||
->get();
|
||||
$leaderboard = $this->addArtistModelToLeaderboard($leaderboard);
|
||||
return $leaderboard;
|
||||
}
|
||||
|
||||
private function addArtistModelToLeaderboard($leaderboard) {
|
||||
$artistIds = [];
|
||||
foreach ($leaderboard as $lb) {
|
||||
$artistIds[] = $lb->artistId;
|
||||
}
|
||||
$artists = Artist::whereIn('id', $artistIds)->get();
|
||||
foreach ($leaderboard as $lb) {
|
||||
$lb->artist = $artists->where('id', $lb->artistId)->first();
|
||||
}
|
||||
$p = 0;
|
||||
foreach ($leaderboard as $lb) {
|
||||
$p++;
|
||||
$lb->position = $p;
|
||||
}
|
||||
return $leaderboard;
|
||||
}
|
||||
|
||||
public function setSessionTheme(Request $request) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ class Kernel extends HttpKernel
|
||||
],
|
||||
|
||||
'api' => [
|
||||
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
|
||||
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
|
||||
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user