2017-02-01 18:50:28 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Helpers\Helper;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2017-02-01 18:50:28 -08:00
|
|
|
use App\Http\Transformers\GroupsTransformer;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Models\Group;
|
|
|
|
use Illuminate\Http\Request;
|
2017-02-01 18:50:28 -08:00
|
|
|
|
|
|
|
class GroupsController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorize('view', Group::class);
|
2021-06-10 13:15:52 -07:00
|
|
|
$allowed_columns = ['id', 'name', 'created_at', 'users_count'];
|
2017-02-01 18:50:28 -08:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$groups = Group::select('id', 'name', 'permissions', 'created_at', 'updated_at')->withCount('users as users_count');
|
2017-02-01 18:50:28 -08:00
|
|
|
|
2019-05-23 17:39:50 -07:00
|
|
|
if ($request->filled('search')) {
|
2017-02-01 18:50:28 -08:00
|
|
|
$groups = $groups->TextSearch($request->input('search'));
|
|
|
|
}
|
|
|
|
|
2020-02-04 12:32:24 -08:00
|
|
|
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which
|
|
|
|
// case we override with the actual count, so we should return 0 items.
|
|
|
|
$offset = (($groups) && ($request->get('offset') > $groups->count())) ? $groups->count() : $request->get('offset', 0);
|
2019-09-03 14:02:08 -07:00
|
|
|
|
|
|
|
// Check to make sure the limit is not higher than the max allowed
|
2019-09-03 20:28:49 -07:00
|
|
|
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
|
2019-09-03 14:02:08 -07:00
|
|
|
|
2017-02-01 18:50:28 -08:00
|
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
|
|
|
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
|
|
|
|
$groups->orderBy($sort, $order);
|
|
|
|
|
|
|
|
$total = $groups->count();
|
|
|
|
$groups = $groups->skip($offset)->take($limit)->get();
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-02-01 18:50:28 -08:00
|
|
|
return (new GroupsTransformer)->transformGroups($groups, $total);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorize('create', Group::class);
|
|
|
|
$group = new Group;
|
|
|
|
$group->fill($request->all());
|
|
|
|
|
|
|
|
if ($group->save()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $group, trans('admin/groups/message.create.success')));
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $group->getErrors()));
|
2017-02-01 18:50:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$this->authorize('view', Group::class);
|
|
|
|
$group = Group::findOrFail($id);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-09-27 15:18:29 -07:00
|
|
|
return (new GroupsTransformer)->transformGroup($group);
|
2017-02-01 18:50:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
2018-07-12 18:28:02 -07:00
|
|
|
$this->authorize('update', Group::class);
|
2017-02-01 18:50:28 -08:00
|
|
|
$group = Group::findOrFail($id);
|
|
|
|
$group->fill($request->all());
|
|
|
|
|
|
|
|
if ($group->save()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $group, trans('admin/groups/message.update.success')));
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $group->getErrors()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
$this->authorize('delete', Group::class);
|
|
|
|
$group = Group::findOrFail($id);
|
|
|
|
$this->authorize('delete', $group);
|
|
|
|
$group->delete();
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/groups/message.delete.success')));
|
2017-02-01 18:50:28 -08:00
|
|
|
}
|
|
|
|
}
|