Skip to content

Commit

Permalink
Merge pull request #56 from saleem-hadad/feat/move-search-to-interface
Browse files Browse the repository at this point in the history
implement search as interface and added to transactions #feature
  • Loading branch information
saleem-hadad committed Mar 28, 2024
2 parents 4bba51a + e251973 commit d455016
Show file tree
Hide file tree
Showing 59 changed files with 173 additions and 200 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions app/Contracts/Searchable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Contracts;

use Illuminate\Database\Eloquent\Builder;

interface Searchable
{
public function search($query): Builder;
}
9 changes: 3 additions & 6 deletions app/GraphQL/Directives/SearchDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\GraphQL\Directives;

use App\Models\Transaction;
use App\Contracts\Searchable;
use Illuminate\Database\Eloquent\Builder;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective;
Expand All @@ -29,11 +29,8 @@ public static function definition(): string
*/
public function handleBuilder($builder, $value): Builder
{
if($builder->getModel() instanceof Transaction) {
return $builder->where('amount', 'LIKE', "%$value%")
->orWhereHas('brand', function($builder) use($value) {
return $builder->where('name', 'LIKE', "%$value%");
})->orWhere('note', 'LIKE', "%$value%");
if($builder->getModel() instanceof Searchable) {
return $builder->getModel()->search($value);
}

return $builder;
Expand Down
34 changes: 25 additions & 9 deletions app/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace App\Models;

use Carbon\Carbon;
use App\Contracts\Searchable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Transaction extends Model
class Transaction extends Model implements Searchable
{
use HasFactory;

Expand All @@ -15,52 +17,52 @@ class Transaction extends Model
protected $casts = [
'meta' => 'array',
];

public function brand()
{
return $this->belongsTo(Brand::class);
}

public function scopeExpenses($query)
public function scopeExpenses($query)
{
return $query->whereHas('brand.category', function ($query) {
return $query->where('type', Category::EXPENSES);
});
}

public function scopeIncome($query)
public function scopeIncome($query)
{
return $query->whereHas('brand.category', function ($query) {
return $query->where('type', Category::INCOME);
});
}

public function scopeSavings($query)
public function scopeSavings($query)
{
return $query->whereHas('brand.category', function ($query) {
return $query->where('type', Category::SAVINGS);
});
}

public function scopeInvestment($query)
public function scopeInvestment($query)
{
return $query->whereHas('brand.category', function ($query) {
return $query->where('type', Category::INVESTMENT);
});
}

public static function tryCreateFromSms($sms)
public static function tryCreateFromSms($sms)
{
$brandFromSms = $sms->meta['data']['brand'] ?? null;
$amountFromSms = $sms->meta['data']['amount'] ?? null;
$transactionDatetimeFromSMS = $sms->meta['data']['datetime'] ?? null;

if(! $brandFromSms || ! $amountFromSms) {
return;
}

$brand = Brand::findOrCreateNew($brandFromSms);

$amount = (float) filter_var($amountFromSms, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$transactionDatetime = $transactionDatetimeFromSMS ? Carbon::parse($transactionDatetimeFromSMS) : now();

Expand All @@ -70,4 +72,18 @@ public static function tryCreateFromSms($sms)
'created_at' => $transactionDatetime
]);
}

/**
* @param $query
* @return Builder
*/
public function search($query): Builder
{
return $this->newQuery()
->where('amount', 'LIKE', "%$query%")
->orWhere('note', 'LIKE', "%$query%")
->orWhereHas('brand', function($builder) use($query) {
return $builder->where('name', 'LIKE', "%$query%");
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Auth;

use App\Models\User;
use App\Providers\RouteServiceProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Brands;

use Tests\TestCase;
use App\Models\Brand;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AllBrandsTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Brands;

use Tests\TestCase;
use App\Models\Brand;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class BrandsTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Brands;

use Tests\TestCase;
use App\Models\Brand;
use App\Models\Category;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CreateBrandTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Brands;

use Tests\TestCase;
use App\Models\Brand;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DeleteBrandTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Brands;

use Tests\TestCase;
use App\Models\Brand;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UpdateBrandTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Categories;

use Tests\TestCase;
use App\Models\Category;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AllCategoriesTest extends TestCase
{
Expand All @@ -17,8 +17,8 @@ public function it_returns_correct_data()

$this->graphQL(/** @lang GraphQL */ '
{
allCategories {
id
allCategories {
id
name
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Categories;

use App\Models\Category;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CategoriesTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Categories;

use Tests\TestCase;
use App\Models\Category;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CreateCategoryTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Categories;

use Tests\TestCase;
use App\Models\Category;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DeleteCategoryTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\Categories;

use Tests\TestCase;
use App\Models\Category;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UpdateCategoryTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\GraphQL\Queries;

use Tests\TestCase;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExpensesPerCategoryTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\GraphQL\Queries;

use Tests\TestCase;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class HighestValueTransactionTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\GraphQL\Queries;

use Tests\TestCase;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class IncomePerCategoryTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\GraphQL\Queries;

use Tests\TestCase;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class LowestValueTransactionTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\GraphQL\Queries;

use Tests\TestCase;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class NumberOfTransactionsPerBrandTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\GraphQL\Queries;

use Tests\TestCase;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class NumberOfTransactionsPerCategoryTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\GraphQL\Queries;

use Tests\TestCase;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class NumberOfTransactionsTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Tests\Feature;
namespace Tests\Feature\GraphQL\Queries;

use Tests\TestCase;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class TotalCashTest extends TestCase
{
Expand Down
Loading

0 comments on commit d455016

Please sign in to comment.