@@ -7,12 +7,10 @@ use App\Knockout\Event; | |||
class EventController extends Controller { | |||
public function get(Request $request) | |||
public function get() | |||
{ | |||
$events = Event::all(); | |||
return view('page/events', [ | |||
'events' => $events | |||
'events' => Event::all()->getRecords() | |||
]); | |||
} | |||
@@ -10,10 +10,11 @@ class IndexController extends Controller { | |||
public function get(Request $request) | |||
{ | |||
$subforums = Subforum::all(); | |||
$records = $subforums->getRecords(); | |||
$len = count($subforums); | |||
$leftSubforums = array_slice($subforums, 0, $len / 2); | |||
$rightSubforums = array_slice($subforums, $len / 2); | |||
$len = count($records); | |||
$leftSubforums = array_slice($records, 0, $len / 2); | |||
$rightSubforums = array_slice($records, $len / 2); | |||
return view('page/index', [ | |||
'leftSubforums' => $leftSubforums, | |||
@@ -15,7 +15,8 @@ class ThreadController extends Controller { | |||
$thread = Thread::one($threadId, $page); | |||
return view('page/thread', [ | |||
'thread' => $thread | |||
'thread' => $thread->getRecord(), | |||
'paginator' => $thread->getPaginator($request) | |||
]); | |||
} | |||
@@ -12,25 +12,25 @@ class ThreadListController extends Controller { | |||
{ | |||
$subforumId = $request->route('subforum', null); | |||
$page = $request->route('page', 1); | |||
$subforum = Subforum::one($subforumId, $page); | |||
return view('page/subforum', [ | |||
'subforum' => $subforum | |||
'subforum' => $subforum->getRecord(), | |||
'paginator' => $subforum->getPaginator($request) | |||
]); | |||
} | |||
public function popular(Request $request) | |||
public function popular() | |||
{ | |||
return view('page/popular-threads', [ | |||
'threads' => Thread::popular() | |||
'threads' => Thread::popular()->getRecords() | |||
]); | |||
} | |||
public function latest(Request $request) | |||
public function latest() | |||
{ | |||
return view('page/latest-threads', [ | |||
'threads' => Thread::latest() | |||
'threads' => Thread::latest()->getRecords() | |||
]); | |||
} | |||
@@ -0,0 +1,81 @@ | |||
<?php | |||
namespace App\Knockout; | |||
use Illuminate\Http\Request; | |||
class Dataset | |||
{ | |||
private $records = []; | |||
private $totalRecords = null; | |||
private $currentPage = null; | |||
private $recordsPerPage = null; | |||
public function __construct($records) | |||
{ | |||
if (!is_array($records)) $records = [$records]; | |||
$this->records = $records; | |||
} | |||
public function setTotalRecords(int $total) | |||
{ | |||
$this->totalRecords = $total; | |||
return $this; | |||
} | |||
public function setCurrentPage(int $page) | |||
{ | |||
$this->currentPage = $page; | |||
return $this; | |||
} | |||
public function setRecordsPerPage(int $count) | |||
{ | |||
$this->recordsPerPage = $count; | |||
return $this; | |||
} | |||
public function getRecords() | |||
{ | |||
return $this->records; | |||
} | |||
public function getRecord() | |||
{ | |||
return $this->records[0] ?? null; | |||
} | |||
private function getPageCount() | |||
{ | |||
$exactPages = ($this->totalRecords / $this->recordsPerPage); | |||
$pages = ceil($exactPages); | |||
return ($pages == $exactPages) ? floor($exactPages) : $pages; | |||
} | |||
public function getPaginator(Request $request) | |||
{ | |||
if (is_null($this->totalRecords)) return null; | |||
if (is_null($this->currentPage)) return null; | |||
if (is_null($this->recordsPerPage)) return null; | |||
$route = $request->route(); | |||
$alias = $route[1]['as'] ?? 'index'; | |||
$params = $route[2] ?? []; | |||
$pages = array_map(function($page) use($alias, $params) { | |||
$currentPage = $params['page'] ?? 1; | |||
return (object) [ | |||
'number' => $page, | |||
'active' => ($page == $currentPage), | |||
'link' => route($alias, array_merge($params, ['page' => $page])) | |||
]; | |||
}, range(1, $this->getPageCount())); | |||
return view('partial/paginator', [ | |||
'pages' => $pages | |||
]); | |||
} | |||
} |
@@ -12,11 +12,6 @@ class Event { | |||
public $content; | |||
public $date; | |||
private static function unwrapContent($content) | |||
{ | |||
return []; | |||
} | |||
public static function unwrap($event) | |||
{ | |||
$s = new self(); | |||
@@ -37,23 +32,24 @@ class Event { | |||
return $s; | |||
} | |||
private static function requestAll() | |||
private static function requestAll(): Dataset | |||
{ | |||
$data = (new AbstractData)->httpGet('/events'); | |||
$json = json_decode($data); | |||
return array_map(function($event) { | |||
$records = array_map(function($event) { | |||
return self::unwrap($event); | |||
}, $json); | |||
return (new Dataset($records)); | |||
} | |||
public static function updateAll() | |||
public static function updateAll(): Dataset | |||
{ | |||
$data = self::requestAll(); | |||
Cache::put('events', $data, 3600); | |||
return $data; | |||
} | |||
public static function all() | |||
public static function all(): Dataset | |||
{ | |||
return Cache::get('events', function() { | |||
return self::requestAll(); | |||
@@ -34,37 +34,42 @@ class Subforum { | |||
return $s; | |||
} | |||
private static function requestAll() | |||
private static function requestAll(): Dataset | |||
{ | |||
$data = (new AbstractData)->httpGet('/subforum'); | |||
$json = json_decode($data); | |||
return array_map(function($subforum) { | |||
$records = array_map(function($subforum) { | |||
return self::unwrap($subforum); | |||
}, $json->list); | |||
return (new Dataset($records)); | |||
} | |||
public static function updateAll() | |||
public static function updateAll(): Dataset | |||
{ | |||
$data = self::requestAll(); | |||
Cache::forever('subforums', $data); | |||
return $data; | |||
} | |||
public static function all() | |||
public static function all(): Dataset | |||
{ | |||
return Cache::get('subforums', function() { | |||
return self::requestAll(); | |||
}); | |||
} | |||
private static function requestOne(int $subforumId, int $page = 1) | |||
private static function requestOne(int $subforumId, int $page = 1): Dataset | |||
{ | |||
$data = (new AbstractData)->httpGet(sprintf('/subforum/%s/%s', $subforumId, $page)); | |||
$json = json_decode($data); | |||
return self::unwrap($json); | |||
$record = self::unwrap($json); | |||
return (new Dataset($record)) | |||
->setTotalRecords($json->totalThreads) | |||
->setCurrentPage($json->currentPage) | |||
->setRecordsPerPage(40); | |||
} | |||
public static function updateOne(int $subforumId, int $page = 1) | |||
public static function updateOne(int $subforumId, int $page = 1): Dataset | |||
{ | |||
$key = sprintf('subforum-%u-%u', $subforumId, $page); | |||
$data = self::requestOne($subforumId, $page); | |||
@@ -72,7 +77,7 @@ class Subforum { | |||
return $data; | |||
} | |||
public static function one(int $subforumId, int $page = 1) | |||
public static function one(int $subforumId, int $page = 1): Dataset | |||
{ | |||
$key = sprintf('subforum-%u-%u', $subforumId, $page); | |||
return Cache::get($key, function() use($subforumId, $page) { | |||
@@ -3,6 +3,7 @@ | |||
namespace App\Knockout; | |||
use Illuminate\Support\Facades\Cache; | |||
use Symfony\Component\VarDumper\Cloner\Data; | |||
class Thread { | |||
@@ -48,14 +49,18 @@ class Thread { | |||
return $s; | |||
} | |||
private static function requestOne(int $threadId, int $page = 1) | |||
private static function requestOne(int $threadId, int $page = 1): Dataset | |||
{ | |||
$data = (new AbstractData)->httpGet(sprintf('/thread/%s/%s', $threadId, $page)); | |||
$json = json_decode($data); | |||
return self::unwrap($json); | |||
$record = self::unwrap($json); | |||
return (new Dataset($record)) | |||
->setTotalRecords($json->totalPosts) | |||
->setCurrentPage($json->currentPage) | |||
->setRecordsPerPage(40); | |||
} | |||
public static function updateOne(int $subforumId, int $page = 1) | |||
public static function updateOne(int $subforumId, int $page = 1): Dataset | |||
{ | |||
$key = sprintf('threads-%u-%u', $subforumId, $page); | |||
$data = self::requestOne($subforumId, $page); | |||
@@ -63,7 +68,7 @@ class Thread { | |||
return $data; | |||
} | |||
public static function one(int $subforumId, int $page = 1) | |||
public static function one(int $subforumId, int $page = 1): Dataset | |||
{ | |||
$key = sprintf('threads-%u-%u', $subforumId, $page); | |||
return Cache::get($key, function() use($subforumId, $page) { | |||
@@ -71,46 +76,48 @@ class Thread { | |||
}); | |||
} | |||
private static function requestPopular() | |||
private static function requestPopular(): Dataset | |||
{ | |||
$data = (new AbstractData)->httpGet('/thread/popular'); | |||
$json = json_decode($data); | |||
return array_map(function($thread) { | |||
$records = array_map(function($thread) { | |||
return self::unwrap($thread); | |||
}, $json->list); | |||
return (new Dataset($records)); | |||
} | |||
public static function updatePopular() | |||
public static function updatePopular(): Dataset | |||
{ | |||
$data = self::requestPopular(); | |||
Cache::forever('popular-threads', $data); | |||
return $data; | |||
} | |||
public static function popular() | |||
public static function popular(): Dataset | |||
{ | |||
return Cache::get('popular-threads', function() { | |||
return self::updatePopular(); | |||
}); | |||
} | |||
private static function requestLatest() | |||
private static function requestLatest(): Dataset | |||
{ | |||
$data = (new AbstractData)->httpGet('/thread/latest'); | |||
$json = json_decode($data); | |||
return array_map(function($thread) { | |||
$records = array_map(function($thread) { | |||
return self::unwrap($thread); | |||
}, $json->list); | |||
return (new Dataset($records)); | |||
} | |||
public static function updateLatest() | |||
public static function updateLatest(): Dataset | |||
{ | |||
$data = self::requestLatest(); | |||
Cache::forever('latest-threads', $data); | |||
return $data; | |||
} | |||
public static function latest() | |||
public static function latest(): Dataset | |||
{ | |||
return Cache::get('latest-threads', function() { | |||
return self::updateLatest(); | |||
@@ -4,7 +4,7 @@ | |||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", | |||
"This file is @generated automatically" | |||
], | |||
"content-hash": "caf91257a77d672a001170237e64fe29", | |||
"content-hash": "0ce9dd22cd325df894b40d30590f0cdb", | |||
"packages": [ | |||
{ | |||
"name": "brick/math", | |||
@@ -349,16 +349,16 @@ | |||
}, | |||
{ | |||
"name": "egulias/email-validator", | |||
"version": "2.1.24", | |||
"version": "2.1.25", | |||
"source": { | |||
"type": "git", | |||
"url": "https://github.com/egulias/EmailValidator.git", | |||
"reference": "ca90a3291eee1538cd48ff25163240695bd95448" | |||
"reference": "0dbf5d78455d4d6a41d186da50adc1122ec066f4" | |||
}, | |||
"dist": { | |||
"type": "zip", | |||
"url": "https://api.github.com/repos/egulias/EmailValidator/zipball/ca90a3291eee1538cd48ff25163240695bd95448", | |||
"reference": "ca90a3291eee1538cd48ff25163240695bd95448", | |||
"url": "https://api.github.com/repos/egulias/EmailValidator/zipball/0dbf5d78455d4d6a41d186da50adc1122ec066f4", | |||
"reference": "0dbf5d78455d4d6a41d186da50adc1122ec066f4", | |||
"shasum": "" | |||
}, | |||
"require": { | |||
@@ -405,7 +405,7 @@ | |||
], | |||
"support": { | |||
"issues": "https://github.com/egulias/EmailValidator/issues", | |||
"source": "https://github.com/egulias/EmailValidator/tree/2.1.24" | |||
"source": "https://github.com/egulias/EmailValidator/tree/2.1.25" | |||
}, | |||
"funding": [ | |||
{ | |||
@@ -413,7 +413,7 @@ | |||
"type": "github" | |||
} | |||
], | |||
"time": "2020-11-14T15:56:27+00:00" | |||
"time": "2020-12-29T14:50:06+00:00" | |||
}, | |||
{ | |||
"name": "graham-campbell/result-type", | |||
@@ -20,6 +20,12 @@ | |||
</div> | |||
</div> | |||
<div class="clearfix"></div> | |||
<div class="row stats"> | |||
<span>Users: 11682</span> | |||
<span>Threads: 19684</span> | |||
<span>Posts: 655765</span> | |||
<span>Ratings: 9013296</span> | |||
</div> | |||
@endsection | |||
@section('scripts') | |||
@@ -9,11 +9,14 @@ | |||
@endsection | |||
@section('content') | |||
<div class="row threads"> | |||
@foreach($subforum->threads as $key => $thread) | |||
@include('partial.thread') | |||
@endforeach | |||
</div> | |||
{!! $paginator !!} | |||
<div class="row threads"> | |||
@foreach($subforum->threads as $key => $thread) | |||
@include('partial.thread') | |||
@endforeach | |||
</div> | |||
<div class="row spacer"></div> | |||
{!! $paginator !!} | |||
@endsection | |||
@section('scripts') | |||
@@ -11,9 +11,11 @@ | |||
@endsection | |||
@section('content') | |||
{!! $paginator !!} | |||
@foreach($thread->posts as $post) | |||
@include('partial/post') | |||
@endforeach | |||
{!! $paginator !!} | |||
@endsection | |||
@section('scripts') | |||
@@ -1,9 +1,3 @@ | |||
<div class="row stats"> | |||
<span>Users: 11682</span> | |||
<span>Threads: 19684</span> | |||
<span>Posts: 655765</span> | |||
<span>Ratings: 9013296</span> | |||
</div> | |||
</div> | |||
<div class="footer"> | |||
<a class="left" href="/disclaimer">disclaimer</a> | |||
@@ -0,0 +1,6 @@ | |||
<div class="row pagination"> | |||
@foreach($pages as $page) | |||
<a class="{{ $page->active ? 'active' : null }}" href="{{ $page->link }}">{{ $page->number }}</a> | |||
@endforeach | |||
</div> | |||
<div class="row spacer"></div> |