mirror of
https://github.com/ZorgCC/lumen-generators.git
synced 2024-12-26 14:05:29 +03:00
66 lines
1.7 KiB
Plaintext
66 lines
1.7 KiB
Plaintext
<?php namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
|
|
trait RESTActions {
|
|
|
|
|
|
public function all()
|
|
{
|
|
$m = self::MODEL;
|
|
return $this->respond(Response::HTTP_OK, $m::all());
|
|
}
|
|
|
|
public function get($id)
|
|
{
|
|
$m = self::MODEL;
|
|
$model = $m::find($id);
|
|
if(is_null($model)){
|
|
return $this->respond(Response::HTTP_NOT_FOUND);
|
|
}
|
|
return $this->respond(Response::HTTP_OK, $model);
|
|
}
|
|
|
|
public function add(Request $request)
|
|
{
|
|
$m = self::MODEL;
|
|
$this->validate($request, $m::$rules);
|
|
return $this->respond(Response::HTTP_CREATED, $m::create($request->all()));
|
|
}
|
|
|
|
public function put(Request $request, $id)
|
|
{
|
|
$m = self::MODEL;
|
|
$this->validate($request, $m::$rules);
|
|
$model = $m::find($id);
|
|
if(is_null($model)){
|
|
return $this->respond(Response::HTTP_NOT_FOUND);
|
|
}
|
|
$model->update($request->all());
|
|
return $this->respond(Response::HTTP_OK, $model);
|
|
}
|
|
|
|
public function remove($id)
|
|
{
|
|
$m = self::MODEL;
|
|
if(is_null($m::find($id))){
|
|
return $this->respond(Response::HTTP_NOT_FOUND);
|
|
}
|
|
$m::destroy($id);
|
|
return $this->respond(Response::HTTP_NO_CONTENT);
|
|
}
|
|
|
|
protected function respond($status, $data = [])
|
|
{
|
|
if($status == Response::HTTP_NO_CONTENT){
|
|
return response(null,Response::HTTP_NO_CONTENT);
|
|
}
|
|
if($status == Response::HTTP_NOT_FOUND){
|
|
return response(['message'=>'resource not found'],Response::HTTP_NOT_FOUND);
|
|
}
|
|
return response()->json($data, $status);
|
|
}
|
|
|
|
}
|