132 lines
3.1 KiB
PHP
132 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\BlogArticleRequest;
|
|
use App\Http\Services\AuthService;
|
|
use App\Models\BlogArticle;
|
|
use Illuminate\Http\Request;
|
|
|
|
class BlogArticleController extends Controller
|
|
{
|
|
private $authService;
|
|
|
|
public function __construct(AuthService $authService)
|
|
{
|
|
$this->authService = $authService;
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function index()
|
|
{
|
|
$query = BlogArticle::orderBy('date', 'desc');
|
|
|
|
if (!$this->authService->isAuthenticated()) {
|
|
$query = $query->where('published', true);
|
|
}
|
|
|
|
$data = $query->paginate(25);
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
/**
|
|
* Display a list of recent articles.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function recent()
|
|
{
|
|
$query = BlogArticle::orderBy('date', 'desc');
|
|
|
|
if (!$this->authService->isAuthenticated()) {
|
|
$query = $query->where('published', true);
|
|
}
|
|
|
|
$data = $query->limit(5)->get();
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
/**
|
|
* Display a list of popular articles.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function popular()
|
|
{
|
|
$data = BlogArticle::select()
|
|
->orderBy('views', 'desc')
|
|
->orderBy('date', 'desc')
|
|
->where('published', true)
|
|
->limit(5)
|
|
->get()
|
|
;
|
|
|
|
return response()->json($data);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\BlogArticleRequest $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function store(BlogArticleRequest $request)
|
|
{
|
|
$article = BlogArticle::create($request->all());
|
|
return response()->json($article, 201);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param \App\Models\BlogArticle $blogArticle
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function show(BlogArticle $blogArticle)
|
|
{
|
|
if (!$this->authService->isAuthenticated() && !$blogArticle->published) {
|
|
abort(404);
|
|
}
|
|
|
|
$blogArticle->views += 1;
|
|
$blogArticle->save();
|
|
|
|
return response()->json($blogArticle);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\BlogArticleRequest $request
|
|
* @param \App\Models\BlogArticle $blogArticle
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(BlogArticleRequest $request, BlogArticle $blogArticle)
|
|
{
|
|
$blogArticle->update($request->all());
|
|
return response(null, 204);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\BlogArticle $blogArticle
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(BlogArticle $blogArticle)
|
|
{
|
|
if (!$this->authService->isAuthenticated()) {
|
|
abort(401);
|
|
}
|
|
|
|
$blogArticle->delete();
|
|
return response(null, 204);
|
|
}
|
|
}
|