BrandController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Http\Controllers\Admin\Brands;
  3. use App\Http\Controllers\Controller;
  4. use App\Shop\Brands\Repositories\BrandRepository;
  5. use App\Shop\Brands\Repositories\BrandRepositoryInterface;
  6. use App\Shop\Brands\Requests\CreateBrandRequest;
  7. use App\Shop\Brands\Requests\UpdateBrandRequest;
  8. class BrandController extends Controller
  9. {
  10. /**
  11. * @var BrandRepositoryInterface
  12. */
  13. private $brandRepo;
  14. /**
  15. * BrandController constructor.
  16. *
  17. * @param BrandRepositoryInterface $brandRepository
  18. */
  19. public function __construct(BrandRepositoryInterface $brandRepository)
  20. {
  21. $this->brandRepo = $brandRepository;
  22. }
  23. /**
  24. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  25. */
  26. public function index()
  27. {
  28. $data = $this->brandRepo->paginateArrayResults($this->brandRepo->listBrands(['*'], 'name', 'asc')->all());
  29. return view('admin.brands.list', ['brands' => $data]);
  30. }
  31. /**
  32. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  33. */
  34. public function create()
  35. {
  36. return view('admin.brands.create');
  37. }
  38. /**
  39. * @param CreateBrandRequest $request
  40. *
  41. * @return \Illuminate\Http\RedirectResponse
  42. */
  43. public function store(CreateBrandRequest $request)
  44. {
  45. $this->brandRepo->createBrand($request->all());
  46. return redirect()->route('admin.brands.index')->with('message', 'Create brand successful!');
  47. }
  48. /**
  49. * @param $id
  50. *
  51. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  52. */
  53. public function edit($id)
  54. {
  55. return view('admin.brands.edit', ['brand' => $this->brandRepo->findBrandById($id)]);
  56. }
  57. /**
  58. * @param UpdateBrandRequest $request
  59. * @param $id
  60. *
  61. * @return \Illuminate\Http\RedirectResponse
  62. * @throws \App\Shop\Brands\Exceptions\UpdateBrandErrorException
  63. */
  64. public function update(UpdateBrandRequest $request, $id)
  65. {
  66. $brand = $this->brandRepo->findBrandById($id);
  67. $brandRepo = new BrandRepository($brand);
  68. $brandRepo->updateBrand($request->all());
  69. return redirect()->route('admin.brands.edit', $id)->with('message', 'Update successful!');
  70. }
  71. /**
  72. * @param $id
  73. *
  74. * @return \Illuminate\Http\RedirectResponse
  75. * @throws \Exception
  76. */
  77. public function destroy($id)
  78. {
  79. $brand = $this->brandRepo->findBrandById($id);
  80. $brandRepo = new BrandRepository($brand);
  81. $brandRepo->dissociateProducts();
  82. $brandRepo->deleteBrand();
  83. return redirect()->route('admin.brands.index')->with('message', 'Delete successful!');
  84. }
  85. }