Move most database interactions out of ReportController
MODIFY
app/Http/Controllers/ReportController.php
app/Http/Controllers/ReportController.php
namespace App\Http\Controllers;
-use App\Helper;
use App\Repositories\TagRepository;
+use App\Repositories\TransactionRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ReportController extends Controller {
- public function index() {
- return view('reports.index');
- }
-
- private function weeklyReport($year) {
- $weeks = [];
- $balance = 0;
-
- $weekMode = 3;
+ private $transactionRepository;
+ private $tagRepository;
- if (date('w', strtotime($year . '-01-01')) == 1) {
- $weekMode = 7;
+ public function __construct(TransactionRepository $transactionRepository, TagRepository $tagRepository)
+ {
+ $this->transactionRepository = $transactionRepository;
+ $this->tagRepository = $tagRepository;
}
- for ($i = 1; $i <= 53; $i ++) { // This used to be 52, IDK what happens after we moved it to 53
- $balance += session('space')
- ->earnings()
- ->whereRaw('YEAR(happened_on) = ? AND WEEK(happened_on, ?) = ?', [$year, $weekMode, $i])
- ->sum('amount');
-
- $balance -= session('space')
- ->spendings()
- ->whereRaw('YEAR(happened_on) = ? AND WEEK(happened_on, ?) = ?', [$year, $weekMode, $i])
- ->sum('amount');
-
- $weeks[$i] = Helper::formatNumber($balance / 100);
+ public function index() {
+ return view('reports.index');
}
+ private function weeklyReport($year) {
return view('reports.weekly_report', [
'year' => $year,
- 'weeks' => $weeks
+ 'weeks' => $this->transactionRepository->getWeeklyBalance($year)
]);
}
private function mostExpensiveTags() {
$totalSpent = session('space')->spendings()->sum('amount');
-
- $tagRepository = new TagRepository();
- $mostExpensiveTags = $tagRepository->getMostExpensiveTags(session('space')->id);
+ $mostExpensiveTags = $this->tagRepository->getMostExpensiveTags(session('space')->id);
return view('reports.most_expensive_tags', compact('totalSpent', 'mostExpensiveTags'));
}

MODIFY
app/Repositories/TransactionRepository.php
app/Repositories/TransactionRepository.php
namespace App\Repositories;
+use App\Helper;
use Carbon\Carbon;
class TransactionRepository {
+ public function getWeeklyBalance(string $year): array
+ {
+ $weeks = [];
+ $balance = 0;
+
+ $weekMode = 3;
+
+ if (date('w', strtotime($year . '-01-01')) == 1) {
+ $weekMode = 7;
+ }
+
+ for ($i = 1; $i <= 53; $i ++) { // This used to be 52, IDK what happens after we moved it to 53
+ $balance += session('space')
+ ->earnings()
+ ->whereRaw('YEAR(happened_on) = ? AND WEEK(happened_on, ?) = ?', [$year, $weekMode, $i])
+ ->sum('amount');
+
+ $balance -= session('space')
+ ->spendings()
+ ->whereRaw('YEAR(happened_on) = ? AND WEEK(happened_on, ?) = ?', [$year, $weekMode, $i])
+ ->sum('amount');
+
+ $weeks[$i] = Helper::formatNumber($balance / 100);
+ }
+
+ return $weeks;
+ }
+
public function getTransactionsByYearMonth(array $filterBy = []) {
$yearMonths = [];

Move most database interactions out of ReportController