Additional API methods for departments

This commit is contained in:
snipe 2017-05-23 01:08:55 -07:00
parent 659e60fd12
commit 6c623866c0

View file

@ -6,6 +6,8 @@ use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Department;
use App\Http\Transformers\DepartmentsTransformer;
use App\Helpers\Helper;
use Auth;
class DepartmentsController extends Controller
{
@ -25,7 +27,7 @@ class DepartmentsController extends Controller
'id',
'name',
'location_id'
])->with('users')->withCount('assets')->withCount('users');
])->with('users')->withCount('users');
if ($request->has('search')) {
$departments = $departments->TextSearch($request->input('search'));
@ -42,4 +44,41 @@ class DepartmentsController extends Controller
return (new DepartmentsTransformer)->transformDepartments($departments, $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', Department::class);
$department = new Department;
$department->fill($request->all());
$department->user_id = Auth::user()->id;
if ($department->save()) {
return response()->json(Helper::formatStandardApiResponse('success', $department, trans('admin/departments/message.create.success')));
}
return response()->json(Helper::formatStandardApiResponse('error', null, $department->getErrors()));
}
/**
* 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', Department::class);
$department = Department::findOrFail($id);
return (new DepartmentsTransformer)->transformDepartment($department);
}
}