Commit c5d4d729 authored by cygnet's avatar cygnet

changes done for the api creation of the blog

parent cc28e461
<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Resources\BlogsResource;
use App\Models\Blogs\Blog;
use App\Repositories\Backend\Blogs\BlogsRepository;
use Illuminate\Http\Request;
use Validator;
class BlogsController extends APIController
{
protected $repository;
/**
* __construct.
*
* @param $repository
*/
public function __construct(BlogsRepository $repository)
{
$this->repository = $repository;
}
/**
* Return the users.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$limit = $request->get('paginate') ? $request->get('paginate') : 25;
return BlogsResource::collection(
$this->repository->getForDataTable()->paginate($limit)
);
}
/**
* Return the specified resource.
*
* @param User $user
*
* @return \Illuminate\Http\Response
*/
public function show(Blog $blog)
{
return new BlogsResource($blog);
}
/**
* Creates the Resource for Blog.
*
* @param Request $request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validation = $this->validatingRequest($request);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
$this->repository->create($request->all());
return new BlogsResource(Blog::orderBy('created_at', 'desc')->first());
}
/**
* @param Blog $blog
* @param UpdateBlogRequest $request
*
* @return mixed
*/
public function update(Request $request, Blog $blog)
{
$validation = $this->validatingRequest($request, 'update');
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
$this->repository->update($blog, $request->all());
$blog = Blog::findOrfail($blog->id);
return new BlogsResource($blog);
}
public function validatingRequest(Request $request, $type="insert")
{
$featured_image = ($type=="insert")?"required":"";
$validation = Validator::make($request->all(), [
'name' => 'required|max:191',
'featured_image' => $featured_image,
'content' => 'required',
'categories' => 'required',
'tags' => 'required',
]);
return $validation;
}
public function messages()
{
return [
'name.required' => 'Please insert Blog Title',
'name.max' => 'Blog Title may not be greater than 191 characters.',
];
}
/**
* @param Blog $blog
* @param DeleteBlogRequest $request
*
* @return mixed
*/
public function destroy(Blog $blog, Request $request)
{
$this->repository->delete($blog);
return ['message'=>'success'];
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class BlogsResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
*
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'publish_datetime' => $this->publish_datetime->format('d/m/Y h:i A'),
'status' => $this->status,
'created_at' => optional($this->created_at)->toDateString(),
'created_by' => (is_null($this->user_name)) ? optional($this->owner)->first_name : $this->user_name,
];
}
}
...@@ -51,5 +51,8 @@ Route::group(['namespace' => 'Api\V1', 'prefix' => 'v1', 'as' => 'v1.'], functio ...@@ -51,5 +51,8 @@ Route::group(['namespace' => 'Api\V1', 'prefix' => 'v1', 'as' => 'v1.'], functio
// Blog Tags // Blog Tags
Route::resource('blog_tags', 'BlogTagsController'); Route::resource('blog_tags', 'BlogTagsController');
// Blogs
Route::resource('blogs', 'BlogsController');
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment