Add (article): ai gen, front views

This commit is contained in:
2023-09-24 22:53:40 +08:00
parent 18705bd5e4
commit 322d680961
115 changed files with 9710 additions and 201 deletions

47
app/Models/Author.php Normal file
View File

@@ -0,0 +1,47 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* Class Author
*
* @property int $id
* @property string $name
* @property string $avatar
* @property string $bio
* @property bool $enabled
* @property bool $public
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Collection|Post[] $posts
*/
class Author extends Model
{
protected $table = 'authors';
protected $casts = [
'enabled' => 'bool',
'public' => 'bool',
];
protected $fillable = [
'name',
'avatar',
'bio',
'enabled',
'public',
];
public function posts()
{
return $this->hasMany(Post::class);
}
}

View File

@@ -16,6 +16,7 @@
*
* @property int $id
* @property string $name
* @property string $short_name
* @property string|null $slug
* @property bool $enabled
* @property Carbon|null $created_at
@@ -39,6 +40,7 @@ class Category extends Model
protected $fillable = [
'name',
'short_name',
'slug',
'enabled',
'_lft',
@@ -64,4 +66,16 @@ public function saveQuietly(array $options = [])
return $this->save($options);
});
}
public function posts()
{
return $this->hasManyThrough(
Post::class,
PostCategory::class,
'category_id', // Foreign key on PostCategory table
'id', // Local key on Post table
'id', // Local key on Category table
'post_id' // Foreign key on PostCategory table
);
}
}

View File

@@ -0,0 +1,59 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class NewsSerpResult
*
* @property int $id
* @property int $category_id
* @property string $category_name
* @property string $serp_provider
* @property string $serp_se
* @property string $serp_se_type
* @property string $serp_keyword
* @property string $serp_country_iso
* @property float|null $serp_cost
* @property int|null $result_count
* @property string $filename
* @property string $status
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Category $category
*/
class NewsSerpResult extends Model
{
protected $table = 'news_serp_results';
protected $casts = [
'category_id' => 'int',
'serp_cost' => 'float',
'result_count' => 'int',
];
protected $fillable = [
'category_id',
'category_name',
'serp_provider',
'serp_se',
'serp_se_type',
'serp_keyword',
'serp_country_iso',
'serp_cost',
'result_count',
'filename',
'status',
];
public function category()
{
return $this->belongsTo(Category::class);
}
}

123
app/Models/Post.php Normal file
View File

@@ -0,0 +1,123 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Spatie\Feed\Feedable;
use Spatie\Feed\FeedItem;
/**
* Class Post
*
* @property int $id
* @property string|null $title
* @property string|null $slug
* @property string|null $type
* @property string|null $excerpt
* @property int|null $author_id
* @property bool $featured
* @property string|null $featured_image
* @property string|null $body
* @property int $views_count
* @property string $status
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Author|null $author
* @property Collection|PostCategory[] $post_categories
* @property Carbon $published_at
*/
class Post extends Model implements Feedable
{
protected $table = 'posts';
protected $casts = [
'author_id' => 'int',
'featured' => 'bool',
'views_count' => 'int',
'keywords' => 'array',
'published_at' => 'datetime',
];
protected $fillable = [
'title',
'short_title',
'slug',
'type',
'excerpt',
'author_id',
'featured',
'featured_image',
'body',
'views_count',
'status',
'main_keyword',
'keywords',
'published_at',
];
public function getFeaturedImageLqipCdnAttribute()
{
if (! is_empty($this->featured_image)) {
// Get the extension of the original featured image
$extension = pathinfo($this->featured_image, PATHINFO_EXTENSION);
// Append "_lqip" before the extension to create the LQIP image URL
$lqipFeaturedImage = str_replace(".{$extension}", "_lqip.{$extension}", $this->featured_image);
return 'https://'.Storage::disk('r2')->url($lqipFeaturedImage);
}
return null;
}
public function getFeaturedImageCdnAttribute()
{
if (! is_empty($this->featured_image)) {
return 'https://'.Storage::disk('r2')->url($this->featured_image);
}
return null;
}
public function author()
{
return $this->belongsTo(Author::class);
}
public function category()
{
return $this->hasOneThrough(
Category::class, // The target model
PostCategory::class, // The through model
'post_id', // The foreign key on the through model
'id', // The local key on the parent model (Post)
'id', // The local key on the through model (PostCategory)
'category_id' // The foreign key on the target model (Category)
);
}
public function toFeedItem(): FeedItem
{
return FeedItem::create([
'id' => $this->id,
'title' => $this->title,
'summary' => $this->excerpt,
'updated' => $this->updated_at,
'link' => route('posts.show', $this->slug),
'authorName' => optional($this->author)->name,
]);
}
public static function getFeedItems()
{
return self::where('status', 'published')->latest('published_at')->take(10)->get();
}
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class PostCategory
*
* @property int $id
* @property int $post_id
* @property int $category_id
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Post $post
* @property Category $category
*/
class PostCategory extends Model
{
protected $table = 'post_categories';
protected $casts = [
'post_id' => 'int',
'category_id' => 'int',
];
protected $fillable = [
'post_id',
'category_id',
];
public function post()
{
return $this->belongsTo(Post::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
}

65
app/Models/SerpUrl.php Normal file
View File

@@ -0,0 +1,65 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class SerpUrl
*
* @property int $id
* @property int $news_serp_result_id
* @property int $category_id
* @property string $category_name
* @property string $source
* @property string $url
* @property string $country_iso
* @property string|null $title
* @property string|null $description
* @property Carbon|null $serp_at
* @property string $status
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property NewsSerpResult $news_serp_result
* @property Category $category
*/
class SerpUrl extends Model
{
protected $table = 'serp_urls';
protected $casts = [
'news_serp_result_id' => 'int',
'category_id' => 'int',
'process_status' => 'int',
'serp_at' => 'datetime',
];
protected $fillable = [
'news_serp_result_id',
'category_id',
'category_name',
'source',
'url',
'country_iso',
'title',
'description',
'process_status',
'serp_at',
'status',
];
public function news_serp_result()
{
return $this->belongsTo(NewsSerpResult::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
}