From 12f67c4e4e6a1b6e17231e589572499ecf5511d0 Mon Sep 17 00:00:00 2001 From: Dym Sohin Date: Sun, 11 Aug 2024 11:53:49 +0200 Subject: [PATCH] progress save --- app/Models/Article.php | 19 ++++++++++ app/Models/Tag.php | 11 ++++++ app/Models/User.php | 5 +++ database/factories/TagFactory.php | 23 ++++++++++++ .../2024_08_09_112756_create_articles.php | 15 ++++---- .../2024_08_09_115812_create_categories.php | 30 ++++++++++++++++ .../2024_08_09_115915_create_tags.php | 35 +++++++++++++++++++ public/css/style.css | 1 + resources/views/components/input.blade.php | 11 ++++++ resources/views/edit.blade.php | 34 +++++++++++++----- routes/web.php | 28 +++++++++++++++ 11 files changed, 197 insertions(+), 15 deletions(-) create mode 100644 app/Models/Tag.php create mode 100644 database/factories/TagFactory.php create mode 100644 database/migrations/2024_08_09_115812_create_categories.php create mode 100644 database/migrations/2024_08_09_115915_create_tags.php diff --git a/app/Models/Article.php b/app/Models/Article.php index cadd27e..c3d5c0b 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -9,6 +9,7 @@ use Illuminate\Support\Arr; class Article extends Model { protected $table = 'articles'; + protected $fillable = [ 'title', 'slug', 'body' ]; use HasFactory; @@ -23,4 +24,22 @@ class Article extends Model return $article; } + + public static function findById(string $id): array + { + $article = Arr::first(Article::all(), fn($article) => $article['id'] === $id); + + if(! $article ) { + abort(404); + } + + return $article; + } + + + public function user_id() + { + return $this->belongsTo(User::class); + } + } diff --git a/app/Models/Tag.php b/app/Models/Tag.php new file mode 100644 index 0000000..6b7af87 --- /dev/null +++ b/app/Models/Tag.php @@ -0,0 +1,11 @@ + 'hashed', ]; } + + public function articles() + { + return $this->hasMany(Article::class); + } } diff --git a/database/factories/TagFactory.php b/database/factories/TagFactory.php new file mode 100644 index 0000000..ad98a0b --- /dev/null +++ b/database/factories/TagFactory.php @@ -0,0 +1,23 @@ + + */ +class TagFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2024_08_09_112756_create_articles.php b/database/migrations/2024_08_09_112756_create_articles.php index a1df494..a15d1ba 100644 --- a/database/migrations/2024_08_09_112756_create_articles.php +++ b/database/migrations/2024_08_09_112756_create_articles.php @@ -13,16 +13,17 @@ return new class extends Migration { Schema::create('articles', function (Blueprint $table) { $table->id(); - $table->string('slug'); + $table->string('slug')->unique(); $table->string('title'); $table->text('body'); - $table->integer('category'); + $table->integer('user_id')->references('id')->on('users'); + $table->integer('category')->references('id')->on('categories'); $table->boolean('is_collection'); - $table->integer('tag_1'); - $table->integer('tag_2'); - $table->integer('tag_3'); - $table->integer('tag_4'); - $table->integer('tag_5'); + $table->integer('tag_1')->references('id')->on('tags'); + $table->integer('tag_2')->references('id')->on('tags'); + $table->integer('tag_3')->references('id')->on('tags'); + $table->integer('tag_4')->references('id')->on('tags'); + $table->integer('tag_5')->references('id')->on('tags'); $table->timestamps(); }); } diff --git a/database/migrations/2024_08_09_115812_create_categories.php b/database/migrations/2024_08_09_115812_create_categories.php new file mode 100644 index 0000000..d1c1b22 --- /dev/null +++ b/database/migrations/2024_08_09_115812_create_categories.php @@ -0,0 +1,30 @@ +id(); + $table->string('name'); + $table->string('color'); + $table->integer('order_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('categories'); + } +}; diff --git a/database/migrations/2024_08_09_115915_create_tags.php b/database/migrations/2024_08_09_115915_create_tags.php new file mode 100644 index 0000000..099a9e2 --- /dev/null +++ b/database/migrations/2024_08_09_115915_create_tags.php @@ -0,0 +1,35 @@ +id(); + $table->string('name'); + $table->string('color'); + $table->timestamps(); + }); + Schema::create('article_tag', function (Blueprint $table) { + $table->id(); + $table->foreignIdFor(\App\Models\Article::class, 'articles'); + $table->foreignIdFor(\App\Models\Tag::class); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('tags'); + } +}; diff --git a/public/css/style.css b/public/css/style.css index 13095b0..4d67d4a 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -575,6 +575,7 @@ ul > li::before ; padding : 0.5rem ; border-radius : 0.5rem ; justify-content : center +; align-items : center ; min-width: 16rem } diff --git a/resources/views/components/input.blade.php b/resources/views/components/input.blade.php index bb6991c..5ffe018 100644 --- a/resources/views/components/input.blade.php +++ b/resources/views/components/input.blade.php @@ -4,6 +4,17 @@ {{ $attributes->merge() }} > +@elseif( 'checkbox' == $attributes['type'] ) +
+ + + +
+ @elseif( 'textarea' == $attributes['type'] )
-
+ @csrf - - - id + title + - + required>slug + body + + category + + is_collection + + tag_1 + tag_2 + tag_3 + tag_4 + tag_5 + diff --git a/routes/web.php b/routes/web.php index 1ce552f..52031fa 100644 --- a/routes/web.php +++ b/routes/web.php @@ -24,6 +24,21 @@ Route::view('/notes', 'notes'); Route::get('/new', function () { return view('new'); }); +Route::post('/new', function (string $title = '', string $slug = '', string $body = '') { + $article = Article::create( + [ 'title' => $title + , 'slug' => $slug + , 'body' => $body + ]); + + return view('edit', + [ 'article' => $article + , 'title' => $article['title'] + , 'slug' => $article['slug'] + , 'body' => $article['body'] + ]); + +}); Route::get('/edit/{slug}', function (string $slug) { $article = Article::find($slug); @@ -36,6 +51,19 @@ Route::get('/edit/{slug}', function (string $slug) { ]); })->where('slug', '[A-Za-z0-9-]+'); +Route::post('/edit/{id}', function (string $id) { + $article = Article::findById($id); + + return view('edit', + [ 'article' => $article + , 'title' => $article['title'] + , 'slug' => $article['slug'] + , 'body' => $article['body'] + ]); +})->where('id', '[0-9-]+'); + + + Route::get('/articles', function () { return view('articles', [ 'articles' => Article::all(),