style updates
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
<?php
|
||||
|
||||
return ['name' => 'News'];
|
||||
|
||||
@@ -1,3 +1,40 @@
|
||||
<?php
|
||||
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;
|
||||
return new class extends Migration { public function up(): void { Schema::create('news_articles', function(Blueprint $table): void { $table->id(); $table->foreignId('author_id')->nullable()->constrained('authors')->nullOnDelete(); $table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete(); $table->string('title'); $table->string('slug')->unique(); $table->text('excerpt')->nullable(); $table->longText('content')->nullable(); $table->string('featured_image')->nullable(); $table->string('status')->default('draft')->index(); $table->boolean('is_featured')->default(false); $table->timestamp('published_at')->nullable()->index(); $table->timestamp('expires_at')->nullable(); $table->string('meta_title')->nullable(); $table->text('meta_description')->nullable(); $table->timestamps(); }); Schema::create('news_article_tag', function(Blueprint $table): void { $table->foreignId('news_article_id')->constrained('news_articles')->cascadeOnDelete(); $table->foreignId('tag_id')->constrained('tags')->cascadeOnDelete(); $table->primary(['news_article_id','tag_id']); }); } public function down(): void { Schema::dropIfExists('news_article_tag'); Schema::dropIfExists('news_articles'); } };
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('news_articles', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('author_id')->nullable()->constrained('authors')->nullOnDelete();
|
||||
$table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete();
|
||||
$table->string('title');
|
||||
$table->string('slug')->unique();
|
||||
$table->text('excerpt')->nullable();
|
||||
$table->longText('content')->nullable();
|
||||
$table->string('featured_image')->nullable();
|
||||
$table->string('status')->default('draft')->index();
|
||||
$table->boolean('is_featured')->default(false);
|
||||
$table->timestamp('published_at')->nullable()->index();
|
||||
$table->timestamp('expires_at')->nullable();
|
||||
$table->string('meta_title')->nullable();
|
||||
$table->text('meta_description')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
Schema::create('news_article_tag', function (Blueprint $table): void {
|
||||
$table->foreignId('news_article_id')->constrained('news_articles')->cascadeOnDelete();
|
||||
$table->foreignId('tag_id')->constrained('tags')->cascadeOnDelete();
|
||||
$table->primary(['news_article_id', 'tag_id']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('news_article_tag');
|
||||
Schema::dropIfExists('news_articles');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,4 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\News\Http\Controllers;
|
||||
use App\Http\Controllers\Controller; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\View\View; use Modules\Authors\Models\Author; use Modules\Categories\Models\Category; use Modules\Tags\Models\Tag; use Modules\News\Models\News;
|
||||
final class NewsController extends Controller { public function index(): View { return view('news::admin.index',['records'=>News::with(['author','category'])->latest()->paginate(25)]); } public function create(): View { return view('news::admin.form',$this->lookups()); } public function store(Request $r): RedirectResponse { $record=News::create($this->data($r)); $record->tags()->sync($r->input('tag_ids',[])); return redirect()->route('administration.news.index')->with('status','News created.'); } public function edit(News $news_article): View { return view('news::admin.form',array_merge($this->lookups(),['record'=>$news_article])); } public function update(Request $r, News $news_article): RedirectResponse { $news_article->update($this->data($r)); $news_article->tags()->sync($r->input('tag_ids',[])); return redirect()->route('administration.news.index')->with('status','News updated.'); } public function destroy(News $news_article): RedirectResponse { $news_article->delete(); return back()->with('status','News deleted.'); } private function lookups(): array { return ['authors'=>Author::where('is_active',true)->orderBy('name')->get(),'categories'=>Category::where('is_active',true)->orderBy('name')->get(),'tags'=>Tag::orderBy('name')->get()]; } private function data(Request $r): array { $d=$r->validate(['author_id'=>['nullable','exists:authors,id'],'category_id'=>['nullable','exists:categories,id'],'title'=>['required','string','max:255'],'slug'=>['required','string','max:255'],'excerpt'=>['nullable','string'],'content'=>['nullable','string'],'featured_image'=>['nullable','string','max:255'],'status'=>['required','in:draft,review,published,archived'],'is_featured'=>['sometimes','boolean'],'published_at'=>['nullable','date'],'expires_at'=>['nullable','date','after:published_at'],'meta_title'=>['nullable','string','max:255'],'meta_description'=>['nullable','string']]); $d['is_featured']=$r->boolean('is_featured'); return $d; } }
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
use Modules\Authors\Models\Author;
|
||||
use Modules\Categories\Models\Category;
|
||||
use Modules\News\Models\News;
|
||||
use Modules\Tags\Models\Tag;
|
||||
|
||||
final class NewsController extends Controller
|
||||
{
|
||||
public function index(): View
|
||||
{
|
||||
return view('news::admin.index', ['records' => News::with(['author', 'category'])->latest()->paginate(25)]);
|
||||
}
|
||||
|
||||
public function create(): View
|
||||
{
|
||||
return view('news::admin.form', $this->lookups());
|
||||
}
|
||||
|
||||
public function store(Request $r): RedirectResponse
|
||||
{
|
||||
$record = News::create($this->data($r));
|
||||
$record->tags()->sync($r->input('tag_ids', []));
|
||||
|
||||
return redirect()->route('administration.news.index')->with('status', 'News created.');
|
||||
}
|
||||
|
||||
public function edit(News $news_article): View
|
||||
{
|
||||
return view('news::admin.form', array_merge($this->lookups(), ['record' => $news_article]));
|
||||
}
|
||||
|
||||
public function update(Request $r, News $news_article): RedirectResponse
|
||||
{
|
||||
$news_article->update($this->data($r));
|
||||
$news_article->tags()->sync($r->input('tag_ids', []));
|
||||
|
||||
return redirect()->route('administration.news.index')->with('status', 'News updated.');
|
||||
}
|
||||
|
||||
public function destroy(News $news_article): RedirectResponse
|
||||
{
|
||||
$news_article->delete();
|
||||
|
||||
return back()->with('status', 'News deleted.');
|
||||
}
|
||||
|
||||
private function lookups(): array
|
||||
{
|
||||
return ['authors' => Author::where('is_active', true)->orderBy('name')->get(), 'categories' => Category::where('is_active', true)->orderBy('name')->get(), 'tags' => Tag::orderBy('name')->get()];
|
||||
}
|
||||
|
||||
private function data(Request $r): array
|
||||
{
|
||||
$d = $r->validate(['author_id' => ['nullable', 'exists:authors,id'], 'category_id' => ['nullable', 'exists:categories,id'], 'title' => ['required', 'string', 'max:255'], 'slug' => ['required', 'string', 'max:255'], 'excerpt' => ['nullable', 'string'], 'content' => ['nullable', 'string'], 'featured_image' => ['nullable', 'string', 'max:255'], 'status' => ['required', 'in:draft,review,published,archived'], 'is_featured' => ['sometimes', 'boolean'], 'published_at' => ['nullable', 'date'], 'expires_at' => ['nullable', 'date', 'after:published_at'], 'meta_title' => ['nullable', 'string', 'max:255'], 'meta_description' => ['nullable', 'string']]);
|
||||
$d['is_featured'] = $r->boolean('is_featured');
|
||||
|
||||
return $d;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,20 @@
|
||||
<?php
|
||||
namespace Modules\News\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\View\View; use Modules\News\Models\News;
|
||||
final class PublicNewsController extends Controller { public function index(): View { return view('news::public.index',['records'=>News::published()->with(['author','category'])->latest('published_at')->paginate(12)]); } public function show(string $slug): View { return view('news::public.show',['record'=>News::published()->with(['author','category','tags'])->where('slug',$slug)->firstOrFail()]); } }
|
||||
|
||||
namespace Modules\News\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\View\View;
|
||||
use Modules\News\Models\News;
|
||||
|
||||
final class PublicNewsController extends Controller
|
||||
{
|
||||
public function index(): View
|
||||
{
|
||||
return view('news::public.index', ['records' => News::published()->with(['author', 'category'])->latest('published_at')->paginate(12)]);
|
||||
}
|
||||
|
||||
public function show(string $slug): View
|
||||
{
|
||||
return view('news::public.show', ['record' => News::published()->with(['author', 'category', 'tags'])->where('slug', $slug)->firstOrFail()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\News\Models;
|
||||
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Modules\Authors\Models\Author; use Modules\Categories\Models\Category; use Modules\Tags\Models\Tag; use Modules\Workflow\Concerns\HasWorkflow; use Modules\Localization\Concerns\HasTranslations;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Modules\Authors\Models\Author;
|
||||
use Modules\Categories\Models\Category;
|
||||
use Modules\Localization\Concerns\HasTranslations;
|
||||
use Modules\Sites\Concerns\BelongsToSites;
|
||||
final class News extends Model { use BelongsToSites, HasTranslations, HasWorkflow; protected $table='news_articles'; protected $fillable=['author_id','category_id','title','slug','excerpt','content','featured_image','status','is_featured','published_at','expires_at','meta_title','meta_description']; protected function casts(): array { return ['is_featured'=>'boolean','published_at'=>'datetime','expires_at'=>'datetime']; } public function author(): BelongsTo { return $this->belongsTo(Author::class); } public function category(): BelongsTo { return $this->belongsTo(Category::class); } public function tags(): BelongsToMany { return $this->belongsToMany(Tag::class, 'news_article_tag'); } public function scopePublished(Builder $query): Builder { return $query->where('status','published')->where(fn(Builder $q)=>$q->whereNull('published_at')->orWhere('published_at','<=',now()))->where(fn(Builder $q)=>$q->whereNull('expires_at')->orWhere('expires_at','>',now())); } }
|
||||
use Modules\Tags\Models\Tag;
|
||||
use Modules\Workflow\Concerns\HasWorkflow;
|
||||
|
||||
final class News extends Model
|
||||
{
|
||||
use BelongsToSites, HasTranslations, HasWorkflow;
|
||||
|
||||
protected $table = 'news_articles';
|
||||
|
||||
protected $fillable = ['author_id', 'category_id', 'title', 'slug', 'excerpt', 'content', 'featured_image', 'status', 'is_featured', 'published_at', 'expires_at', 'meta_title', 'meta_description'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['is_featured' => 'boolean', 'published_at' => 'datetime', 'expires_at' => 'datetime'];
|
||||
}
|
||||
|
||||
public function author(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Author::class);
|
||||
}
|
||||
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
public function tags(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Tag::class, 'news_article_tag');
|
||||
}
|
||||
|
||||
public function scopePublished(Builder $query): Builder
|
||||
{
|
||||
return $query->where('status', 'published')->where(fn (Builder $q) => $q->whereNull('published_at')->orWhere('published_at', '<=', now()))->where(fn (Builder $q) => $q->whereNull('expires_at')->orWhere('expires_at', '>', now()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\News\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
final class NewsServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
@@ -8,6 +11,7 @@ final class NewsServiceProvider extends ServiceProvider
|
||||
$this->app->register(RouteServiceProvider::class);
|
||||
$this->mergeConfigFrom(module_path('News', 'Config/config.php'), 'news');
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->loadMigrationsFrom(module_path('News', 'Database/Migrations'));
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\News\Providers;
|
||||
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
final class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
<?php
|
||||
use Illuminate\Support\Facades\Route; use Modules\News\Http\Controllers\NewsController; use Modules\News\Http\Controllers\PublicNewsController;
|
||||
Route::middleware(['auth','verified'])->prefix('administration/news')->name('administration.news.')->group(function (): void { Route::resource('/', NewsController::class)->parameters([''=>'news_article'])->except('show'); });
|
||||
Route::get('/news',[PublicNewsController::class,'index'])->name('news.public.index'); Route::get('/news/{slug}',[PublicNewsController::class,'show'])->name('news.public.show');
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Modules\News\Http\Controllers\NewsController;
|
||||
use Modules\News\Http\Controllers\PublicNewsController;
|
||||
|
||||
Route::middleware(['auth', 'verified'])->prefix('administration/news')->name('administration.news.')->group(function (): void {
|
||||
Route::resource('/', NewsController::class)->parameters(['' => 'news_article'])->except('show');
|
||||
});
|
||||
Route::get('/news', [PublicNewsController::class, 'index'])->name('news.public.index');
|
||||
Route::get('/news/{slug}', [PublicNewsController::class, 'show'])->name('news.public.show');
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\News\Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
final class ModuleBootTest extends TestCase
|
||||
{
|
||||
public function test_module_configuration_is_available(): void
|
||||
|
||||
Reference in New Issue
Block a user