Skip to content

Instantly share code, notes, and snippets.

@raank
Created April 3, 2019 19:25
Show Gist options
  • Save raank/5dc4340970c52e201a1127ebdcbcb0c1 to your computer and use it in GitHub Desktop.
Save raank/5dc4340970c52e201a1127ebdcbcb0c1 to your computer and use it in GitHub Desktop.
<?php
namespace Modules\Users\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Users\Entities\User;
use Modules\Users\Http\Requests\UsersRequest;
use Ramsey\Uuid\Uuid;
class UsersController extends Controller
{
/**
* Display a listing of the resource.
* @return Response
*/
public function index()
{
return response()
->json(User::all(), 200);
}
/**
* Store a newly created resource in storage.
*
* @param UsersRequest $request
*
* @return Response
*/
public function store(UsersRequest $request)
{
try {
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
if ($user->save()) {
return response()
->json([
'message' => __('request.success.store')
], 200);
} else {
return response()
->json([
'message' => __('request.error.store')
], 402);
}
} catch (\Exception $exception) {
return response()
->json([
'_message' => $exception->getMessage(),
'_file' => $exception->getFile(),
'_line' => $exception->getLine(),
'_code' => $exception->getCode(),
'message' => __('request.fail.try')
], 402);
}
}
/**
* Show the specified resource.
*
* @param Request $request
*
* @return Response
*/
public function show(Request $request)
{
return response()
->json(User::reference($request->reference), 200);
}
/**
* Update the specified resource in storage.
*
* @param UsersRequest $request
*
* @return Response
*/
public function update(UsersRequest $request)
{
try {
$user = User::reference($request->reference);
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
if ($user->save()) {
return response()
->json([
'message' => __('request.success.update')
], 200);
} else {
return response()
->json([
'message' => __('request.error.update')
], 402);
}
} catch (\Exception $exception) {
return response()
->json([
'_message' => $exception->getMessage(),
'_file' => $exception->getFile(),
'_line' => $exception->getLine(),
'_code' => $exception->getCode(),
'message' => __('request.fail.try')
], 402);
}
}
/**
* Remove the specified resource from storage.
*
* @param Request $request
*
* @return Response
*/
public function destroy(Request $request)
{
try {
$user = User::reference($request->reference);
if ($user->delete()) {
return response()
->json([
'message' => __('request.success.destroy')
], 200);
} else {
return response()
->json([
'message' => __('request.error.destroy')
], 402);
}
} catch (\Exception $exception) {
return response()
->json([
'_message' => $exception->getMessage(),
'_file' => $exception->getFile(),
'_line' => $exception->getLine(),
'_code' => $exception->getCode(),
'message' => __('request.fail.try')
], 402);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment