Browse Source

Add Circle controller,model and migration

Daniel Supernault 6 years ago
parent
commit
e00f65da03

+ 38 - 0
app/Circle.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Circle extends Model
+{
+    protected $fillable = [
+    	'name',
+    	'description',
+    	'bcc',
+    	'scope',
+    	'active'
+    ];
+
+    public function members()
+    {
+    	return $this->hasManyThrough(
+    		Profile::class,
+    		CircleProfile::class,
+    		'circle_id',
+    		'id',
+    		'id',
+    		'profile_id'
+    	);
+    }
+
+    public function owner()
+    {
+    	return $this->belongsTo(Profile::class, 'profile_id');
+    }
+
+    public function url()
+    {
+        return url("/i/circle/show/{$this->id}");
+    }
+}

+ 69 - 0
app/Http/Controllers/CircleController.php

@@ -0,0 +1,69 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Http\Request;
+use Illuminate\Validation\Rule;
+use Auth;
+use App\{
+	Circle,
+	CircleProfile,
+	Profile,
+	Status,
+};
+
+class CircleController extends Controller
+{
+    public function __construct()
+    {
+    	$this->middleware('auth');
+    }
+
+    public function home(Request $request)
+    {
+    	$circles = Circle::whereProfileId(Auth::user()->profile->id)
+    		->orderByDesc('created_at')
+    		->paginate(10);
+    	return view('account.circles.home', compact('circles'));
+    }
+
+    public function create(Request $request)
+    {
+    	return view('account.circles.create');
+    }
+
+    public function store(Request $request)
+    {
+    	$this->validate($request, [
+    		'name' => 'required|string|min:1',
+    		'description' => 'nullable|string|max:255',
+    		'scope' => [
+    			'required',
+    			'string',
+    			Rule::in([
+    				'public',
+    				'private',
+    				'unlisted',
+    				'exclusive'
+    			])
+    		],
+    	]);
+
+    	$circle = Circle::firstOrCreate([
+    		'profile_id' => Auth::user()->profile->id,
+    		'name' => $request->input('name')
+    	], [
+    		'description' => $request->input('description'),
+    		'scope' => $request->input('scope'),
+    		'active' => false
+    	]);
+
+    	return redirect(route('account.circles'));
+    }
+
+    public function show(Request $request, $id)
+    {
+        $circle = Circle::findOrFail($id);
+    	return view('account.circles.show', compact('circle'));
+    }
+}

+ 37 - 0
database/migrations/2019_02_09_045935_create_circles_table.php

@@ -0,0 +1,37 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateCirclesTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('circles', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->bigInteger('profile_id')->unsigned()->index();
+            $table->string('name')->nullable();
+            $table->text('description')->nullable();
+            $table->string('scope')->default('public');
+            $table->boolean('bcc')->default(false);
+            $table->boolean('active')->default(false)->index();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('circles');
+    }
+}