Skip to content

Commit d71beaf

Browse files
committed
Added Blocks and Fixed Subscription Module
1 parent ef049fd commit d71beaf

20 files changed

+780
-518
lines changed

database/migrations/2023_05_06_190730_update_subscriptions_table.php

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public function up()
2424
$table->string('schedule')->nullable()->after('is_downgrade');
2525
$table->dateTime('cancels_at')->nullable()->after('ends_at');
2626
$table->dateTime('expires_at')->nullable()->after('ends_at');
27+
28+
$table->unsignedBigInteger('user_id')->nullable()->change();
29+
$table->foreign('user_id')->references('id')->on('users')->cascadeOnUpdate()->nullOnDelete();
2730
});
2831

2932
$this->setAutoIncrement('subscriptions');

database/migrations/2024_04_23_205120_create_pages_table.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function up(): void
1818
$table->id();
1919
$table->string('title');
2020
$table->string('slug')->unique();
21-
$table->{$this->jsonable()}('data')->nullable();
21+
$table->binary('data')->nullable();
2222
$table->longText('body')->nullable();
2323
$table->longText('styles')->nullable();
2424
$table->string('meta_title')->nullable();
@@ -35,7 +35,7 @@ public function up(): void
3535
Schema::create('page_templates', function (Blueprint $table) {
3636
$table->id();
3737
$table->string('name');
38-
$table->{$this->jsonable()}('data')->nullable();
38+
$table->binary('data')->nullable();
3939
$table->text('thumbnail')->nullable();
4040
$table->timestamps();
4141
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Coderstm\Traits\Helpers;
4+
use Illuminate\Database\Migrations\Migration;
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Schema;
7+
8+
return new class extends Migration
9+
{
10+
use Helpers;
11+
12+
/**
13+
* Run the migrations.
14+
*/
15+
public function up(): void
16+
{
17+
Schema::create('page_blocks', function (Blueprint $table) {
18+
$table->id();
19+
$table->binary('data')->nullable();
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::dropIfExists('page_blocks');
30+
}
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Coderstm\Http\Controllers\Page;
4+
5+
use Illuminate\Http\Request;
6+
use Coderstm\Models\Page\Block;
7+
use Coderstm\Http\Controllers\Controller;
8+
use Illuminate\Http\Resources\Json\ResourceCollection;
9+
10+
class BlockController extends Controller
11+
{
12+
/**
13+
* Create the controller instance.
14+
*
15+
* @return void
16+
*/
17+
public function __construct()
18+
{
19+
// $this->authorizeResource(Block::class);
20+
}
21+
22+
/**
23+
* Display a listing of the resource.
24+
*/
25+
public function index(Request $request)
26+
{
27+
$block = Block::query();
28+
29+
$block = $block->orderBy($request->sortBy ?? 'created_at', $request->direction ?? 'desc');
30+
31+
if ($request->isNotFilled('rowsPerPage')) {
32+
return $block->get();
33+
}
34+
35+
return new ResourceCollection($block->paginate($request->rowsPerPage ?? 15));
36+
}
37+
38+
/**
39+
* Store a newly created resource in storage.
40+
*/
41+
public function store(Request $request)
42+
{
43+
// Set rules
44+
$rules = [
45+
'data' => 'array|required',
46+
'data.key' => 'string|required',
47+
'data.options' => 'array|required',
48+
];
49+
50+
// Validate those rules
51+
$request->validate($rules);
52+
53+
// create the block
54+
$block = Block::create($request->input());
55+
56+
return response()->json([
57+
'data' => $block,
58+
'message' => trans_module('store', 'block'),
59+
], 200);
60+
}
61+
62+
/**
63+
* Remove the specified resource from storage.
64+
*/
65+
public function destroy(Block $block)
66+
{
67+
$block->delete();
68+
69+
return response()->json([
70+
'message' => trans_module('destroy', 'block'),
71+
], 200);
72+
}
73+
}

src/Http/Controllers/Page/TemplateController.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,27 @@ public function __construct()
2525
/**
2626
* Display a listing of the resource.
2727
*/
28-
public function index(Request $request, Template $template)
28+
public function index(Request $request)
2929
{
30-
$template = $template->query();
30+
$template = Template::query();
3131

3232
if ($request->filled('filter')) {
3333
$template->where('name', 'like', "%{$request->filter}%");
3434
}
3535

36-
if ($request->rowsPerPage == -1) {
36+
$template = $template->orderBy($request->sortBy ?? 'created_at', $request->direction ?? 'desc');
37+
38+
if ($request->isNotFilled('rowsPerPage')) {
3739
return $template->get();
3840
}
3941

40-
$template = $template->orderBy($request->sortBy ?? 'created_at', $request->direction ?? 'desc');
41-
4242
return new ResourceCollection($template->paginate($request->rowsPerPage ?? 15));
4343
}
4444

4545
/**
4646
* Store a newly created resource in storage.
4747
*/
48-
public function store(Request $request, Template $template)
48+
public function store(Request $request)
4949
{
5050
// Set rules
5151
$rules = [
@@ -102,6 +102,7 @@ public function show(Template $template)
102102
public function destroy(Template $template)
103103
{
104104
$template->delete();
105+
105106
return response()->json([
106107
'message' => trans_module('destroy', 'template'),
107108
], 200);

src/Http/Controllers/PageController.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Coderstm\Models\Page;
66
use Illuminate\Http\Request;
77
use Coderstm\Http\Controllers\Controller;
8+
use Coderstm\Http\Resources\PageCollection;
89
use Illuminate\Http\Resources\Json\ResourceCollection;
910

1011
class PageController extends Controller
@@ -24,7 +25,16 @@ public function __construct()
2425
*/
2526
public function index(Request $request, Page $page)
2627
{
27-
$page = $page->query();
28+
$page = $page->query()->select([
29+
'id',
30+
'title',
31+
'slug',
32+
'meta_title',
33+
'meta_keywords',
34+
'meta_description',
35+
'is_active',
36+
'template',
37+
]);
2838

2939
if ($request->filled('filter')) {
3040
$page->where(function ($query) use ($request) {
@@ -47,7 +57,7 @@ public function index(Request $request, Page $page)
4757

4858
$page = $page->orderBy($request->sortBy ?? 'created_at', $request->direction ?? 'desc')
4959
->paginate($request->rowsPerPage ?? 15);
50-
return new ResourceCollection($page);
60+
return new PageCollection($page);
5161
}
5262

5363
/**

src/Http/Resources/PageCollection.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Coderstm\Http\Resources;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\ResourceCollection;
7+
8+
class PageCollection extends ResourceCollection
9+
{
10+
/**
11+
* Transform the resource collection into an array.
12+
*
13+
* @return array<int|string, mixed>
14+
*/
15+
public function toArray(Request $request): array
16+
{
17+
return parent::toArray($request);
18+
}
19+
}

src/Http/Resources/PageResource.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Coderstm\Http\Resources;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
8+
class PageResource extends JsonResource
9+
{
10+
/**
11+
* Transform the resource into an array.
12+
*
13+
* @return array<string, mixed>
14+
*/
15+
public function toArray(Request $request): array
16+
{
17+
$this->makeHidden(['data', 'body', 'styles']);
18+
19+
return parent::toArray($request);
20+
}
21+
}

src/Models/Page.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66
use Spatie\Sluggable\HasSlug;
77
use Spatie\Sluggable\SlugOptions;
88
use Illuminate\Support\Facades\DB;
9+
use Coderstm\Traits\JsonCompressible;
910
use Illuminate\Support\Facades\Blade;
1011
use Illuminate\Database\Eloquent\Model;
12+
use Illuminate\Database\Eloquent\Casts\Attribute;
1113

1214
class Page extends Model
1315
{
14-
use Core, HasSlug;
16+
use Core, HasSlug, JsonCompressible;
1517

1618
protected $dates = ['created_at', 'updated_at'];
1719

20+
protected $logIgnore = ['body', 'styles'];
21+
1822
protected $fillable = [
1923
'title',
2024
'slug',
@@ -30,9 +34,19 @@ class Page extends Model
3034

3135
protected $casts = [
3236
'is_active' => 'boolean',
33-
'data' => 'json',
3437
];
3538

39+
/**
40+
* Interact with the model's JSON data column.
41+
*/
42+
protected function data(): Attribute
43+
{
44+
return Attribute::make(
45+
get: fn(string $value) => $this->uncompress($value),
46+
set: fn(array $value) => gzcompress(json_encode($value))
47+
);
48+
}
49+
3650
public function getSlugOptions(): SlugOptions
3751
{
3852
return SlugOptions::create()

src/Models/Page/Block.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Coderstm\Models\Page;
4+
5+
use Coderstm\Traits\Logable;
6+
use Coderstm\Traits\SerializeDate;
7+
use Coderstm\Traits\JsonCompressible;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\Casts\Attribute;
10+
use Illuminate\Database\Eloquent\Factories\HasFactory;
11+
12+
class Block extends Model
13+
{
14+
use HasFactory, Logable, SerializeDate, JsonCompressible;
15+
16+
protected $table = 'page_blocks';
17+
18+
protected $fillable = [
19+
'data',
20+
];
21+
22+
/**
23+
* Interact with the model's JSON data column.
24+
*/
25+
protected function data(): Attribute
26+
{
27+
return Attribute::make(
28+
get: fn(string $value) => $this->uncompress($value),
29+
set: fn(array $value) => gzcompress(json_encode($value))
30+
);
31+
}
32+
}

src/Models/Page/Template.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,31 @@
44

55
use Coderstm\Traits\Logable;
66
use Coderstm\Traits\SerializeDate;
7-
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Coderstm\Traits\JsonCompressible;
88
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\Casts\Attribute;
10+
use Illuminate\Database\Eloquent\Factories\HasFactory;
911

1012
class Template extends Model
1113
{
12-
use Logable, HasFactory, SerializeDate;
14+
use Logable, HasFactory, SerializeDate, JsonCompressible;
1315

1416
protected $table = 'page_templates';
15-
protected $dates = ['created_at', 'updated_at'];
1617

1718
protected $fillable = [
1819
'name',
1920
'thumbnail',
2021
'data',
2122
];
2223

23-
protected $casts = [
24-
'data' => 'json',
25-
];
24+
/**
25+
* Interact with the model's JSON data column.
26+
*/
27+
protected function data(): Attribute
28+
{
29+
return Attribute::make(
30+
get: fn(string $value) => $this->uncompress($value),
31+
set: fn(array $value) => gzcompress(json_encode($value))
32+
);
33+
}
2634
}

0 commit comments

Comments
 (0)