Skip to content

Commit d8799e9

Browse files
v0.0.1
1 parent 288d2a1 commit d8799e9

22 files changed

+3481
-5
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Snippet;
6+
7+
class SnippetController extends Controller
8+
{
9+
public function index()
10+
{
11+
$snippets = Snippet::paginate(10); // Adjust the number for pagination
12+
return view('snippets.index', compact('snippets'));
13+
}
14+
15+
public function embed($uid)
16+
{
17+
// Find the snippet by its UID
18+
$snippet = Snippet::with('codes.lang')->where('uid', $uid)->firstOrFail();
19+
20+
// Different views based on the snippet type
21+
switch ($snippet->type_name) {
22+
case 'single':
23+
return view('snippets.single', compact('snippet'));
24+
case 'multi':
25+
return view('snippets.multi', compact('snippet'));
26+
// $relatedSnippets = Snippet::where('type', 'multi')->where('id', '!=', $snippet->id)->get(); // Get related multi snippets
27+
// return view('snippets.multi', compact('snippet', 'relatedSnippets'));
28+
default:
29+
abort(404); // Handle unknown snippet types
30+
}
31+
}
32+
33+
public function show($uid) {
34+
return $this->embed($uid);
35+
}
36+
}

app/Models/Code.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Code extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $fillable = ['snippet_id', 'lang_id', 'code'];
13+
14+
public function snippet()
15+
{
16+
return $this->belongsTo(Snippet::class);
17+
}
18+
19+
public function lang()
20+
{
21+
return $this->belongsTo(Lang::class);
22+
}
23+
24+
// Custom accessor to get the lang name
25+
public function getLangNameAttribute()
26+
{
27+
return $this->lang ? $this->lang->name : null; // Return the name or null if no lang exists
28+
}
29+
}

app/Models/Lang.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Lang extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $fillable = ['name', 'logo'];
13+
14+
public function codes()
15+
{
16+
return $this->hasMany(Code::class);
17+
}
18+
}

app/Models/Snippet.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Snippet extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $fillable = ['snippet_type_id', 'uid', 'name'];
13+
14+
public function type()
15+
{
16+
return $this->belongsTo(SnippetType::class, 'snippet_type_id');
17+
}
18+
19+
public function codes()
20+
{
21+
return $this->hasMany(Code::class);
22+
}
23+
24+
// Custom accessor to get the type name
25+
public function getTypeNameAttribute()
26+
{
27+
return $this->type ? $this->type->name : null; // Return the name or null if no type exists
28+
}
29+
30+
}

app/Models/SnippetType.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class SnippetType extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $fillable = ['name'];
13+
14+
public function snippets()
15+
{
16+
return $this->hasMany(Snippet::class, 'snippet_type_id');
17+
}
18+
}

coderog-snippets

96 KB
Binary file not shown.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateLangsTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('langs', function (Blueprint $table) {
12+
$table->id();
13+
$table->string('name')->unique();
14+
$table->string('logo')->nullable();
15+
$table->timestamps();
16+
});
17+
}
18+
19+
public function down()
20+
{
21+
Schema::dropIfExists('langs');
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateSnippetTypesTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('snippet_types', function (Blueprint $table) {
12+
$table->id();
13+
$table->string('name')->unique(); // Unique name for the snippet type
14+
$table->timestamps();
15+
});
16+
}
17+
18+
public function down()
19+
{
20+
Schema::dropIfExists('snippet_types');
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateSnippetsTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('snippets', function (Blueprint $table) {
12+
$table->id();
13+
$table->foreignId('snippet_type_id')->constrained('snippet_types')->onDelete('cascade'); // Foreign key for snippet types
14+
$table->string('uid', 8)->unique();
15+
$table->string('name');
16+
$table->timestamps();
17+
});
18+
}
19+
20+
public function down()
21+
{
22+
Schema::dropIfExists('snippets');
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateCodesTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('codes', function (Blueprint $table) {
12+
$table->id();
13+
$table->foreignId('snippet_id')->constrained()->onDelete('cascade');
14+
$table->foreignId('lang_id')->constrained('langs')->onDelete('cascade');
15+
$table->longText('code');
16+
$table->timestamps();
17+
});
18+
}
19+
20+
public function down()
21+
{
22+
Schema::dropIfExists('codes');
23+
}
24+
}
25+

0 commit comments

Comments
 (0)