Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 116 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| TemplateCategoryController | |
0.00% |
0 / 116 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
| create | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| update | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| list | |
0.00% |
0 / 71 |
|
0.00% |
0 / 1 |
2 | |||
| details | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| delete | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| dependency | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v1; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Helpers\ChangeManagement\TemplateCategory\Move; |
| 7 | use App\Http\Helpers\ChangeManagement\TemplateCategory\Remove; |
| 8 | use App\Http\Models\TemplateCategory; |
| 9 | use App\Http\Requests\TemplateCategoryFormRequest; |
| 10 | use Illuminate\Http\JsonResponse; |
| 11 | use Illuminate\Http\Request; |
| 12 | use Illuminate\Support\Facades\Cache; |
| 13 | use Illuminate\Support\Facades\Config; |
| 14 | |
| 15 | class TemplateCategoryController extends Controller |
| 16 | { |
| 17 | public function create(TemplateCategoryFormRequest $request): JsonResponse |
| 18 | { |
| 19 | $data = $request->validated(); |
| 20 | |
| 21 | $category_slug = str_replace(' ', '', $request->name); |
| 22 | $data['slug'] = strtolower($category_slug); |
| 23 | |
| 24 | $result = TemplateCategory::create($data); |
| 25 | |
| 26 | return response()->json($result['_id']); |
| 27 | } |
| 28 | |
| 29 | public function update(TemplateCategoryFormRequest $request, $categoryId): JsonResponse |
| 30 | { |
| 31 | $data = $request->validated(); |
| 32 | $searchArray = ['_id' => $categoryId]; |
| 33 | |
| 34 | $category_slug = str_replace(' ', '', $request->name); |
| 35 | $data['slug'] = strtolower($category_slug); |
| 36 | |
| 37 | $result = TemplateCategory::where($searchArray)->update($data); |
| 38 | |
| 39 | |
| 40 | Cache::forget('template_category_listlist'); |
| 41 | |
| 42 | return response()->json((bool) $result); |
| 43 | } |
| 44 | |
| 45 | public function list(Request $request) |
| 46 | { |
| 47 | $cacheKey = 'template_category_list'.'list'; |
| 48 | |
| 49 | // use 604800s = 60 seconds * 60 minutes * 24 hours * 7 days = 1 week since the endpoint data doesn't change frequently. |
| 50 | $result = Cache::remember($cacheKey, 604800, function () { |
| 51 | return TemplateCategory::raw(function ($collection) { |
| 52 | return $collection->aggregate([ |
| 53 | ['$sort' => ['position' => 1]], // sort the result by the template category position |
| 54 | ['$lookup' => [ |
| 55 | 'from' => 'template_subcategory_lv1', // Perform a left outer join on the template_subcategory_lv1 collection |
| 56 | 'let' => ['id' => [ '$toString' => '$_id' ]], // Convert mongo id to string to allow for join comparison |
| 57 | 'pipeline' => [ |
| 58 | [ |
| 59 | '$match' => [ |
| 60 | '$expr' => [ |
| 61 | '$eq' => ['$$id', '$parent_category_id'] // Join comparison |
| 62 | ] |
| 63 | ], |
| 64 | ], |
| 65 | ['$lookup' => [ // Repeat the last three comments for the template_subcategory_lv2 |
| 66 | 'from' => 'template_subcategory_lv2', |
| 67 | 'let' => ['subId' => [ '$toString' => '$_id' ]], |
| 68 | 'pipeline' => [ |
| 69 | [ |
| 70 | '$match' => [ |
| 71 | '$expr' => [ |
| 72 | '$eq' => ['$$subId', '$parent_sub_category_id'] |
| 73 | ] |
| 74 | ] |
| 75 | ], |
| 76 | [ |
| 77 | '$addFields' => [ // Convert mongo object id to string |
| 78 | 'id' => [ '$toString' => '$_id' ], |
| 79 | 'updated_at' => [ // Convert dates from timestamps to carbon format |
| 80 | '$dateToString' => [ |
| 81 | 'format' => '%Y-%m-%dT%H:%M:%S.%LZ', |
| 82 | 'date' => '$updated_at' |
| 83 | ] |
| 84 | ], |
| 85 | 'created_at' => [ |
| 86 | '$dateToString' => [ |
| 87 | 'format' => '%Y-%m-%dT%H:%M:%S.%LZ', |
| 88 | 'date' => '$created_at' |
| 89 | ] |
| 90 | ] |
| 91 | ] |
| 92 | ] |
| 93 | ], |
| 94 | 'as' => 'subcategory_lv2' |
| 95 | ]], |
| 96 | [ |
| 97 | '$addFields' => [ |
| 98 | 'id' => [ '$toString' => '$_id' ], |
| 99 | 'updated_at' => [ |
| 100 | '$dateToString' => [ |
| 101 | 'format' => '%Y-%m-%dT%H:%M:%S.%LZ', |
| 102 | 'date' => '$updated_at' |
| 103 | ] |
| 104 | ], |
| 105 | 'created_at' => [ |
| 106 | '$dateToString' => [ |
| 107 | 'format' => '%Y-%m-%dT%H:%M:%S.%LZ', |
| 108 | 'date' => '$created_at' |
| 109 | ] |
| 110 | ] |
| 111 | ] |
| 112 | ] |
| 113 | ], |
| 114 | 'as' => 'subcategory_lv1' |
| 115 | ]] |
| 116 | ]); |
| 117 | }); |
| 118 | }); |
| 119 | |
| 120 | return response()->json($result); |
| 121 | } |
| 122 | |
| 123 | public function details($categoryId) |
| 124 | { |
| 125 | $searchArray = ['_id' => $categoryId]; |
| 126 | $template = TemplateCategory::with('subcategory_lv1')->where($searchArray)->first(); |
| 127 | $template['templates_count'] = $template->templates()->count(); |
| 128 | |
| 129 | return response()->json($template); |
| 130 | } |
| 131 | |
| 132 | public function delete(TemplateCategoryFormRequest $request, $categoryId): JsonResponse |
| 133 | { |
| 134 | $data = $request->validated(); |
| 135 | |
| 136 | switch ($data['type']) { |
| 137 | case 'remove': |
| 138 | $obj = new Remove(); |
| 139 | $obj->resolve($categoryId); |
| 140 | break; |
| 141 | |
| 142 | case 'move': |
| 143 | $obj = new Move(); |
| 144 | $obj->categoryId = $data['category_id']; |
| 145 | $obj->resolve($categoryId); |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | $searchArray = ['_id' => $categoryId]; |
| 150 | $result = TemplateCategory::where($searchArray)->delete(); |
| 151 | |
| 152 | Cache::forget('template_category_listlist'); |
| 153 | |
| 154 | return response()->json((bool) $result); |
| 155 | } |
| 156 | |
| 157 | public function dependency($categoryId, $type) |
| 158 | { |
| 159 | $cacheKey = 'template_category_dependency_'.$categoryId.'_type_'.$type; |
| 160 | $result = Cache::remember($cacheKey, Config::get('cache.expiry'), function () use ($categoryId, $type) { |
| 161 | $result = []; |
| 162 | switch ($type) { |
| 163 | case 'remove': |
| 164 | $obj = new Remove(); |
| 165 | $result = $obj->dependency($categoryId); |
| 166 | break; |
| 167 | |
| 168 | case 'move': |
| 169 | $obj = new Move(); |
| 170 | $result = $obj->dependency($categoryId); |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | return $result; |
| 175 | }); |
| 176 | |
| 177 | return response()->json($result); |
| 178 | } |
| 179 | } |