Create SpendingRepository
MODIFY
app/Http/Controllers/ImportController.php
app/Http/Controllers/ImportController.php
@@ -5,12 +5,20 @@ namespace App\Http\Controllers;
use App\Helper;
use App\Models\Import;
use App\Models\Spending;
+use App\Repositories\SpendingRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
class ImportController extends Controller {
+ private $spendingRepository;
+
+ public function __construct(SpendingRepository $spendingRepository)
+ {
+ $this->spendingRepository = $spendingRepository;
+ }
+
public function index() {
return view('imports.index')->with([
'imports' => session('space')->imports()->orderBy('created_at', 'DESC')->get()
@@ -139,14 +147,14 @@ class ImportController extends Controller {
// TODO CHECK HOW THIS WORKS WITH 1k+ AMOUNTS
$amount = str_replace(',', '.', $row['amount']);
- Spending::create([
- 'space_id' => session('space')->id,
- 'import_id' => $import->id,
- 'tag_id' => $row['tag_id'],
- 'happened_on' => $row['happened_on'],
- 'description' => $row['description'],
- 'amount' => Helper::rawNumberToInteger($amount)
- ]);
+ $this->spendingRepository->create(
+ session('space')->id,
+ $import->id,
+ $row['tag_id'],
+ $row['happened_on'],
+ $row['description'],
+ $amount
+ );
}
}

MODIFY
app/Http/Controllers/SpendingController.php
app/Http/Controllers/SpendingController.php
namespace App\Http\Controllers;
use App\Helper;
+use App\Models\Spending;
+use App\Repositories\SpendingRepository;
use Illuminate\Http\Request;
-use App\Models\Spending;
-use App\Models\Tag;
+class SpendingController extends Controller {
+ private $spendingRepository;
-use Auth;
+ public function __construct(SpendingRepository $spendingRepository)
+ {
+ $this->spendingRepository = $spendingRepository;
+ }
-class SpendingController extends Controller {
public function create() {
$tags = session('space')->tags()->orderBy('created_at', 'DESC')->get();
@@ -27,22 +31,16 @@ class SpendingController extends Controller {
}
public function store(Request $request) {
- $request->validate([
- 'tag_id' => 'nullable|exists:tags,id', // TODO CHECK IF TAG BELONGS TO USER
- 'date' => 'required|date|date_format:Y-m-d',
- 'description' => 'required|max:255',
- 'amount' => 'required|regex:/^\d*(\.\d{2})?$/'
- ]);
-
- $spending = new Spending;
-
- $spending->space_id = session('space')->id;
- $spending->tag_id = $request->input('tag_id');
- $spending->happened_on = $request->input('date');
- $spending->description = $request->input('description');
- $spending->amount = Helper::rawNumberToInteger($request->input('amount'));
+ $request->validate($this->spendingRepository->getValidationRules());
- $spending->save();
+ $this->spendingRepository->create(
+ session('space')->id,
+ null,
+ $request->input('tag_id'),
+ $request->input('date'),
+ $request->input('description'),
+ Helper::rawNumberToInteger($request->input('amount'))
+ );
return redirect()->route('dashboard');
}
@@ -58,20 +56,14 @@ class SpendingController extends Controller {
public function update(Request $request, Spending $spending) {
$this->authorize('update', $spending);
- // TODO MOVE TO CENTRAL PLACE FOR REUSABILITY
- $request->validate([
- 'tag_id' => 'nullable|exists:tags,id', // TODO CHECK IF TAG BELONGS TO USER
- 'date' => 'required|date|date_format:Y-m-d',
- 'description' => 'required|max:255',
- 'amount' => 'required|regex:/^\d*(\.\d{2})?$/'
- ]);
+ $request->validate($this->spendingRepository->getValidationRules());
- $spending->fill([
+ $this->spendingRepository->update($spending->id, [
'tag_id' => $request->input('tag_id'),
'happened_on' => $request->input('date'),
'description' => $request->input('description'),
'amount' => Helper::rawNumberToInteger($request->input('amount'))
- ])->save();
+ ]);
return redirect()->route('transactions.index');
}

ADD
app/Repositories/SpendingRepository.php
+<?php
+
+namespace App\Repositories;
+
+use App\Models\Spending;
+use Exception;
+
+class SpendingRepository
+{
+ public function getValidationRules(): array
+ {
+ return [
+ 'tag_id' => 'nullable|exists:tags,id', // TODO CHECK IF TAG BELONGS TO USER
+ 'date' => 'required|date|date_format:Y-m-d',
+ 'description' => 'required|max:255',
+ 'amount' => 'required|regex:/^\d*(\.\d{2})?$/'
+ ];
+ }
+
+ public function create(int $spaceId, ?int $importId = null, ?int $tagId, string $date, string $description, int $amount): Spending
+ {
+ return Spending::create([
+ 'space_id' => $spaceId,
+ 'import_id' => $importId,
+ 'tag_id' => $tagId,
+ 'happened_on' => $date,
+ 'description' => $description,
+ 'amount' => $amount
+ ]);
+ }
+
+ public function update(int $spendingId, array $data): void
+ {
+ $spending = Spending::find($spendingId);
+
+ if (!$spending) {
+ throw new Exception('Could not find spending with ID ' . $spendingId);
+ }
+
+ $spending->fill($data)->save();
+ }
+}

Create SpendingRepository