repository = $semester; $this->repository ->pushCriteria(app('Litepie\Repository\Criteria\RequestCriteria')) ->pushCriteria(new \Asadi\Semester\Repositories\Criteria\SemesterUserCriteria()); parent::__construct(); } /** * Display a list of semester. * * @return json */ public function index(SemesterRequest $request) { $semesters = $this->repository ->setPresenter('\\Asadi\\Semester\\Repositories\\Presenter\\SemesterListPresenter') ->scopeQuery(function($query){ return $query->orderBy('id','DESC'); })->all(); $semesters['code'] = 2000; return response()->json($semesters) ->setStatusCode(200, 'INDEX_SUCCESS'); } /** * Display semester. * * @param Request $request * @param Model Semester * * @return Json */ public function show(SemesterRequest $request, Semester $semester) { if ($semester->exists) { $semester = $semester->presenter(); $semester['code'] = 2001; return response()->json($semester) ->setStatusCode(200, 'SHOW_SUCCESS');; } else { return response()->json([]) ->setStatusCode(400, 'SHOW_ERROR'); } } /** * Show the form for creating a new semester. * * @param Request $request * * @return json */ public function create(SemesterRequest $request, Semester $semester) { $semester = $semester->presenter(); $semester['code'] = 2002; return response()->json($semester) ->setStatusCode(200, 'CREATE_SUCCESS'); } /** * Create new semester. * * @param Request $request * * @return json */ public function store(SemesterRequest $request) { try { $attributes = $request->all(); $attributes['user_id'] = user_id('admin.api'); $semester = $this->repository->create($attributes); $semester = $semester->presenter(); $semester['code'] = 2004; return response()->json($semester) ->setStatusCode(201, 'STORE_SUCCESS'); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 4004, ])->setStatusCode(400, 'STORE_ERROR'); } } /** * Show semester for editing. * * @param Request $request * @param Model $semester * * @return json */ public function edit(SemesterRequest $request, Semester $semester) { if ($semester->exists) { $semester = $semester->presenter(); $semester['code'] = 2003; return response()-> json($semester) ->setStatusCode(200, 'EDIT_SUCCESS');; } else { return response()->json([]) ->setStatusCode(400, 'SHOW_ERROR'); } } /** * Update the semester. * * @param Request $request * @param Model $semester * * @return json */ public function update(SemesterRequest $request, Semester $semester) { try { $attributes = $request->all(); $semester->update($attributes); $semester = $semester->presenter(); $semester['code'] = 2005; return response()->json($semester) ->setStatusCode(201, 'UPDATE_SUCCESS'); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 4005, ])->setStatusCode(400, 'UPDATE_ERROR'); } } /** * Remove the semester. * * @param Request $request * @param Model $semester * * @return json */ public function destroy(SemesterRequest $request, Semester $semester) { try { $t = $semester->delete(); return response()->json([ 'message' => trans('messages.success.delete', ['Module' => trans('semester::semester.name')]), 'code' => 2006 ])->setStatusCode(202, 'DESTROY_SUCCESS'); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 4006, ])->setStatusCode(400, 'DESTROY_ERROR'); } } }