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

Tweet query

parent 1969795e
No related branches found
No related tags found
No related merge requests found
<?php
namespace App\Tweets;
class TweetQuery {
public $year;
public $month;
public $date;
public $search;
public function forYear($year)
{
$this->year = $year;
}
public function forMonth($month)
{
$this->month = $month;
}
public function forDate($date)
{
$this->date = $date;
}
public function search($search)
{
$this->search = $search;
}
}
\ No newline at end of file
Loading
Loading
@@ -9,6 +9,39 @@ class TweetRepository {
 
protected static $paginate = 50;
 
public function all(TweetQuery $query)
{
$db = Tweet::latest('time');
$date = Carbon::createFromDate($query->year, $query->month, $query->date);
$start = null;
$end = null;
if ($query->date) {
$start = $date->copy()->startOfDay();
$end = $date->copy()->endOfDay();
}
elseif ($query->month) {
$start = $date->copy()->startOfMonth();
$end = $date->copy()->endOfMonth();
}
elseif ($query->year) {
$start = $date->copy()->startOfYear();
$end = $date->copy()->endOfYear();
}
if ($start && $end) {
$db->where('time', '>=', $start->getTimestamp())
->where('time', '<=', $end->getTimestamp());
}
if ($query->search) {
$db->where('text', 'LIKE', "%$query->search%");
}
return [$db->pluck('id')->toArray(), $db->paginate(self::$paginate)];
}
/**
* Get all tweets
*
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