Skip to content
Snippets Groups Projects
Unverified Commit fff0a7f7 authored by Robb Lewis's avatar Robb Lewis
Browse files

Better count querying, better tweet querying

parent 1fa8b3b4
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -3,6 +3,7 @@
namespace App\Http\Controllers;
 
use App\Breadcrumbs\BreadcrumbInterface;
use App\Tweets\TweetQuery;
use App\Tweets\TweetRepository;
use Illuminate\Support\Facades\Input;
 
Loading
Loading
@@ -24,15 +25,25 @@ class TweetController {
$this->breadcrumbs->addCrumb('All Tweets', '/');
}
 
public function index()
public function index($year = null, $month = null, $date = null)
{
$tweets = $this->tweets->getAll();
$query = new TweetQuery();
$monthCounts = null;
$dayCounts = null;
if ($date) $query->forDate($date);
if ($month) $query->forMonth($month);
if ($year) $query->forYear($year);
 
$monthCounts = $this->tweets->monthCount();
list($ids, $tweets) = $this->tweets->all($query);
if (! $month) $monthCounts = $this->tweets->monthCount($ids);
if ($month && ! $date) $dayCounts = $this->tweets->dayCount($ids);
 
return view('tweets.index', compact(
'tweets',
'monthCounts'
'monthCounts',
'dayCounts'
));
}
 
Loading
Loading
@@ -56,33 +67,6 @@ class TweetController {
return $this->show($tweet->tweetid);
}
 
public function date($year, $month = null, $day = null)
{
$monthCounts = null;
$dayCounts = null;
$this->breadcrumbs->addCrumb($year, $year);
if ($month) $this->breadcrumbs->addCrumb(displayMonth($month), $month);
if ($day) $this->breadcrumbs->addCrumb(displayDate($day), $day);
$tweets = $this->tweets->getForDate($year, $month, $day);
if ( ! $month)
{
$monthCounts = $this->tweets->monthCountForYear($year);
}
elseif ( ! $day)
{
$dayCounts = $this->tweets->dayCountForMonth($year, $month);
}
return view('tweets.index', compact(
'tweets',
'monthCounts',
'dayCounts'
));
}
public function search()
{
$search = Input::get('search');
Loading
Loading
Loading
Loading
@@ -9,8 +9,10 @@ class TweetRepository {
 
protected static $paginate = 50;
 
public function all(TweetQuery $query)
public function all(TweetQuery $query = null)
{
if (!$query) $query = new TweetQuery();
$db = Tweet::latest('time');
 
$date = Carbon::createFromDate($query->year, $query->month, $query->date);
Loading
Loading
@@ -42,16 +44,6 @@ class TweetRepository {
return [$db->pluck('id')->toArray(), $db->paginate(self::$paginate)];
}
 
/**
* Get all tweets
*
* @return array Tweets
*/
public function getAll()
{
return Tweet::orderBy('time', 'desc')->paginate(self::$paginate);
}
/**
* Find tweet by id
*
Loading
Loading
@@ -209,33 +201,22 @@ class TweetRepository {
->paginate(self::$paginate);
}
 
public function monthCount()
{
$counts = DB::select(DB::raw('select Year(FROM_UNIXTIME(time)) as year, Month(FROM_UNIXTIME(time)) as month, Count(*) as count
FROM tweets
GROUP BY Year(FROM_UNIXTIME(time)), Month(FROM_UNIXTIME(time))
ORDER BY Year(FROM_UNIXTIME(time)) desc, Month(FROM_UNIXTIME(time)) desc'));
return $this->calculatePercentagesAndTotal($counts);
}
public function monthCountForYear($year)
public function monthCount($ids = null)
{
$counts = DB::select(DB::raw('select Year(FROM_UNIXTIME(time)) as year, Month(FROM_UNIXTIME(time)) as month, Count(*) as count
FROM tweets
where year(FROM_UNIXTIME(time)) = '.$year.'
' . ($ids ? 'where id in (' . implode(',', $ids) . ')' : '') . '
GROUP BY Year(FROM_UNIXTIME(time)), Month(FROM_UNIXTIME(time))
ORDER BY Year(FROM_UNIXTIME(time)) desc, Month(FROM_UNIXTIME(time)) desc'));
 
return $this->calculatePercentagesAndTotal($counts);
}
 
public function dayCountForMonth($year, $month)
public function dayCount($ids = null)
{
$counts = DB::select(DB::raw('select Year(FROM_UNIXTIME(time)) as year, Month(FROM_UNIXTIME(time)) as month, Day(FROM_UNIXTIME(time)) as day, Count(*) as count
FROM tweets
where year(FROM_UNIXTIME(time)) = '.$year.'
and month(FROM_UNIXTIME(time)) = '.$month.'
' . ($ids ? 'where id in (' . implode(',', $ids) . ')' : '') . '
GROUP BY Year(FROM_UNIXTIME(time)), Month(FROM_UNIXTIME(time)), Day(FROM_UNIXTIME(time))'));
 
return $this->calculatePercentagesAndTotal($counts);
Loading
Loading
Loading
Loading
@@ -25,10 +25,10 @@ Route::group(['middleware' => ['private']], function() {
Route::get('/tweet', function() { return redirect('/random'); });
Route::get('/random', 'TweetController@random');
 
Route::get('{year}/{month?}/{day?}', 'TweetController@date')->where([
Route::get('{year}/{month?}/{date?}', 'TweetController@index')->where([
'year' => '[0-9]+',
'month' => '[0-9]+',
'day' => '[0-9]+'
'date' => '[0-9]+'
]);
 
Route::get('/search', function() { return redirect('/'); });
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment