Last active
May 14, 2020 22:36
-
-
Save NandoKstroNet/d15400cf5dc7a3b91ee427f7a32b7f3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Api; | |
use App\Api\ApiMessages; | |
use App\Category; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
class CategoryController extends Controller | |
{ | |
/** | |
* @var Category | |
*/ | |
private $category; | |
public function __construct(Category $category) | |
{ | |
$this->category = $category; | |
} | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index() | |
{ | |
$category = $this->category->paginate('10'); | |
return response()->json($category, 200); | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request) | |
{ | |
$data = $request->all(); | |
try{ | |
$category = $this->category->create($data); | |
return response()->json([ | |
'data' => [ | |
'msg' => 'Categoria cadastrada com sucesso!' | |
] | |
], 200); | |
} catch (\Exception $e) { | |
$message = new ApiMessages($e->getMessage()); | |
return response()->json($message->getMessage(), 401); | |
} | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function show($id) | |
{ | |
try{ | |
$category = $this->category->findOrFail($id); | |
return response()->json([ | |
'data' => $category | |
], 200); | |
} catch (\Exception $e) { | |
$message = new ApiMessages($e->getMessage()); | |
return response()->json($message->getMessage(), 401); | |
} | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function update(Request $request, $id) | |
{ | |
$data = $request->all(); | |
try{ | |
$category = $this->category->findOrFail($id); | |
$category->update($data); | |
return response()->json([ | |
'data' => [ | |
'msg' => 'Categoria atualizada com sucesso!' | |
] | |
], 200); | |
} catch (\Exception $e) { | |
$message = new ApiMessages($e->getMessage()); | |
return response()->json($message->getMessage(), 401); | |
} | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function destroy($id) | |
{ | |
try{ | |
$category = $this->category->findOrFail($id); | |
$category->delete(); | |
return response()->json([ | |
'data' => [ | |
'msg' => 'Categoria removida com sucesso!' | |
] | |
], 200); | |
} catch (\Exception $e) { | |
$message = new ApiMessages($e->getMessage()); | |
return response()->json($message->getMessage(), 401); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
na linha 44 você esqueceu de tirar a verificação de senha do usuario ... deve ter ocorrido quando você copiou e colou do UserController