diff --git a/app/Http/Controllers/Api/V1/MediaSourceController.php b/app/Http/Controllers/Api/V1/MediaSourceController.php new file mode 100644 index 000000000..3b99d0103 --- /dev/null +++ b/app/Http/Controllers/Api/V1/MediaSourceController.php @@ -0,0 +1,148 @@ +mediaSourceRepository = $mediaSourceRepository; + } + + /** + * Get All Media Source for listing. + * + * @urlParam query string Query to search media sources settings against name or media type Example: Kaltura or Video/Image + * @urlParam size integer Size to show per page records Example: 10 + * @urlParam order_by_column string To sort data with specific column Example: name + * @urlParam order_by_type string To sort data in ascending or descending order Example: asc + * @urlParam filter string to search media sources by Image or Video Example: Video/Image + * + * @responseFile responses/admin/mediasources/media-source-settings-list.json + * + * @param SearchMediaSourceRequest $request + * @return MediaSourceCollection + */ + public function index(SearchMediaSourceRequest $request) + { + $collections = $this->mediaSourceRepository->getAll($request->all()); + return new MediaSourceCollection($collections); + } + + /** + * Get Media Source + * + * Get the specified media source details. + * + * @urlParam id required The Id of a media source Example: 1 + * + * @responseFile responses/admin/mediasources/media-source-settings-show.json + * + * @response 500 { + * "errors": [ + * "Media Source not found!" + * ] + * } + * + * @param $id int + * @return MediaSourceResource + */ + public function show($id) + { + $setting = $this->mediaSourceRepository->find($id); + return new MediaSourceResource($setting); + } + + /** + * Create Media Source + * + * Creates the new media source in database + * + * @bodyParam name string required Media Source name Example: Kaltura + * @bodyParam media_type string required Media Source Type Example: Video/Image + * + * @responseFile 200 responses/admin/mediasources/media-source-settings-create.json + * + * @response 500 { + * "errors": [ + * "Unable to create media source, please try again later!" + * ] + * } + * + * @param StoreMediaSourceRequest $request + * @return MediaSourceResource + */ + public function store(StoreMediaSourceRequest $request) + { + $response = $this->mediaSourceRepository->create($request->all()); + return response(['message' => $response['message'], 'data' => new MediaSourceResource($response['data'])], 200); + } + + /** + * Update Media Source + * + * Update the specific media source record + * + * @bodyParam name string required Media Source name Example: Kaltura + * @bodyParam media_type string required Media Source Type Example: Video/Image + * + * @responseFile 200 responses/admin/mediasources/media-source-settings-update.json + * + * @response 500 { + * "errors": [ + * "Unable to update media source, please try again later!" + * ] + * } + * + * @param UpdateMediaSourceRequest $request + * @param $id int + * @return MediaSourceResource + */ + public function update(UpdateMediaSourceRequest $request, $id) + { + $response = $this->mediaSourceRepository->update($id, $request->all()); + return response(['message' => $response['message'], 'data' => new MediaSourceResource($response['data'])], 200); + } + + /** + * Delete Media Source + * + * Delete the specific media source record + * + * @urlParam id required The Id of a Media Source Example: 1 + * + * @param $id int + * + * @responseFile 200 responses/admin/mediasources/media-source-settings-destory.json + * + * @response 500 { + * "errors": [ + * "Unable to delete media source, please try again later!" + * ] + * } + */ + public function destroy($id) + { + return response(['message' => $this->mediaSourceRepository->destroy($id)], 200); + } +} diff --git a/app/Http/Requests/V1/MediaSource/SearchMediaSourceRequest.php b/app/Http/Requests/V1/MediaSource/SearchMediaSourceRequest.php new file mode 100644 index 000000000..5d6a5fdd5 --- /dev/null +++ b/app/Http/Requests/V1/MediaSource/SearchMediaSourceRequest.php @@ -0,0 +1,48 @@ + 'string|max:255', + 'size' => 'integer|max:100', + 'order_by_column' => 'string|in:name,media_type', + 'order_by_type' => 'string|in:asc,desc', + 'filter' => 'string|in:Video,Image' + ]; + } + + /** + * Get custom messages for validator errors. + * + * @return array + */ + public function messages() + { + return [ + 'order_by_column.in' => 'The selected order by column should be name OR media type only', + 'order_by_type.in' => 'The selected order by type should be asc OR desc only', + 'order_by_type.in' => 'The selected filter should be Video OR Image only' + ]; + } +} diff --git a/app/Http/Requests/V1/MediaSource/StoreMediaSourceRequest.php b/app/Http/Requests/V1/MediaSource/StoreMediaSourceRequest.php new file mode 100644 index 000000000..f2659f7ba --- /dev/null +++ b/app/Http/Requests/V1/MediaSource/StoreMediaSourceRequest.php @@ -0,0 +1,48 @@ + 'required|string|max:255|unique:media_sources,name,NULL,id,deleted_at,NULL,media_type,' . $mediaType, + 'media_type' => 'required|in:Video,Image' + ]; + } + + /** + * Get custom messages for validator errors. + * + * @return array + */ + public function messages() + { + return [ + 'name.unique' => 'The name has already been taken with the mentioned media_type.' + ]; + } +} diff --git a/app/Http/Requests/V1/MediaSource/UpdateMediaSourceRequest.php b/app/Http/Requests/V1/MediaSource/UpdateMediaSourceRequest.php new file mode 100644 index 000000000..8cd93696a --- /dev/null +++ b/app/Http/Requests/V1/MediaSource/UpdateMediaSourceRequest.php @@ -0,0 +1,49 @@ +route('media_source_setting'); + $mediaType = request('media_type'); + return [ + 'name' => 'required|string|max:255|unique:media_sources,name, ' . $id . ' ,id,deleted_at,NULL,media_type,' . $mediaType, + 'media_type' => 'required|in:Video,Image' + ]; + } + + /** + * Get custom messages for validator errors. + * + * @return array + */ + public function messages() + { + return [ + 'name.unique' => 'The name has already been taken with the mentioned media_type.' + ]; + } +} diff --git a/app/Http/Resources/V1/MediaSource/MediaSourceCollection.php b/app/Http/Resources/V1/MediaSource/MediaSourceCollection.php new file mode 100644 index 000000000..0725ee44f --- /dev/null +++ b/app/Http/Resources/V1/MediaSource/MediaSourceCollection.php @@ -0,0 +1,18 @@ + $this->collection]; + } +} diff --git a/app/Http/Resources/V1/MediaSource/MediaSourceResource.php b/app/Http/Resources/V1/MediaSource/MediaSourceResource.php new file mode 100644 index 000000000..e79615d4c --- /dev/null +++ b/app/Http/Resources/V1/MediaSource/MediaSourceResource.php @@ -0,0 +1,18 @@ +belongsToMany('App\Models\Organization', 'organization_media_sources'); + } } diff --git a/app/Repositories/MediaSources/MediaSourcesRepository.php b/app/Repositories/MediaSources/MediaSourcesRepository.php index b22ca7d56..a3f8fa431 100644 --- a/app/Repositories/MediaSources/MediaSourcesRepository.php +++ b/app/Repositories/MediaSources/MediaSourcesRepository.php @@ -2,8 +2,12 @@ namespace App\Repositories\MediaSources; -use App\Repositories\BaseRepository; +use App\Exceptions\GeneralException; use App\Models\MediaSource; +use App\Models\Organization; +use App\Repositories\BaseRepository; +use Illuminate\Support\Facades\Log; +use App\Repositories\MediaSources\MediaSourcesInterface; class MediaSourcesRepository extends BaseRepository implements MediaSourcesInterface { @@ -17,6 +21,94 @@ public function __construct(MediaSource $model) $this->model = $model; } + /** + * @param $data array + * @return mixed + */ + public function getAll($data) + { + $perPage = isset($data['size']) ? $data['size'] : config('constants.default-pagination-per-page'); + $query = $this->model; + if (isset($data['query']) && $data['query'] !== '') { + $query->where(function ($query) use ($data) { + $query->orWhere('name', 'iLIKE', '%' . $data['query'] . '%'); + $query->orWhere('media_type', 'iLIKE', '%' . $data['query'] . '%'); + }); + } + if (isset($data['order_by_column']) && $data['order_by_column'] !== '') + { + $orderByType = isset($data['order_by_type']) ? $data['order_by_type'] : 'ASC'; + $query->orderBy($data['order_by_column'], $orderByType); + } + if (isset($data['filter']) && $data['filter'] !== '') { + $query = $query->where('media_type', $data['filter']); + } + return $query->paginate($perPage)->withQueryString(); + } + + /** + * @param $data array + * @return mixed + * @throws GeneralException + */ + public function create($data) + { + try { + if ($createSetting = $this->model->create($data)) { + return ['message' => 'Media Source created successfully!', 'data' => $createSetting]; + } + } catch (\Exception $e) { + Log::error($e->getMessage()); + } + throw new GeneralException('Unable to create media source, please try again later!'); + } + + /** + * @param $data array + * @param $id int + * @return mixed + * @throws GeneralException + */ + public function update($id, $data) + { + try { + if ($this->find($id)->update($data)) { + return ['message' => 'Media Source updated successfully!', 'data' => $this->find($id)]; + } + } catch (\Exception $e) { + Log::error($e->getMessage()); + } + throw new GeneralException('Unable to update media source, please try again later!'); + } + + /** + * @param $id int + * @return mixed + * @throws GeneralException + */ + public function find($id) + { + if ( ctype_digit($id) ) { + $setting = $this->model->find($id); + return $setting; + } + throw new GeneralException('Media Source not found!'); + } + + /** + * @param $id int + * @return mixed + * @throws GeneralException + */ + public function destroy($id) + { + if ( ctype_digit($id) ) { + $this->find($id)->delete(); + return ['message' => 'Media Source deleted!', 'data' => []]; + } + throw new GeneralException('Unable to delete media source, please try again later!'); + } + /** * To get media source id by name * diff --git a/resources/views/apidoc/index.blade.php b/resources/views/apidoc/index.blade.php index 11dff7932..d85e43d78 100644 --- a/resources/views/apidoc/index.blade.php +++ b/resources/views/apidoc/index.blade.php @@ -52291,6 +52291,642 @@
POST api/v1/video/get-direct-url
APIs for media source on admin panel.
+ +Returns the paginated response with pagination links (DataTables are fully supported - All Params).
+++Example request:
+
curl -X GET \
+ -G "http://localhost:8000/api/v1/media-source-settings?query=Komodo&filter=Video/Image&start=0&length=25" \
+ -H "Content-Type: application/json" \
+ -H "Accept: application/json"
+const url = new URL(
+ "http://localhost:8000/api/v1/media-source-settings"
+);
+
+let params = {
+ "query": "Komodo",
+ "filter": "Video/Image",
+ "start": "0",
+ "length": "25",
+};
+Object.keys(params)
+ .forEach(key => url.searchParams.append(key, params[key]));
+
+let headers = {
+ "Content-Type": "application/json",
+ "Accept": "application/json",
+};
+
+fetch(url, {
+ method: "GET",
+ headers: headers,
+})
+ .then(response => response.json())
+ .then(json => console.log(json));
+
+$client = new \GuzzleHttp\Client();
+$response = $client->get(
+ 'http://localhost:8000/api/v1/media-source-settings',
+ [
+ 'headers' => [
+ 'Content-Type' => 'application/json',
+ 'Accept' => 'application/json',
+ ],
+ 'query' => [
+ 'query'=> 'Komodo',
+ 'filter'=> 'Video/Image',
+ 'start'=> '0',
+ 'length'=> '25',
+ ],
+ ]
+);
+$body = $response->getBody();
+print_r(json_decode((string) $body));
+import requests
+import json
+
+url = 'http://localhost:8000/api/v1/media-source-settings'
+params = {
+ 'query': 'Komodo',
+ 'filter': 'Video/Image',
+ 'start': '0',
+ 'length': '25',
+}
+headers = {
+ 'Content-Type': 'application/json',
+ 'Accept': 'application/json'
+}
+response = requests.request('GET', url, headers=headers, params=params)
+response.json()
+++Example response (200):
+
{
+ "data": [
+ {
+ "id": 2,
+ "name": "YouTube",
+ "media_type": "Video",
+ "created_at": "2022-05-09T08:18:12.000000Z",
+ "updated_at": null,
+ "deleted_at": null
+ },
+ {
+ "id": 4,
+ "name": "Safari Montage",
+ "media_type": "Video",
+ "created_at": "2022-05-09T08:18:12.000000Z",
+ "updated_at": null,
+ "deleted_at": null
+ },
+ {
+ "id": 5,
+ "name": "BrightCove",
+ "media_type": "Video",
+ "created_at": "2022-05-09T08:18:12.000000Z",
+ "updated_at": null,
+ "deleted_at": null
+ },
+ {
+ "id": 6,
+ "name": "Vimeo",
+ "media_type": "Video",
+ "created_at": "2022-05-09T08:18:12.000000Z",
+ "updated_at": null,
+ "deleted_at": null
+ },
+ {
+ "id": 7,
+ "name": "My device",
+ "media_type": "Image",
+ "created_at": "2022-05-09T08:18:12.000000Z",
+ "updated_at": null,
+ "deleted_at": null
+ },
+ {
+ "id": 8,
+ "name": "Pexels",
+ "media_type": "Image",
+ "created_at": "2022-05-09T08:18:12.000000Z",
+ "updated_at": null,
+ "deleted_at": null
+ },
+ {
+ "id": 9,
+ "name": "Safari Montage",
+ "media_type": "Image",
+ "created_at": "2022-05-09T08:18:12.000000Z",
+ "updated_at": null,
+ "deleted_at": null
+ },
+ {
+ "id": 1,
+ "name": "My device",
+ "media_type": "Video",
+ "created_at": "2022-05-09T08:18:12.000000Z",
+ "updated_at": "2022-07-07T15:02:54.000000Z",
+ "deleted_at": null
+ },
+ {
+ "id": 13,
+ "name": "Komodo",
+ "media_type": "Video",
+ "created_at": "2022-08-01T09:44:58.000000Z",
+ "updated_at": null,
+ "deleted_at": null
+ },
+ {
+ "id": 19,
+ "name": "Smithsonian",
+ "media_type": "Image",
+ "created_at": "2022-09-28T08:54:01.000000Z",
+ "updated_at": null,
+ "deleted_at": null
+ }
+ ],
+ "links": {
+ "first": "http://curriki-studio-api.local/api/v1/media-source-settings?page=1",
+ "last": "http://curriki-studio-api.local/api/v1/media-source-settings?page=2",
+ "prev": null,
+ "next": "http://curriki-studio-api.local/api/v1/media-source-settings?page=2"
+ },
+ "meta": {
+ "current_page": 1,
+ "from": 1,
+ "last_page": 2,
+ "links": [
+ {
+ "url": null,
+ "label": "« Previous",
+ "active": false
+ },
+ {
+ "url": "http://curriki-studio-api.local/api/v1/media-source-settings?page=1",
+ "label": "1",
+ "active": true
+ },
+ {
+ "url": "http://curriki-studio-api.local/api/v1/media-source-settings?page=2",
+ "label": "2",
+ "active": false
+ },
+ {
+ "url": "http://curriki-studio-api.local/api/v1/media-source-settings?page=2",
+ "label": "Next »",
+ "active": false
+ }
+ ],
+ "path": "http://curriki-studio-api.local/api/v1/media-source-settings",
+ "per_page": 10,
+ "to": 10,
+ "total": 12
+ }
+}
+GET api/v1/media-source-settings
Parameter | +Status | +Description | +
---|---|---|
query |
+optional | +Media Source Name | +
filter |
+optional | +Media Type | +
start |
+optional | +Offset for getting the paginated response, Default 0. | +
length |
+optional | +Limit for getting the paginated records, Default 25. | +
Creates the new media source in database.
+++Example request:
+
curl -X POST \
+ "http://localhost:8000/api/v1/media-source-settings" \
+ -H "Content-Type: application/json" \
+ -H "Accept: application/json" \
+ -d '{"name":"Komodo","media_type":"Video/Image"}'
+
+const url = new URL(
+ "http://localhost:8000/api/v1/media-source-settings"
+);
+
+let headers = {
+ "Content-Type": "application/json",
+ "Accept": "application/json",
+};
+
+let body = {
+ "name": "Komodo",
+ "media_type": "Video/Image"
+}
+
+fetch(url, {
+ method: "POST",
+ headers: headers,
+ body: body
+})
+ .then(response => response.json())
+ .then(json => console.log(json));
+
+$client = new \GuzzleHttp\Client();
+$response = $client->post(
+ 'http://localhost:8000/api/v1/media-source-settings',
+ [
+ 'headers' => [
+ 'Content-Type' => 'application/json',
+ 'Accept' => 'application/json',
+ ],
+ 'json' => [
+ 'name' => 'https://google.com',
+ 'media_type' => 'Video/Image'
+ ],
+ ]
+);
+$body = $response->getBody();
+print_r(json_decode((string) $body));
+import requests
+import json
+
+url = 'http://localhost:8000/api/v1/media-source-settings'
+payload = {
+ "name": "Komodo",
+ "media_type": "Video/Image"
+}
+headers = {
+ 'Content-Type': 'application/json',
+ 'Accept': 'application/json'
+}
+response = requests.request('POST', url, headers=headers, json=payload)
+response.json()
+++Example response (200):
+
{
+ "message": "Media Source created successfully!",
+ "data": {
+ "name": "Komodo",
+ "media_type": "Video",
+ "updated_at": "2023-01-10T14:23:57.000000Z",
+ "created_at": "2023-01-10T14:23:57.000000Z",
+ "id": 13
+ }
+}
+++Example response (500):
+
{
+ "errors": [
+ "Unable to create media source, please try again later!"
+ ]
+}
+POST api/v1/media-source-settings
Parameter | +Type | +Status | +Description | +
---|---|---|---|
name |
+required|string|max:255|unique | +required | +Valid media source name like Kaltur or Komodo. | +
media_type |
+string | +required|in:Video,Image | +Valid media type like Video or Image. | +
Get the specified media source data.
+++Example request:
+
curl -X GET \
+ -G "http://localhost:8000/api/v1/media-source-settings/13" \
+ -H "Content-Type: application/json" \
+ -H "Accept: application/json"
+const url = new URL(
+ "http://localhost:8000/api/v1/media-source-settings/13"
+);
+
+let headers = {
+ "Content-Type": "application/json",
+ "Accept": "application/json",
+};
+
+fetch(url, {
+ method: "GET",
+ headers: headers,
+})
+ .then(response => response.json())
+ .then(json => console.log(json));
+
+$client = new \GuzzleHttp\Client();
+$response = $client->get(
+ 'http://localhost:8000/api/v1/media-source-settings/13',
+ [
+ 'headers' => [
+ 'Content-Type' => 'application/json',
+ 'Accept' => 'application/json',
+ ],
+ ]
+);
+$body = $response->getBody();
+print_r(json_decode((string) $body));
+import requests
+import json
+
+url = 'http://localhost:8000/api/v1/media-source-settings/13'
+headers = {
+ 'Content-Type': 'application/json',
+ 'Accept': 'application/json'
+}
+response = requests.request('GET', url, headers=headers)
+response.json()
+++Example response (200):
+
{
+ "data": {
+ "id": 13,
+ "name": "Komodo",
+ "media_type": "Video",
+ "created_at": "2023-01-10T13:00:26.000000Z",
+ "updated_at": "2023-01-10T13:15:38.000000Z",
+ "deleted_at": null
+ }
+}
+GET api/v1/media-source-settings/{id}
Parameter | +Status | +Description | +
---|---|---|
id |
+required | +The Id of a media source | +
Updates the media source in database.
+++Example request:
+
curl -X PUT \
+ "http://localhost:8000/api/v1/media-source-settings/13" \
+ -H "Content-Type: application/json" \
+ -H "Accept: application/json" \
+ -d '{"name":"Komodo","media_type":"Video/Image"}'
+
+const url = new URL(
+ "http://localhost:8000/api/v1/media-source-settings/13"
+);
+
+let headers = {
+ "Content-Type": "application/json",
+ "Accept": "application/json",
+};
+
+let body = {
+ "name": "Komodo",
+ "media_type": "Video/Image"
+}
+
+fetch(url, {
+ method: "PUT",
+ headers: headers,
+ body: body
+})
+ .then(response => response.json())
+ .then(json => console.log(json));
+
+$client = new \GuzzleHttp\Client();
+$response = $client->put(
+ 'http://localhost:8000/api/v1/media-source-settings/13',
+ [
+ 'headers' => [
+ 'Content-Type' => 'application/json',
+ 'Accept' => 'application/json',
+ ],
+ 'json' => [
+ 'name' => 'Komodo',
+ 'media_type' => 'Video/Image'
+ ],
+ ]
+);
+$body = $response->getBody();
+print_r(json_decode((string) $body));
+import requests
+import json
+
+url = 'http://localhost:8000/api/v1/media-source-settings/13'
+payload = {
+ "name": "Komodo",
+ "media_type": "Video/Image"
+}
+headers = {
+ 'Content-Type': 'application/json',
+ 'Accept': 'application/json'
+}
+response = requests.request('PUT', url, headers=headers, json=payload)
+response.json()
+++Example response (200):
+
{
+ "message": "Media Source updated successfully!",
+ "data": {
+ "id": 13,
+ "name": "Komodo",
+ "media_type": "Video",
+ "created_at": "2023-01-10T13:00:26.000000Z",
+ "updated_at": "2023-01-10T13:15:38.000000Z",
+ "deleted_at": null
+ }
+}
+++Example response (500):
+
{
+ "errors": [
+ "Unable to update media source, please try again later!"
+ ]
+}
+PUT api/v1/media-source-settings/{id}
PATCH api/v1/media-source-settings/{id}
Parameter | +Status | +Description | +
---|---|---|
id |
+required | +The Id of a media source | +
Parameter | +Type | +Status | +Description | +
---|---|---|---|
name |
+string | +required|string|max:255|unique | +Valid media source name. | +
media_type |
+string | +required|in:Video,Image | +Valid media type | +
Deletes the media source from database.
+++Example request:
+
curl -X DELETE \
+ "http://localhost:8000/api/v1/media-source-settings/13" \
+ -H "Content-Type: application/json" \
+ -H "Accept: application/json"
+const url = new URL(
+ "http://localhost:8000/api/v1/media-source-settings/13"
+);
+
+let headers = {
+ "Content-Type": "application/json",
+ "Accept": "application/json",
+};
+
+fetch(url, {
+ method: "DELETE",
+ headers: headers,
+})
+ .then(response => response.json())
+ .then(json => console.log(json));
+
+$client = new \GuzzleHttp\Client();
+$response = $client->delete(
+ 'http://localhost:8000/api/v1/media-source-settings/13',
+ [
+ 'headers' => [
+ 'Content-Type' => 'application/json',
+ 'Accept' => 'application/json',
+ ],
+ ]
+);
+$body = $response->getBody();
+print_r(json_decode((string) $body));
+import requests
+import json
+
+url = 'http://localhost:8000/api/v1/media-source-settings/13'
+headers = {
+ 'Content-Type': 'application/json',
+ 'Accept': 'application/json'
+}
+response = requests.request('DELETE', url, headers=headers)
+response.json()
+++Example response (200):
+
null
+++Example response (500):
+
{
+ "errors": [
+ "Unable to delete media source, please try again later!"
+ ]
+}
+DELETE api/v1/media-source-settings/{id}
Parameter | +Status | +Description | +
---|---|---|
id |
+required | +The Id of a media source | +