Implement BlogArticle API
This commit is contained in:
parent
cd84dc03f6
commit
5cfa98653e
@ -2,11 +2,20 @@
|
||||
|
||||
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.
|
||||
*
|
||||
@ -14,7 +23,15 @@ class BlogArticleController extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return response()->json([]);
|
||||
$query = BlogArticle::orderBy('date', 'desc');
|
||||
|
||||
if (!$this->authService->isAuthenticated()) {
|
||||
$query = $query->where('published', true);
|
||||
}
|
||||
|
||||
$data = $query->paginate(25);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -24,7 +41,13 @@ class BlogArticleController extends Controller
|
||||
*/
|
||||
public function recent()
|
||||
{
|
||||
$data = BlogArticle::orderBy('date', 'desc')->limit(5)->get();
|
||||
$query = BlogArticle::orderBy('date', 'desc');
|
||||
|
||||
if (!$this->authService->isAuthenticated()) {
|
||||
$query = $query->where('published', true);
|
||||
}
|
||||
|
||||
$data = $query->limit(5)->get();
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
@ -36,18 +59,26 @@ class BlogArticleController extends Controller
|
||||
*/
|
||||
public function popular()
|
||||
{
|
||||
return response()->json([]);
|
||||
$query = BlogArticle::orderBy('views', 'desc')->orderBY('date', 'desc');
|
||||
|
||||
if (!$this->authService->isAuthenticated()) {
|
||||
$query = $query->where('published', true);
|
||||
}
|
||||
|
||||
$data = $query->limit(5)->get();
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \App\Http\Requests\BlogArticleRequest $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function store(Request $request)
|
||||
public function store(BlogArticleRequest $request)
|
||||
{
|
||||
return response()->json([]);
|
||||
$article = BlogArticle::create($request->all());
|
||||
return response()->json($article, 201);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,29 +89,42 @@ class BlogArticleController extends Controller
|
||||
*/
|
||||
public function show(BlogArticle $blogArticle)
|
||||
{
|
||||
return response()->json([]);
|
||||
if (!$this->authService->isAuthenticated() && !$blogArticle->published) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$blogArticle->views += 1;
|
||||
$blogArticle->save();
|
||||
|
||||
return response()->json($blogArticle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \App\Http\Requests\BlogArticleRequest $request
|
||||
* @param \App\Models\BlogArticle $blogArticle
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, BlogArticle $blogArticle)
|
||||
public function update(BlogArticleRequest $request, BlogArticle $blogArticle)
|
||||
{
|
||||
return response()->json([]);
|
||||
$blogArticle->update($request->all());
|
||||
return response(null, 204);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param \App\Models\BlogArticle $blogArticle
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy(BlogArticle $blogArticle)
|
||||
{
|
||||
return response()->json([]);
|
||||
if (!$this->authService->isAuthenticated()) {
|
||||
abort(401);
|
||||
}
|
||||
|
||||
$blogArticle->delete();
|
||||
return response(null, 204);
|
||||
}
|
||||
}
|
||||
|
||||
77
app/Http/Requests/BlogArticleRequest.php
Normal file
77
app/Http/Requests/BlogArticleRequest.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Http\Services\AuthService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class BlogArticleRequest extends FormRequest
|
||||
{
|
||||
private $authService;
|
||||
|
||||
public function __construct(AuthService $authService, array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
|
||||
{
|
||||
parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
|
||||
|
||||
$this->authService = $authService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return $this->authService->isAuthenticated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run before validating data
|
||||
*/
|
||||
protected function prepareForValidation()
|
||||
{
|
||||
if (!$this->exists('slug')) {
|
||||
$this->merge([
|
||||
'slug' => Str::slug($this->get('title')),
|
||||
]);
|
||||
}
|
||||
|
||||
if (!$this->exists('date')) {
|
||||
$this->merge([
|
||||
'date' => Carbon::now()->toDateTime(),
|
||||
]);
|
||||
}
|
||||
|
||||
if (!$this->exists('summary')) {
|
||||
$content = $this->get('content');
|
||||
$pos = -1;
|
||||
if (strlen($content) > 200) {
|
||||
$pos = strpos($content, ' ', 200);
|
||||
}
|
||||
$summary = substr($content, 0, $pos);
|
||||
|
||||
$this->merge([
|
||||
'summary' => $summary,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'title' => 'required|string|max:255',
|
||||
'slug' => 'required|string|max:255',
|
||||
'date' => 'required|date',
|
||||
'summary' => 'required|string|max:1023',
|
||||
'content' => 'required|string',
|
||||
];
|
||||
}
|
||||
}
|
||||
26
app/Http/Services/AuthService.php
Normal file
26
app/Http/Services/AuthService.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Services;
|
||||
|
||||
class AuthService
|
||||
{
|
||||
/**
|
||||
* Check whether the user is authenticated
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isAuthenticated()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function authenticate()
|
||||
{
|
||||
session()->put('auth', true);
|
||||
}
|
||||
|
||||
public function logout()
|
||||
{
|
||||
session()->remove('auth');
|
||||
}
|
||||
}
|
||||
@ -14,12 +14,17 @@ class BlogArticle extends Model
|
||||
'slug',
|
||||
'date',
|
||||
'summary',
|
||||
'content'
|
||||
'content',
|
||||
'views',
|
||||
'published'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
'date' => 'datetime',
|
||||
'views' => 'integer',
|
||||
'published' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
@ -27,6 +27,8 @@ class BlogArticleFactory extends Factory
|
||||
'date' => $this->faker->dateTimeBetween('100d'),
|
||||
'summary' => $this->faker->text(200),
|
||||
'content' => $this->faker->text(1000),
|
||||
'views' => $this->faker->numberBetween(0, 50),
|
||||
'published' => $this->faker->boolean(80),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,11 +17,13 @@ class CreateBlogArticlesTable extends Migration
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
|
||||
$table->string('title');
|
||||
$table->string('slug');
|
||||
$table->string('title', 255);
|
||||
$table->string('slug', 255);
|
||||
$table->dateTime('date');
|
||||
$table->string('summary');
|
||||
$table->string('summary', 1023);
|
||||
$table->text('content');
|
||||
$table->integer('views')->default(0);
|
||||
$table->boolean('published');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -23,14 +23,3 @@ Route::get('/blog/recent', [\App\Http\Controllers\BlogArticleController::class,
|
||||
Route::get('/blog/popular', [\App\Http\Controllers\BlogArticleController::class, 'popular']);
|
||||
Route::resource('/blog', \App\Http\Controllers\BlogArticleController::class)->except(['create', 'edit']);
|
||||
|
||||
Route::get('/blog/popular', function() {
|
||||
return [
|
||||
['title' => 'Lorem, ipsum dolor sit amet consectetur adipisicing.', 'id' => 1],
|
||||
['title' => 'Lorem, ipsum dolor sit amet consectetur adipisicing.', 'id' => 2],
|
||||
['title' => 'Lorem, ipsum dolor sit amet consectetur adipisicing.', 'id' => 3],
|
||||
['title' => 'Lorem, ipsum dolor sit.', 'id' => 4],
|
||||
['title' => 'Lorem, ipsum dolor sit amet consectetur adipisicing.', 'id' => 5],
|
||||
['title' => 'Lorem, ipsum dolor sit amet consectetur adipisicing.', 'id' => 6],
|
||||
['title' => 'Lorem, ipsum dolor sit amet consectetur adipisicing.', 'id' => 7],
|
||||
];
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user