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

Get rid of over-engineered scopes #481

Merged
merged 1 commit into from
Dec 30, 2023
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
4 changes: 3 additions & 1 deletion app/Http/Controllers/ActivityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class ActivityController extends Controller
{
public function index()
{
$activities = Activity::ofSpace(session('space_id'))->get();
$activities = Activity::query()
->where('space_id', session('space_id'))
->get();

return view('activities.index', ['activities' => $activities]);
}
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Api/ActivitiesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public function __invoke(Request $request)
/** @var ApiKey $apiKey */
$apiKey = $request->get('apiKey');

$activities = Activity::ofSpace($apiKey->user->spaces()->first()->id)->get();
$activities = Activity::query()
->where('space_id', $apiKey->user->spaces()->first()->id)
->get();

return ActivityResource::collection($activities);
}
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/BudgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public function index()

public function create()
{
$tags = Tag::ofSpace(session('space_id'))->latest()->get();
$tags = Tag::query()
->where('space_id', session('space_id'))
->latest()
->get();

return view('budgets.create', ['tags' => $tags]);
}
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(SpendingRepository $spendingRepository)
public function index()
{
return view('imports.index')->with([
'imports' => Import::ofSpace(session('space_id'))->latest()->get()
'imports' => Import::query()->where('space_id', session('space_id'))->latest()->get()
]);
}

