Skip to content

Commit

Permalink
Merge pull request #18 from dcblogdev/refactored-filtered-load-from-m…
Browse files Browse the repository at this point in the history
…odel

moved filters to model
  • Loading branch information
dcblogdev authored May 6, 2024
2 parents 02bb1e2 + 7af5c9c commit e3009bb
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 32 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
],
"require": {
"illuminate/support": "9.x|10.x|11.x",
"citco/carbon": "^2.0",
"ext-zlib": "*"
},
"require-dev": {
Expand Down
34 changes: 5 additions & 29 deletions src/Controllers/SentEmailsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
20 changes: 19 additions & 1 deletion src/Models/SentEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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%");
});
}
}
61 changes: 60 additions & 1 deletion tests/SentEmailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]']);
SentEmail::factory()->create();

$this->get(route('sentemails.index', ['from' => '[email protected]']))
->assertOk()
->assertViewHas('emails', function ($emails) {
return $emails->count() === 1;
}
);
});

test('can filter emails by to', function () {

SentEmail::factory()->create(['to' => '[email protected]']);
SentEmail::factory()->create();

$this->get(route('sentemails.index', ['to' => '[email protected]']))
->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 () {
Expand Down

0 comments on commit e3009bb

Please sign in to comment.