Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
CategoryController
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 8
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 index
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 store
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 show
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 edit
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
 destroy
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Http\Controllers\admin;
4
5use App\Http\Controllers\Controller;
6use App\Http\Requests\ConfigurationRequest;
7use App\Models\Category;
8use App\Repository\ConfigurationRepository;
9use App\Service\PosService;
10use App\Service\TableName;
11use App\Traits\PosTrait;
12use Illuminate\Http\Request;
13use Illuminate\Http\Response;
14use Illuminate\Support\Facades\DB;
15use Illuminate\Support\Str;
16use Illuminate\Validation\Rule;
17
18class CategoryController extends Controller
19{
20    use PosTrait;
21
22    protected $repository;
23
24    public function __construct(ConfigurationRepository $repository)
25    {
26        $this->repository = $repository;
27    }
28
29    /**
30     * Display a listing of the resource.
31     *
32     * @return Response
33     */
34    public function index()
35    {
36        $data['categories'] = Category::getAll();
37        $data['categories_orderBy'] = Category::getAll(false, 'name');
38
39        return view('admin.configuration.category')->with($data);
40    }
41
42    /**
43     * Show the form for creating a new resource.
44     *
45     * @return Response
46     */
47    public function create()
48    {
49        //
50    }
51
52    /**
53     * Store a newly created resource in storage.
54     *
55     * @param  Request  $request
56     * @return Response
57     */
58    public function store(ConfigurationRequest $request)
59    {
60        $data['request'] = $request->validated();
61        try {
62            DB::beginTransaction();
63            $table_name = TableName::CATEGORY;
64            $category = $this->repository::storeConfig($table_name, $data);
65
66            if ($request->hasFile('thumbnail')) {
67                $data['thumbnail'] = $this->FileProcessing($request->file('thumbnail'), PosService::CATEGORY_IMAGE, 429, 500);
68                $category->update(['thumbnail' => $data['thumbnail']]);
69            }
70
71            DB::commit();
72
73            return redirect()->back()->with('success', 'Category Successfully Inserted');
74        } catch (\Throwable $e) {
75            DB::rollBack();
76            report($e);
77
78            return back()->withInput()->with('error', 'Could not save the category.');
79        }
80
81    }
82
83    /**
84     * Display the specified resource.
85     *
86     * @param  int  $id
87     * @return Response
88     */
89    public function show($id)
90    {
91        return 'hello data';
92    }
93
94    /**
95     * Show the form for editing the specified resource.
96     *
97     * @param  int  $id
98     * @return Response
99     */
100    public function edit($id)
101    {
102        $data['category'] = Category::findById($id);
103        $data['categories'] = Category::getAll();
104        $data['categories_orderBy'] = Category::getAll(false, 'name');
105
106        return view('admin.configuration.category')->with($data);
107    }
108
109    /**
110     * Update the specified resource in storage.
111     *
112     * @param  int  $id
113     * @return Response
114     */
115    public function update(Request $request, $id)
116    {
117        $data['request'] = $request->validate([
118            'name' => ['required', Rule::unique('categories')->where('parent_id', $request->parent_id)->ignore($id)],
119            'parent_id' => 'nullable',
120            'description' => 'nullable',
121        ]);
122        try {
123            $category = Category::findById($id);
124
125            DB::beginTransaction();
126
127            $data['value'] = ['slug' => Str::slug($request->name)];
128            $data = array_merge($data['value'], $data['request']);
129
130            if ($request->hasFile('thumbnail')) {
131                $data['thumbnail'] = $this->FileProcessing($request->file('thumbnail'), PosService::CATEGORY_IMAGE, 429, 500);
132            }
133            $category->update($data);
134
135            DB::commit();
136
137            return redirect()->route('admin.category.index')->with('success', 'Category Successfully Inserted');
138        } catch (\Throwable $e) {
139            DB::rollBack();
140            report($e);
141
142            return back()->withInput()->with('error', 'Could not update the category.');
143        }
144    }
145
146    /**
147     * Remove the specified resource from storage.
148     *
149     * @param  int  $id
150     * @return Response
151     */
152    public function destroy($id)
153    {
154        $category = Category::findById($id);
155        $deleteImage = $category->image;
156        if (file_exists($deleteImage)) {
157            File::Delete($deleteImage);
158        }
159        $category->delete();
160
161        return redirect()->back()->with('delete', 'Category Deleted Successfully');
162    }
163}