Expand Down Expand Up @@ -97,7 +97,9 @@ public function getComplete(Import $import)
{
$this->authorize('modify', $import);

$tags = Tag::ofSpace(session('space_id'))->get();
$tags = Tag::query()
->where('space_id', session('space_id'))
->get();

$file = fopen(storage_path('app/imports/' . $import->file), 'r');

Expand Down
7 changes: 5 additions & 2 deletions app/Http/Controllers/RecurringController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public function __construct(RecurringRepository $recurringRepository)

public function index()
{
$recurrings = Recurring::ofSpace(session('space_id'))->latest()->get();
$recurrings = Recurring::query()
->where('space_id', session('space_id'))
->latest()
->get();

return view('recurrings.index', ['recurrings' => $recurrings]);
}
Expand All @@ -36,7 +39,7 @@ public function create()
{
$tags = [];

foreach (Tag::ofSpace(session('space_id'))->get() as $tag) {
foreach (Tag::query()->where('space_id', session('space_id'))->get() as $tag) {
$tags[] = ['key' => $tag->id, 'label' => $tag->name];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private function weeklyReport($year)

private function mostExpensiveTags()
{
$totalSpent = Spending::ofSpace(session('space_id'))->sum('amount');
$totalSpent = Spending::query()->where('space_id', session('space_id'))->sum('amount');
$mostExpensiveTags = $this->tagRepository->getMostExpensiveTags(session('space_id'));

return view('reports.most_expensive_tags', compact('totalSpent', 'mostExpensiveTags'));
Expand Down
10 changes: 8 additions & 2 deletions app/Http/Controllers/SpendingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public function __construct(

public function create()
{
$tags = Tag::ofSpace(session('space_id'))->latest()->get();
$tags = Tag::query()
->where('space_id', session('space_id'))
->latest()
->get();

return view('spendings.create', ['tags' => $tags]);
}
Expand Down Expand Up @@ -71,7 +74,10 @@ public function edit(Spending $spending)
{
$this->authorize('edit', $spending);

$tags = Tag::ofSpace(session('space_id'))->latest()->get();
$tags = Tag::query()
->where('space_id', session('space_id'))
->latest()
->get();

return view('spendings.edit', compact('tags', 'spending'));
}
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public function __construct(TagRepository $tagRepository)

public function index()
{
$tags = Tag::ofSpace(session('space_id'))->latest()->get();
$tags = Tag::query()
->where('space_id', session('space_id'))
->latest()
->get();

return view('tags.index', ['tags' => $tags]);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/TransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public function index(Request $request)

return view('transactions.index', [
'yearMonths' => $this->repository->getTransactionsByYearMonth($filterBy),
'tags' => Tag::ofSpace(session('space_id'))->get()
'tags' => Tag::query()->where('space_id', session('space_id'))->get(),
]);
}

public function create()
{
$tags = [];

foreach (Tag::ofSpace(session('space_id'))->get() as $tag) {
foreach (Tag::query()->where('space_id', session('space_id'))->get() as $tag) {
$tags[] = [
'key' => $tag->id,
'label' => '<div class="row"><div class="row__column row__column--compact row__column--middle mr-1"><div style="width: 15px; height: 15px; border-radius: 2px; background: #' . $tag->color . ';"></div></div><div class="row__column row__column--middle">' . $tag->name . '</div></div>' // phpcs:ignore
Expand Down
6 changes: 0 additions & 6 deletions app/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,4 @@ public function user()
{
return $this->belongsTo(User::class);
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,4 @@ protected function isoLowercased(): Attribute
{
return Attribute::make(fn () => strtolower($this->iso));
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Earning.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,4 @@ public function attachments()
return $this->hasMany(Attachment::class, 'transaction_id')
->where('transaction_type', 'earning');
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,4 @@ public function spendings()
{
return $this->hasMany(Spending::class);
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,4 @@ public function tag()
{
return $this->belongsTo(Tag::class);
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Spending.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,4 @@ public function attachments()
return $this->hasMany(Attachment::class, 'transaction_id')
->where('transaction_type', 'spending');
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,4 @@ public static function randomColor()
{
return self::randomColorPart() . self::randomColorPart() . self::randomColorPart();
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
9 changes: 6 additions & 3 deletions app/Repositories/DashboardRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function getLeftToSpend(string $year, string $month)
// TODO MOVE TO SPENDINGREPOSITORY IN FUTURE
public function getTotalAmountSpent(string $year, string $month)
{
return Spending::ofSpace(session('space_id'))
return Spending::query()
->where('space_id', session('space_id'))
->whereRaw('YEAR(happened_on) = ? AND MONTH(happened_on) = ?', [$year, $month])
->sum('amount');
}
Expand All @@ -43,11 +44,13 @@ public function getDailyBalance(int $spaceId, string $year, string $month): arra
$dailyBalance = [];

for ($i = 1; $i <= $daysInMonth; $i++) {
$balanceTick -= Spending::ofSpace($spaceId)
$balanceTick -= Spending::query()
->where('space_id', $spaceId)
->where('happened_on', $year . '-' . $month . '-' . $i)
->sum('amount');

$balanceTick += Earning::ofSpace($spaceId)
$balanceTick += Earning::query()
->where('space_id', $spaceId)
->where('happened_on', $year . '-' . $month . '-' . $i)
->sum('amount');

Expand Down
10 changes: 6 additions & 4 deletions app/Repositories/TransactionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public function getWeeklyBalance(string $year): array
}

for ($i = 1; $i <= 53; $i++) { // This used to be 52, IDK what happens after we moved it to 53
$balance += Earning::ofSpace(session('space_id'))
$balance += Earning::query()
->where('space_id', session('space_id'))
->whereRaw('YEAR(happened_on) = ? AND WEEK(happened_on, ?) = ?', [$year, $weekMode, $i])
->sum('amount');

$balance -= Spending::ofSpace(session('space_id'))
$balance -= Spending::query()
->where('space_id', session('space_id'))
->whereRaw('YEAR(happened_on) = ? AND WEEK(happened_on, ?) = ?', [$year, $weekMode, $i])
->sum('amount');

Expand All @@ -40,7 +42,7 @@ public function getTransactionsByYearMonth(array $filterBy = [])
$yearMonths = [];

// Populate yearMonths with earnings
foreach (Earning::ofSpace(session('space_id'))->get() as $earning) {
foreach (Earning::query()->where('space_id', session('space_id'))->get() as $earning) {
$shouldAdd = false;

if (!$filterBy) {
Expand All @@ -59,7 +61,7 @@ public function getTransactionsByYearMonth(array $filterBy = [])
}

// Populate yearMonths with spendings
foreach (Spending::ofSpace(session('space_id'))->get() as $spending) {
foreach (Spending::query()->where('space_id', session('space_id'))->get() as $spending) {
$shouldAdd = true;

// Filter
Expand Down
9 changes: 6 additions & 3 deletions app/Widgets/Spent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public function render()
$currencySymbol = $space->currency->symbol;

if ($this->properties->period === 'today') {
$spent = Spending::ofSpace(session('space_id'))
$spent = Spending::query()
->where('space_id', session('space_id'))
->whereRaw('DATE(happened_on) = ?', [date('Y-m-d')])
->sum('amount');
}
Expand All @@ -31,13 +32,15 @@ public function render()
$monday = date('Y-m-d', strtotime('monday this week'));
$sunday = date('Y-m-d', strtotime('sunday this week'));

$spent = Spending::ofSpace(session('space_id'))
$spent = Spending::query()
->where('space_id', session('space_id'))
->whereRaw('DATE(happened_on) >= ? AND DATE(happened_ON) <= ?', [$monday, $sunday])
->sum('amount');
}

if ($this->properties->period === 'this_month') {
$spent = Spending::ofSpace(session('space_id'))
$spent = Spending::query()
->where('space_id', session('space_id'))
->whereRaw('YEAR(happened_on) = ? AND MONTH(happened_on) = ?', [date('Y'), date('n')])
->sum('amount');
}
Expand Down