Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 89 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| SearchDataController | |
0.00% |
0 / 89 |
|
0.00% |
0 / 4 |
552 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
12 | |||
| advancedSearch | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
210 | |||
| categories | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| flycutName | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v1; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Models\Shortcut; |
| 7 | use App\Http\Models\ShortcutCategory; |
| 8 | use App\Http\Models\Template; |
| 9 | use App\Http\Models\TemplateCategory; |
| 10 | use Carbon\Carbon; |
| 11 | use Exception; |
| 12 | use Illuminate\Http\JsonResponse; |
| 13 | use Illuminate\Http\Request; |
| 14 | use Illuminate\Support\Facades\Validator; |
| 15 | use Symfony\Component\HttpFoundation\Response; |
| 16 | |
| 17 | class SearchDataController extends Controller |
| 18 | { |
| 19 | public function index(Request $request): JsonResponse |
| 20 | { |
| 21 | $validator = Validator::make($request->all(), [ |
| 22 | 'query' => 'required|min:1', |
| 23 | ]); |
| 24 | |
| 25 | if ($validator->fails()) { |
| 26 | return response()->json(['data' => [], 'message' => $validator->errors()->first('query')], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 27 | } |
| 28 | |
| 29 | $data = []; |
| 30 | $query = "%{$request->get('query')}%"; |
| 31 | |
| 32 | try { |
| 33 | $shortcuts = Shortcut::where(function ($queryBuilder) use ($query) { |
| 34 | $queryBuilder->where('title', 'like', $query) |
| 35 | ->orWhere('text', 'like', $query) |
| 36 | ->orWhere('shortcut', 'like', $query) |
| 37 | ->orWhere('first_line', 'like', $query); |
| 38 | }) |
| 39 | ->limit(50)->get(); |
| 40 | |
| 41 | $data[] = [ |
| 42 | 'type' => 'Shortcuts', |
| 43 | 'items' => $shortcuts, |
| 44 | ]; |
| 45 | |
| 46 | $templates = Template::where('title', 'like', $query) |
| 47 | ->orWhere('text', 'like', $query) |
| 48 | ->orWhere('shortcut', 'like', $query) |
| 49 | ->orWhere('first_line', 'like', $query) |
| 50 | ->limit(50)->get()->toArray(); |
| 51 | |
| 52 | $data[] = [ |
| 53 | 'type' => 'FlyPlates', |
| 54 | 'items' => $templates, |
| 55 | ]; |
| 56 | |
| 57 | return response()->json(['data' => $data, 'message' => 'Succeed'], Response::HTTP_OK); |
| 58 | } catch (Exception $e) { |
| 59 | return response()->json(['data' => [], 'message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public function advancedSearch(Request $request): JsonResponse |
| 64 | { |
| 65 | try { |
| 66 | /* Params */ |
| 67 | $flycut = $request->get('flycut'); |
| 68 | $title = $request->get('title'); |
| 69 | $hasTheWords = $request->get('hasTheWords'); |
| 70 | $doesntHave = $request->get('doesntHave'); |
| 71 | $createdWithin = $request->get('createdWithin'); |
| 72 | $date = $request->get('date'); |
| 73 | $bySearch = $request->get('bySearch'); |
| 74 | $hasAttachment = $request->get('hasAttachment'); |
| 75 | $hasHyperlink = $request->get('hasHyperlink'); |
| 76 | /* END: Params */ |
| 77 | |
| 78 | $search = []; |
| 79 | |
| 80 | if (filled($flycut)) { |
| 81 | $search[] = ['shortcut', 'like', '%'.$flycut.'%']; |
| 82 | } |
| 83 | if (filled($title)) { |
| 84 | $search[] = ['title', 'like', '%'.$title.'%']; |
| 85 | } |
| 86 | if (filled($hasTheWords)) { |
| 87 | $search[] = ['text', 'like', '%'.$hasTheWords.'%']; |
| 88 | } |
| 89 | if (filled($doesntHave)) { |
| 90 | $search[] = ['text', 'not like', '%'.$doesntHave.'%']; |
| 91 | } |
| 92 | if (filled($bySearch)) { |
| 93 | $search[] = ['category_id', '=', $bySearch]; |
| 94 | } |
| 95 | |
| 96 | if(filled($hasHyperlink) && ($hasHyperlink == 'true')) { |
| 97 | $search[] = ['html', 'regex', '/href\s*=\s*["\']([^"\']*)["\']/i']; |
| 98 | } |
| 99 | if(filled($hasAttachment) && ($hasAttachment == 'true')) { |
| 100 | $search[] = ['html', 'regex', '/src\s*=\s*["\']([^"\']*)/i']; |
| 101 | } |
| 102 | |
| 103 | //Templates are created by admin and so date of creation is irrelevant to user search |
| 104 | $templateCategories = Template::where($search)->limit(50)->get(); |
| 105 | |
| 106 | if (filled($createdWithin) && filled($date)) { |
| 107 | $dateMap = [ |
| 108 | '1d' => 1, '3d' => 3, '1w' => 7, '2w' => 14, '1m' => 30 |
| 109 | ]; |
| 110 | |
| 111 | if (isset($dateMap[$createdWithin])) { |
| 112 | $createdAtStar = Carbon::create($date)->subDays($dateMap[$createdWithin]); |
| 113 | $createdAtEnd = Carbon::create($date)->addDays($dateMap[$createdWithin]); |
| 114 | |
| 115 | $search[] = ['created_at', '>=', $createdAtStar]; |
| 116 | $search[] = ['created_at', '<=', $createdAtEnd]; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | //Shortcuts are filtered by users based on App\Http\Scopes\UserScope |
| 121 | $shortcuts = Shortcut::where($search)->limit(50)->get(); |
| 122 | |
| 123 | return response()->json(['data' => $shortcuts, 'data2' => $templateCategories, 'message' => 'Succeed'], Response::HTTP_OK); |
| 124 | } catch (Exception $e) { |
| 125 | return response()->json(['data' => [], 'message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public function categories(Request $request): JsonResponse |
| 130 | { |
| 131 | try { |
| 132 | $shortcutCategories = ShortcutCategory::limit(100)->get(['id', 'name'])->makeHidden('shortcuts_count'); |
| 133 | $templateCategories = TemplateCategory::limit(100)->get(['id', 'name']); |
| 134 | $data = [ |
| 135 | [ |
| 136 | 'type' => 'FlyCuts', |
| 137 | 'items' => $shortcutCategories, |
| 138 | ], |
| 139 | [ |
| 140 | 'type' => 'FlyPlates', |
| 141 | 'items' => $templateCategories, |
| 142 | ] |
| 143 | ]; |
| 144 | return response()->json(['data' => $data, 'message' => 'Succeed'], Response::HTTP_OK); |
| 145 | } catch (Exception $e) { |
| 146 | return response()->json(['data' => [], 'message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public function flycutName(Request $request): JsonResponse |
| 151 | { |
| 152 | try { |
| 153 | $query = $request->get('query'); |
| 154 | $result = Shortcut::where('shortcut', $query)->get(); |
| 155 | $exists = $result && count($result) > 0 ? true : false; |
| 156 | } catch (Exception $e) { |
| 157 | return response()->json(['data' => [], 'message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
| 158 | } |
| 159 | |
| 160 | return response()->json(['data' => ['exists' => $exists], 'message' => 'Succeed'], Response::HTTP_OK); |
| 161 | } |
| 162 | } |