|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Api\V1; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use App\Http\Requests\SearchRequest; |
| 8 | +use Illuminate\Http\Response; |
| 9 | +use App\Repositories\Activity\ActivityRepositoryInterface; |
| 10 | +use Illuminate\Support\Facades\Validator; |
| 11 | + |
| 12 | +/** |
| 13 | + * @authenticated |
| 14 | + * |
| 15 | + * @group Search API |
| 16 | + * |
| 17 | + * APIs for search |
| 18 | + */ |
| 19 | +class SearchController extends Controller |
| 20 | +{ |
| 21 | + private $activityRepository; |
| 22 | + |
| 23 | + /** |
| 24 | + * SearchController constructor. |
| 25 | + * |
| 26 | + * @param ActivityRepositoryInterface $activityRepository |
| 27 | + */ |
| 28 | + public function __construct(ActivityRepositoryInterface $activityRepository) |
| 29 | + { |
| 30 | + $this->activityRepository = $activityRepository; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Deep Linking Search |
| 35 | + * |
| 36 | + * Search projects, playlists and activities for deep linking |
| 37 | + * |
| 38 | + * @queryParam query required Query to search. Example: test |
| 39 | + * @queryParam sort Field to sort by. Example: created_at |
| 40 | + * @queryParam order Order to sort by. Example: desc |
| 41 | + * @queryParam from Index where the pagination start from. Example: 0 |
| 42 | + * @queryParam size Number of records to return. Example: 10 |
| 43 | + * |
| 44 | + * @response { |
| 45 | + * "projects": { |
| 46 | + * "457": { |
| 47 | + * "id": 457, |
| 48 | + * "name": "Text Structure Lesson 2 Problem and Solution", |
| 49 | + * "description": "Learning Objective\nThe learner will define and describe the main characteristics...", |
| 50 | + * "thumb_url": "/storage/uploads/3zjZuLoQrRk0MZ5wP7UPyOut5zUybf3tW3a4Q2M1.png", |
| 51 | + * "mongo_userid": "5ef5300e41668b53ea5ed1b3", |
| 52 | + * "starter_project": false, |
| 53 | + * "created_at": "2020-07-17T17:49:01.000000Z", |
| 54 | + * "updated_at": "2020-08-06T13:28:07.000000Z", |
| 55 | + * "deleted_at": null, |
| 56 | + * "playlists": { |
| 57 | + * "225": { |
| 58 | + * "id": 225, |
| 59 | + * "title": "Solving Ratio and Rate Problems", |
| 60 | + * "project_id": 64, |
| 61 | + * "order": null, |
| 62 | + * "mongo_projectid": "5f3ae92a924a1d5ddf44dd80", |
| 63 | + * "created_at": null, |
| 64 | + * "updated_at": null, |
| 65 | + * "deleted_at": null, |
| 66 | + * "activities": { |
| 67 | + * "993": { |
| 68 | + * "id": 993, |
| 69 | + * "playlist_id": 225, |
| 70 | + * "title": "", |
| 71 | + * "type": "h5p", |
| 72 | + * "content": "", |
| 73 | + * "h5p_content_id": 17474, |
| 74 | + * "thumb_url": "/storage/uploads/5f3aedeba24fb.jpeg", |
| 75 | + * "subject_id": null, |
| 76 | + * "education_level_id": null, |
| 77 | + * "shared": false, |
| 78 | + * "order": 11, |
| 79 | + * "mongo_playlistid": "5f3aeccd924a1d5ddf44ddd0", |
| 80 | + * "created_at": null, |
| 81 | + * "updated_at": null, |
| 82 | + * "deleted_at": null |
| 83 | + * } |
| 84 | + * } |
| 85 | + * } |
| 86 | + * } |
| 87 | + * } |
| 88 | + * } |
| 89 | + * } |
| 90 | + * @response 400 { |
| 91 | + * "errors": { |
| 92 | + * "query": [ |
| 93 | + * "The query field is required." |
| 94 | + * ] |
| 95 | + * } |
| 96 | + * } |
| 97 | + * @param SearchRequest $searchRequest |
| 98 | + * @return Response |
| 99 | + */ |
| 100 | + public function search(SearchRequest $searchRequest) |
| 101 | + { |
| 102 | + $data = $searchRequest->validated(); |
| 103 | + |
| 104 | + $projects = $this->activityRepository->searchForm($data); |
| 105 | + |
| 106 | + return response([ |
| 107 | + 'projects' => $projects, |
| 108 | + ], 200); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Advance search |
| 113 | + * |
| 114 | + * Advance search for projects, playlists and activities |
| 115 | + * |
| 116 | + * @queryParam query required Query to search. Example: test |
| 117 | + * @queryParam negativeQuery Terms that should not exist. Example: badword |
| 118 | + * @queryParam userIds Array of user ids to match. Example: [1] |
| 119 | + * @queryParam model Index to filter by. Example: activities |
| 120 | + * @queryParam sort Field to sort by. Example: created_at |
| 121 | + * @queryParam order Order to sort by. Example: desc |
| 122 | + * @queryParam from Index where the pagination start from. Example: 0 |
| 123 | + * @queryParam size Number of records to return. Example: 10 |
| 124 | + * |
| 125 | + * @apiResourceCollection App\Http\Resources\V1\SearchResource |
| 126 | + * @apiResourceModel App\Models\Activity |
| 127 | + * |
| 128 | + * @response { |
| 129 | + * "data": [ |
| 130 | + * { |
| 131 | + * "id": 457, |
| 132 | + * "thumb_url": "/storage/uploads/3zjZuLoQrRk0MZ5wP7UPyOut5zUybf3tW3a4Q2M1.png", |
| 133 | + * "title": "Text Structure Lesson 2 Problem and Solution", |
| 134 | + * "description": "Learning Objective\nThe learner will define and describe the main characteristics...", |
| 135 | + * "model": "Project", |
| 136 | + * "user": { |
| 137 | + * "id": 1, |
| 138 | + * "name": "localuser", |
| 139 | + |
| 140 | + * "email_verified_at": null, |
| 141 | + * "created_at": "2020-08-22T12:13:52.000000Z", |
| 142 | + * "updated_at": "2020-08-22T12:13:52.000000Z", |
| 143 | + * "first_name": "test", |
| 144 | + * "last_name": "test", |
| 145 | + * "organization_name": "organization_name", |
| 146 | + * "job_title": "job_title", |
| 147 | + * "address": null, |
| 148 | + * "phone_number": null, |
| 149 | + * "organization_type": null, |
| 150 | + * "website": null, |
| 151 | + * "deleted_at": null, |
| 152 | + * "role": null, |
| 153 | + * "gapi_access_token": null, |
| 154 | + * "pivot": { |
| 155 | + * "project_id": 457, |
| 156 | + * "user_id": 1, |
| 157 | + * "role": "teacher", |
| 158 | + * "created_at": "2020-08-25T09:35:35.000000Z", |
| 159 | + * "updated_at": "2020-08-25T09:35:35.000000Z" |
| 160 | + * } |
| 161 | + * } |
| 162 | + * }, |
| 163 | + * { |
| 164 | + * "id": 1102, |
| 165 | + * "title": "All About That Text", |
| 166 | + * "model": "Playlist", |
| 167 | + * "user": null |
| 168 | + * } |
| 169 | + * ], |
| 170 | + * "meta": { |
| 171 | + * "projects": 7, |
| 172 | + * "playlists": 3, |
| 173 | + * "activities": 2, |
| 174 | + * "total": 12 |
| 175 | + * } |
| 176 | + * } |
| 177 | + * @response 400 { |
| 178 | + * "errors": { |
| 179 | + * "userIds": [ |
| 180 | + * "The user ids must be an array." |
| 181 | + * ] |
| 182 | + * } |
| 183 | + * } |
| 184 | + * |
| 185 | + * @param SearchRequest $searchRequest |
| 186 | + * @return Response |
| 187 | + */ |
| 188 | + public function advance(SearchRequest $searchRequest) |
| 189 | + { |
| 190 | + $data = $searchRequest->validated(); |
| 191 | + |
| 192 | + $results = $this->activityRepository->advanceSearchForm($data); |
| 193 | + |
| 194 | + return $results; |
| 195 | + } |
| 196 | +} |
0 commit comments