AttributeController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Controllers\Admin\Attributes;
  3. use App\Http\Controllers\Controller;
  4. use App\Shop\Attributes\Exceptions\AttributeNotFoundException;
  5. use App\Shop\Attributes\Exceptions\CreateAttributeErrorException;
  6. use App\Shop\Attributes\Exceptions\UpdateAttributeErrorException;
  7. use App\Shop\Attributes\Repositories\AttributeRepository;
  8. use App\Shop\Attributes\Repositories\AttributeRepositoryInterface;
  9. use App\Shop\Attributes\Requests\CreateAttributeRequest;
  10. use App\Shop\Attributes\Requests\UpdateAttributeRequest;
  11. class AttributeController extends Controller
  12. {
  13. private $attributeRepo;
  14. /**
  15. * AttributeController constructor.
  16. * @param AttributeRepositoryInterface $attributeRepository
  17. */
  18. public function __construct(AttributeRepositoryInterface $attributeRepository)
  19. {
  20. $this->attributeRepo = $attributeRepository;
  21. }
  22. /**
  23. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  24. */
  25. public function index()
  26. {
  27. $results = $this->attributeRepo->listAttributes();
  28. $attributes = $this->attributeRepo->paginateArrayResults($results->all());
  29. return view('admin.attributes.list', compact('attributes'));
  30. }
  31. /**
  32. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  33. */
  34. public function create()
  35. {
  36. return view('admin.attributes.create');
  37. }
  38. /**
  39. * @param CreateAttributeRequest $request
  40. * @return \Illuminate\Http\RedirectResponse
  41. */
  42. public function store(CreateAttributeRequest $request)
  43. {
  44. $attribute = $this->attributeRepo->createAttribute($request->except('_token'));
  45. $request->session()->flash('message', 'Create attribute successful!');
  46. return redirect()->route('admin.attributes.edit', $attribute->id);
  47. }
  48. /**
  49. * @param $id
  50. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  51. */
  52. public function show($id)
  53. {
  54. try {
  55. $attribute = $this->attributeRepo->findAttributeById($id);
  56. $attributeRepo = new AttributeRepository($attribute);
  57. return view('admin.attributes.show', [
  58. 'attribute' => $attribute,
  59. 'values' => $attributeRepo->listAttributeValues()
  60. ]);
  61. } catch (AttributeNotFoundException $e) {
  62. request()->session()->flash('error', 'The attribute you are looking for is not found.');
  63. return redirect()->route('admin.attributes.index');
  64. }
  65. }
  66. /**
  67. * @param int $id
  68. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  69. */
  70. public function edit($id)
  71. {
  72. $attribute = $this->attributeRepo->findAttributeById($id);
  73. return view('admin.attributes.edit', compact('attribute'));
  74. }
  75. /**
  76. * @param UpdateAttributeRequest $request
  77. * @param $id
  78. * @return \Illuminate\Http\RedirectResponse
  79. */
  80. public function update(UpdateAttributeRequest $request, $id)
  81. {
  82. try {
  83. $attribute = $this->attributeRepo->findAttributeById($id);
  84. $attributeRepo = new AttributeRepository($attribute);
  85. $attributeRepo->updateAttribute($request->except('_token'));
  86. $request->session()->flash('message', 'Attribute update successful!');
  87. return redirect()->route('admin.attributes.edit', $attribute->id);
  88. } catch (UpdateAttributeErrorException $e) {
  89. $request->session()->flash('error', $e->getMessage());
  90. return redirect()->route('admin.attributes.edit', $id)->withInput();
  91. }
  92. }
  93. /**
  94. * @param $id
  95. * @return bool|null
  96. */
  97. public function destroy($id)
  98. {
  99. $this->attributeRepo->findAttributeById($id)->delete();
  100. request()->session()->flash('message', 'Attribute deleted successfully!');
  101. return redirect()->route('admin.attributes.index');
  102. }
  103. }