From b6890ce2c2ef91df51fb2c4ffc24c04ce2ad0610 Mon Sep 17 00:00:00 2001 From: Braydon Kains Date: Sun, 31 Mar 2019 22:49:30 -0400 Subject: [PATCH] committing because I'm paranoid --- app/Http/Controllers/ThreadController.php | 15 +++++-- app/Post.php | 7 ++- app/Tag.php | 14 +++++- app/Thread.php | 14 ++++++ app/User.php | 21 +++++++++ .../2019_03_30_224135_create_forums_table.php | 31 ------------- ...2019_03_30_224150_create_threads_table.php | 9 ++++ .../2019_03_30_224204_create_posts_table.php | 15 +++++++ .../2019_03_30_233001_create_tags_table.php | 9 ++++ .../2019_03_31_172224_tag_thread.php | 45 +++++++++++++++++++ database/seeds/PostsTableSeeder.php | 16 +++++++ database/seeds/ThreadsTableSeeder.php | 32 +++++++++++++ database/seeds/UsersTableSeeder.php | 28 ++++++++++++ resources/js/app.js | 2 +- resources/js/components/LoginComponent.vue | 5 --- resources/js/components/ThreadComponent.vue | 33 ++++++++++++++ routes/api.php | 2 + 17 files changed, 255 insertions(+), 43 deletions(-) delete mode 100644 database/migrations/2019_03_30_224135_create_forums_table.php create mode 100644 database/migrations/2019_03_31_172224_tag_thread.php create mode 100644 database/seeds/PostsTableSeeder.php create mode 100644 database/seeds/ThreadsTableSeeder.php create mode 100644 database/seeds/UsersTableSeeder.php delete mode 100644 resources/js/components/LoginComponent.vue create mode 100644 resources/js/components/ThreadComponent.vue diff --git a/app/Http/Controllers/ThreadController.php b/app/Http/Controllers/ThreadController.php index f49b135..ad2d265 100644 --- a/app/Http/Controllers/ThreadController.php +++ b/app/Http/Controllers/ThreadController.php @@ -14,7 +14,9 @@ class ThreadController extends Controller */ public function index() { - // + $threads = Thread::orderBy('created_at', 'desc')->paginate(20); + + return Thread::collection($threads); } /** @@ -24,7 +26,7 @@ class ThreadController extends Controller */ public function create() { - // + return view("create_thread"); } /** @@ -35,7 +37,12 @@ class ThreadController extends Controller */ public function store(Request $request) { - // + $thread = new Thread; + + $thread->thread_title = $request->thread_title; + $thread->thread_poster_id = $request->user_id; + + $thread->save(); } /** @@ -46,7 +53,7 @@ class ThreadController extends Controller */ public function show(Thread $thread) { - // + return view("thread_view")->withThread($thread); } /** diff --git a/app/Post.php b/app/Post.php index 44fdf5d..f76dd9e 100644 --- a/app/Post.php +++ b/app/Post.php @@ -6,5 +6,10 @@ use Illuminate\Database\Eloquent\Model; class Post extends Model { - // + /** + * Get the poster of the post. + */ + public function poster() { + return $this->belongsTo('App\User', 'poster_id'); + } } diff --git a/app/Tag.php b/app/Tag.php index 47aa704..79434d1 100644 --- a/app/Tag.php +++ b/app/Tag.php @@ -6,5 +6,17 @@ use Illuminate\Database\Eloquent\Model; class Tag extends Model { - // + /** + * Get threads that have this tag. + */ + public function threads() { + return $this->belongsToMany('App\Thread'); + } + + /** + * Get creator of this tag. + */ + public function creator() { + return $this->belongsTo('App\User', 'creator_id'); + } } diff --git a/app/Thread.php b/app/Thread.php index 2ddcad9..1cded13 100644 --- a/app/Thread.php +++ b/app/Thread.php @@ -7,4 +7,18 @@ use Illuminate\Database\Eloquent\Model; class Thread extends Model { // + + /** + * Get the creator of the thread. + */ + public function creator() { + return $this->belongsTo('App\User', 'thread_creator_id'); + } + + /** + * Get the tags for the thread. + */ + public function tags() { + return $this->belongsToMany('App\Tag'); + } } diff --git a/app/User.php b/app/User.php index faa03c3..c570259 100644 --- a/app/User.php +++ b/app/User.php @@ -36,4 +36,25 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + /** + * Get the threads the user has posted. + */ + public function threads() { + return $this->hasMany('App\Thread'); + } + + /** + * Get the posts the user has made. + */ + public function posts() { + return $this->hasMany('App\Post'); + } + + /** + * Get the tags the user has created. + */ + public function tags() { + return $this->hasMany('App\Tag'); + } } diff --git a/database/migrations/2019_03_30_224135_create_forums_table.php b/database/migrations/2019_03_30_224135_create_forums_table.php deleted file mode 100644 index 1d219ff..0000000 --- a/database/migrations/2019_03_30_224135_create_forums_table.php +++ /dev/null @@ -1,31 +0,0 @@ -bigIncrements('id'); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('forums'); - } -} diff --git a/database/migrations/2019_03_30_224150_create_threads_table.php b/database/migrations/2019_03_30_224150_create_threads_table.php index 4a5c5f3..141a6b4 100644 --- a/database/migrations/2019_03_30_224150_create_threads_table.php +++ b/database/migrations/2019_03_30_224150_create_threads_table.php @@ -14,8 +14,17 @@ class CreateThreadsTable extends Migration public function up() { Schema::create('threads', function (Blueprint $table) { + //Columns $table->bigIncrements('id'); + $table->string('thread_title'); + $table->unsignedBigInteger('thread_creator_id')->nullable(); $table->timestamps(); + + //Key constraints + $table->foreign('thread_creator_id')-> + references('id')-> + on('users')-> + onDelete('set null'); }); } diff --git a/database/migrations/2019_03_30_224204_create_posts_table.php b/database/migrations/2019_03_30_224204_create_posts_table.php index 7e7ef6a..34366fb 100644 --- a/database/migrations/2019_03_30_224204_create_posts_table.php +++ b/database/migrations/2019_03_30_224204_create_posts_table.php @@ -14,8 +14,23 @@ class CreatePostsTable extends Migration public function up() { Schema::create('posts', function (Blueprint $table) { + //Columns $table->bigIncrements('id'); + $table->string('content'); + $table->unsignedBigInteger('poster_id')->nullable(); + $table->unsignedBigInteger('thread_id'); $table->timestamps(); + + //Key constraints + $table->foreign('poster_id')-> + references('id')-> + on('users')-> + onDelete('set null'); + + $table->foreign('thread_id')-> + references('id')-> + on('threads')-> + onDelete('cascade'); }); } diff --git a/database/migrations/2019_03_30_233001_create_tags_table.php b/database/migrations/2019_03_30_233001_create_tags_table.php index 21659c9..826d13b 100644 --- a/database/migrations/2019_03_30_233001_create_tags_table.php +++ b/database/migrations/2019_03_30_233001_create_tags_table.php @@ -14,8 +14,17 @@ class CreateTagsTable extends Migration public function up() { Schema::create('tags', function (Blueprint $table) { + //Columns $table->bigIncrements('id'); + $table->string('tag_text'); + $table->unsignedBigInteger('creator_id')->nullable(); $table->timestamps(); + + //Key Constraints + $table->foreign('creator_id') + ->references('id') + ->on('users') + ->onDelete('set null'); }); } diff --git a/database/migrations/2019_03_31_172224_tag_thread.php b/database/migrations/2019_03_31_172224_tag_thread.php new file mode 100644 index 0000000..920fd59 --- /dev/null +++ b/database/migrations/2019_03_31_172224_tag_thread.php @@ -0,0 +1,45 @@ +bigIncrements('id'); + $table->unsignedBigInteger('tag_id'); + $table->foreign('tag_id') + ->references('id') + ->on('tags') + ->onDelete('cascade'); + + $table->unsignedBigInteger('thread_id'); + $table->foreign('thread_id') + ->references('id') + ->on('threads') + ->onDelete('cascade'); + + //Key constraints + + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('tag_thread'); + } +} diff --git a/database/seeds/PostsTableSeeder.php b/database/seeds/PostsTableSeeder.php new file mode 100644 index 0000000..0237ab7 --- /dev/null +++ b/database/seeds/PostsTableSeeder.php @@ -0,0 +1,16 @@ +insert([ + 'thread_title' => $titles[$i], + 'thread_creator_id' => $i+2 + ]); + } + + DB::table('threads')->insert([ + 'thread_title' => "Welcome to GameGab!", + 'thread_creator_id' => 1 + ]); + } +} diff --git a/database/seeds/UsersTableSeeder.php b/database/seeds/UsersTableSeeder.php new file mode 100644 index 0000000..7be805f --- /dev/null +++ b/database/seeds/UsersTableSeeder.php @@ -0,0 +1,28 @@ +insert([ + 'name' => 'GameGabTeam', + 'email' => Str::random(10).'@gmail.com', + 'password' => bcrypt('secret'), + ]); + + for($i=0; $i<10; $i++) { + DB::table('users')->insert([ + 'name' => Str::random(10), + 'email' => Str::random(10).'@gmail.com', + 'password' => bcrypt('secret'), + ]); + } + } +} diff --git a/resources/js/app.js b/resources/js/app.js index 4131ca0..dc8570e 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -20,7 +20,7 @@ window.Vue = require('vue'); // const files = require.context('./', true, /\.vue$/i); // files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default)); -Vue.component('example-component', require('./components/ExampleComponent.vue').default); +Vue.component('thread-component', require('./components/ThreadComponent.vue').default); /** * Next, we will create a fresh Vue application instance and attach it to diff --git a/resources/js/components/LoginComponent.vue b/resources/js/components/LoginComponent.vue deleted file mode 100644 index a86a891..0000000 --- a/resources/js/components/LoginComponent.vue +++ /dev/null @@ -1,5 +0,0 @@ - \ No newline at end of file diff --git a/resources/js/components/ThreadComponent.vue b/resources/js/components/ThreadComponent.vue new file mode 100644 index 0000000..059c729 --- /dev/null +++ b/resources/js/components/ThreadComponent.vue @@ -0,0 +1,33 @@ + + + \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index c641ca5..8c2120f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -16,3 +16,5 @@ use Illuminate\Http\Request; Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); }); + +Route::get('threads', 'ThreadController@index'); \ No newline at end of file