Advertisement
Guest User

categoryController

a guest
Mar 30th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers\Admin;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\Contracts\CategoryContract;
  7. use App\Http\Controllers\BaseController;
  8.  
  9. /**
  10. * Class CategoryController
  11. * @package App\Http\Controllers\Admin
  12. */
  13. class CategoryController extends BaseController
  14. {
  15. /**
  16. * @var CategoryContract
  17. */
  18. protected $categoryRepository;
  19.  
  20. /**
  21. * CategoryController constructor.
  22. * @param CategoryContract $categoryRepository
  23. */
  24. public function __construct(CategoryContract $categoryRepository)
  25. {
  26. $this->categoryRepository = $categoryRepository;
  27. }
  28.  
  29. /**
  30. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  31. */
  32. public function index()
  33. {
  34. $categories = $this->categoryRepository->listCategories();
  35.  
  36. $this->setPageTitle('categories', 'List of all categories');
  37. return view('admin.categories.index', compact('categories'));
  38. }
  39.  
  40. /**
  41. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  42. */
  43. public function create()
  44. {
  45. $categories = $this->categoryRepository->treeList();
  46.  
  47. $this->setPageTitle('Categories', 'Create Category');
  48. return view('admin.categories.create', compact('categories'));
  49. }
  50.  
  51. /**
  52. * @param Request $request
  53. * @return \Illuminate\Http\RedirectResponse
  54. * @throws \Illuminate\Validation\ValidationException
  55. */
  56. public function store(Request $request)
  57. {
  58. $this->validate($request, [
  59. 'name' => 'required|max:191',
  60. 'parent_id' => 'required|not_in:0',
  61. 'image' => 'mimes:jpg,jpeg,png|max:1000'
  62. ]);
  63.  
  64. $params = $request->except('_token');
  65.  
  66. $category = $this->categoryRepository->createCategory($params);
  67.  
  68. if (!$category) {
  69. return $this->responseRedirectBack('Error occurred while creating category.', 'error', true, true);
  70. }
  71. return $this->responseRedirect('admin.categories.index', 'Category added successfully' ,'success',false, false);
  72. }
  73.  
  74. /**
  75. * @param $id
  76. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  77. */
  78. public function edit($id)
  79. {
  80. $targetCategory = $this->categoryRepository->findCategoryById($id);
  81. $categories = $this->categoryRepository->treeList();
  82.  
  83. $this->setPageTitle('Categories', 'Edit Category : '.$targetCategory->name);
  84. return view('admin.categories.edit', compact('categories', 'targetCategory'));
  85. }
  86.  
  87. /**
  88. * @param Request $request
  89. * @return \Illuminate\Http\RedirectResponse
  90. * @throws \Illuminate\Validation\ValidationException
  91. */
  92. public function update(Request $request)
  93. {
  94. $this->validate($request, [
  95. 'name' => 'required|max:191',
  96. 'parent_id' => 'required|not_in:0',
  97. 'image' => 'mimes:jpg,jpeg,png|max:1000'
  98. ]);
  99.  
  100. $params = $request->except('_token');
  101.  
  102. $category = $this->categoryRepository->updateCategory($params);
  103.  
  104. if (!$category) {
  105. return $this->responseRedirectBack('Error occurred while updating category.', 'error', true, true);
  106. }
  107. return $this->responseRedirectBack('Category updated successfully' ,'success',false, false);
  108. }
  109.  
  110. /**
  111. * @param $id
  112. * @return \Illuminate\Http\RedirectResponse
  113. */
  114. public function delete($id)
  115. {
  116. $category = $this->categoryRepository->deleteCategory($id);
  117.  
  118. if (!$category) {
  119. return $this->responseRedirectBack('Error occurred while deleting category.', 'error', true, true);
  120. }
  121. return $this->responseRedirect('admin.categories.index', 'Category deleted successfully' ,'success',false, false);
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement