repository = $todo; $this->repository ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class) ->pushCriteria(\Litepci\Todo\Repositories\Criteria\TodoResourceCriteria::class); } /** * Display a list of todo. * * @return Response */ public function index(TodoRequest $request) { $view = $this->response->theme->listView(); if ($this->response->typeIs('json')) { $function = camel_case('get-' . $view); return $this->repository ->setPresenter(\Litepci\Todo\Repositories\Presenter\TodoPresenter::class) ->$function(); } $todos = $this->repository->paginate(); return $this->response->setMetaTitle(trans('todo::todo.names')) ->view('todo::todo.index', true) ->data(compact('todos', 'view')) ->output(); } /** * Display todo. * * @param Request $request * @param Model $todo * * @return Response */ public function show(TodoRequest $request, Todo $todo) { if ($todo->exists) { $view = 'todo::todo.show'; } else { $view = 'todo::todo.new'; } return $this->response->setMetaTitle(trans('app.view') . ' ' . trans('todo::todo.name')) ->data(compact('todo')) ->view($view, true) ->output(); } /** * Show the form for creating a new todo. * * @param Request $request * * @return Response */ public function create(TodoRequest $request) { $todo = $this->repository->newInstance([]); return $this->response->setMetaTitle(trans('app.new') . ' ' . trans('todo::todo.name')) ->view('todo::todo.create', true) ->data(compact('todo')) ->output(); } /** * Create new todo. * * @param Request $request * * @return Response */ public function store(TodoRequest $request) { try { $attributes = $request->all(); $attributes['user_id'] = user_id(); $attributes['user_type'] = user_type(); $todo = $this->repository->create($attributes); return $this->response->message(trans('messages.success.created', ['Module' => trans('todo::todo.name')])) ->code(204) ->status('success') ->url(guard_url('todo/todo/' . $todo->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('/todo/todo')) ->redirect(); } } /** * Show todo for editing. * * @param Request $request * @param Model $todo * * @return Response */ public function edit(TodoRequest $request, Todo $todo) { return $this->response->setMetaTitle(trans('app.edit') . ' ' . trans('todo::todo.name')) ->view('todo::todo.edit', true) ->data(compact('todo')) ->output(); } /** * Update the todo. * * @param Request $request * @param Model $todo * * @return Response */ public function update(TodoRequest $request, Todo $todo) { try { $attributes = $request->all(); $todo->update($attributes); return $this->response->message(trans('messages.success.updated', ['Module' => trans('todo::todo.name')])) ->code(204) ->status('success') ->url(guard_url('todo/todo/' . $todo->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('todo/todo/' . $todo->getRouteKey())) ->redirect(); } } /** * Remove the todo. * * @param Model $todo * * @return Response */ public function destroy(TodoRequest $request, Todo $todo) { try { $todo->delete(); return $this->response->message(trans('messages.success.deleted', ['Module' => trans('todo::todo.name')])) ->code(202) ->status('success') ->url(guard_url('todo/todo/0')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('todo/todo/' . $todo->getRouteKey())) ->redirect(); } } /** * Remove multiple todo. * * @param Model $todo * * @return Response */ public function delete(TodoRequest $request, $type) { try { $ids = hashids_decode($request->input('ids')); if ($type == 'purge') { $this->repository->purge($ids); } else { $this->repository->delete($ids); } return $this->response->message(trans('messages.success.deleted', ['Module' => trans('todo::todo.name')])) ->status("success") ->code(202) ->url(guard_url('todo/todo')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/todo/todo')) ->redirect(); } } /** * Restore deleted todos. * * @param Model $todo * * @return Response */ public function restore(TodoRequest $request) { try { $ids = hashids_decode($request->input('ids')); $this->repository->restore($ids); return $this->response->message(trans('messages.success.restore', ['Module' => trans('todo::todo.name')])) ->status("success") ->code(202) ->url(guard_url('/todo/todo')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/todo/todo/')) ->redirect(); } } }