From 6acf71fc5b87df62047d24294da8452e24cd8599 Mon Sep 17 00:00:00 2001 From: David Carr Date: Mon, 6 May 2024 12:54:41 +0100 Subject: [PATCH 1/2] moved filters to model --- src/Controllers/SentEmailsController.php | 34 ++----------- src/Models/SentEmail.php | 20 +++++++- tests/SentEmailsTest.php | 61 +++++++++++++++++++++++- 3 files changed, 84 insertions(+), 31 deletions(-) diff --git a/src/Controllers/SentEmailsController.php b/src/Controllers/SentEmailsController.php index 587ae6f..8f99c5f 100644 --- a/src/Controllers/SentEmailsController.php +++ b/src/Controllers/SentEmailsController.php @@ -2,43 +2,19 @@ namespace Dcblogdev\LaravelSentEmails\Controllers; -use Citco\Carbon; use Dcblogdev\LaravelSentEmails\Models\SentEmail; use Dcblogdev\LaravelSentEmails\Models\SentEmailAttachment; use Illuminate\Contracts\View\View; +use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\BinaryFileResponse; class SentEmailsController { - public function index(): View + public function index(Request $request): View { - $emails = SentEmail::orderby('id', 'desc'); - - if (request()->exists('date')) { - $date = request('date'); - $from = request('from'); - $to = request('to'); - $subject = request('subject'); - - if ($date != '') { - $date = Carbon::parse($date)->format('Y-m-d'); - $emails->where('date', '=', $date); - } - - if ($from != '') { - $emails->where('from', 'like', "%$from%"); - } - - if ($to != '') { - $emails->where('to', 'like', "%$to%"); - } - - if ($subject != '') { - $emails->where('subject', 'like', "%$subject%"); - } - } - - $emails = $emails->paginate(config('sentemails.perPage')); + $emails = SentEmail::orderby('id', 'desc') + ->applyFilters($request) + ->paginate(config('sentemails.perPage')); return view('sentemails::index', compact('emails')); } diff --git a/src/Models/SentEmail.php b/src/Models/SentEmail.php index eb721af..22bd3f2 100644 --- a/src/Models/SentEmail.php +++ b/src/Models/SentEmail.php @@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Http\Request; +use Illuminate\Support\Carbon; class SentEmail extends Model { @@ -46,8 +48,24 @@ public function attachments(): HasMany return $this->hasMany(SentEmailAttachment::class); } - public function applyFilters() + public function scopeApplyFilters($query, Request $request) { + $date = $request->input('date'); + $from = $request->input('from'); + $to = $request->input('to'); + $subject = $request->input('subject'); + $query->when($date, function ($query, $date) { + $query->where('date', '=', Carbon::parse($date)->format('Y-m-d')); + }) + ->when($from, function ($query, $from) { + $query->where('from', 'like', "%$from%"); + }) + ->when($to, function ($query, $to) { + $query->where('to', 'like', "%$to%"); + }) + ->when($subject, function ($query, $subject) { + $query->where('subject', 'like', "%$subject%"); + }); } } diff --git a/tests/SentEmailsTest.php b/tests/SentEmailsTest.php index 913c3ee..9d34089 100644 --- a/tests/SentEmailsTest.php +++ b/tests/SentEmailsTest.php @@ -4,7 +4,66 @@ use Dcblogdev\LaravelSentEmails\Models\SentEmailAttachment; test('can see emails inbox', function () { - $this->get(route('sentemails.index'))->assertOk(); + SentEmail::factory()->count(10)->create(); + $this->get(route('sentemails.index')) + ->assertOk() + ->assertViewHas('emails', function ($emails) { + return $emails->count() === 10; + } + ); +}); + +test('can filter emails by date', function () { + + SentEmail::factory()->create(['date' => '2023-10-20']); + SentEmail::factory()->create(['date' => '2023-10-20']); + SentEmail::factory()->create(); + + $this->get(route('sentemails.index', ['date' => '2023-10-20'])) + ->assertOk() + ->assertViewHas('emails', function ($emails) { + return $emails->count() === 2; + } + ); +}); + +test('can filter emails by from', function () { + + SentEmail::factory()->create(['from' => 'demo@demo.com']); + SentEmail::factory()->create(); + + $this->get(route('sentemails.index', ['from' => 'demo@demo.com'])) + ->assertOk() + ->assertViewHas('emails', function ($emails) { + return $emails->count() === 1; + } + ); +}); + +test('can filter emails by to', function () { + + SentEmail::factory()->create(['to' => 'demo@demo.com']); + SentEmail::factory()->create(); + + $this->get(route('sentemails.index', ['to' => 'demo@demo.com'])) + ->assertOk() + ->assertViewHas('emails', function ($emails) { + return $emails->count() === 1; + } + ); +}); + +test('can filter emails by subject', function () { + + SentEmail::factory()->create(['subject' => 'Demo']); + SentEmail::factory()->create(['subject' => 'Other']); + + $this->get(route('sentemails.index', ['subject' => 'Demo'])) + ->assertOk() + ->assertViewHas('emails', function ($emails) { + return $emails->count() === 1; + } + ); }); test('can see email', function () { From 7af5c9ca1403b776b97ca210695e2f9f924503cb Mon Sep 17 00:00:00 2001 From: David Carr Date: Mon, 6 May 2024 12:55:36 +0100 Subject: [PATCH 2/2] moved filters to model --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index cf2ff87..fe199ff 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,6 @@ ], "require": { "illuminate/support": "9.x|10.x|11.x", - "citco/carbon": "^2.0", "ext-zlib": "*" }, "require-dev": {