Implement new view count system on blog article
This commit is contained in:
parent
13eb273772
commit
5c260b98f6
@ -89,14 +89,23 @@ class BlogArticleController extends Controller
|
|||||||
* @param \App\Models\BlogArticle $blogArticle
|
* @param \App\Models\BlogArticle $blogArticle
|
||||||
* @return \Illuminate\Http\JsonResponse
|
* @return \Illuminate\Http\JsonResponse
|
||||||
*/
|
*/
|
||||||
public function show(BlogArticle $blogArticle)
|
public function show(int $id)
|
||||||
{
|
{
|
||||||
if (!$this->authService->isAuthenticated() && !$blogArticle->published) {
|
$blogArticle = BlogArticle::withCount('views')->find($id);
|
||||||
|
|
||||||
|
if ($blogArticle === null) {
|
||||||
abort(404);
|
abort(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$blogArticle->views += 1;
|
if (!$this->authService->isAuthenticated()) {
|
||||||
$blogArticle->save();
|
if (!$blogArticle->published) {
|
||||||
|
abort(404);
|
||||||
|
}
|
||||||
|
|
||||||
|
views($blogArticle)
|
||||||
|
->cooldown(now()->addHour())
|
||||||
|
->record();
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json($blogArticle);
|
return response()->json($blogArticle);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,12 +2,15 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use CyrildeWit\EloquentViewable\Contracts\Viewable;
|
||||||
|
use CyrildeWit\EloquentViewable\InteractsWithViews;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
class BlogArticle extends Model
|
class BlogArticle extends Model implements Viewable
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
use InteractsWithViews;
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'title',
|
'title',
|
||||||
@ -15,7 +18,6 @@ class BlogArticle extends Model
|
|||||||
'date',
|
'date',
|
||||||
'summary',
|
'summary',
|
||||||
'content',
|
'content',
|
||||||
'views',
|
|
||||||
'published'
|
'published'
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -24,7 +26,8 @@ class BlogArticle extends Model
|
|||||||
'created_at' => 'datetime',
|
'created_at' => 'datetime',
|
||||||
'updated_at' => 'datetime',
|
'updated_at' => 'datetime',
|
||||||
'date' => 'datetime',
|
'date' => 'datetime',
|
||||||
'views' => 'integer',
|
|
||||||
'published' => 'boolean',
|
'published' => 'boolean',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
protected $removeViewsOnDelete = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user