-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation layer for storing channel and depend on link not ident…
…ifier - Add validation layer for storing channel and depend on link - Add pagination for channels - Redirect to channels index when click on dashboard - Enhance title of pages
- Loading branch information
1 parent
2e2f03d
commit f8e7262
Showing
12 changed files
with
253 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use App\Services\GettingChannelIdByLinkService; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class StoreChannelRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'name' => 'required', | ||
'link' => 'required|url', | ||
'remote_identifier' => 'required|unique:channels,remote_identifier', | ||
'user_id' => 'required' | ||
]; | ||
} | ||
|
||
public function prepareForValidation() | ||
{ | ||
$this->merge(['remote_identifier' => $this->link ? $this->getChannelIdFromLink($this->link) : null]); | ||
$this->merge(['user_id' => auth()->id()]); | ||
} | ||
|
||
private function getChannelIdFromLink(string $url): string | ||
{ | ||
$parsed = parse_url(rtrim($url, '/')); | ||
if (isset($parsed['path']) && preg_match('/^\/channel\/(([^\/])+?)$/', $parsed['path'], $matches)) { | ||
return $matches[1]; | ||
} | ||
return (new GettingChannelIdByLinkService)->execute($url); | ||
} | ||
|
||
public function messages() | ||
{ | ||
return [ | ||
'remote_identifier.unique' => 'Channel already exists' | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
|
||
namespace App\Services; | ||
|
||
|
||
use ArrayAccess; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class GettingChannelIdByLinkService | ||
{ | ||
/** | ||
* @param string $link | ||
* @return array|ArrayAccess|mixed | ||
*/ | ||
public function execute(string $link) | ||
{ | ||
$response = Http::get(config('youtube.urls.search'), [ | ||
'part' => 'snippet', | ||
'q' => $link, | ||
'type' => 'channel', | ||
'key' => config('youtube.key') | ||
])->json(); | ||
|
||
return Arr::get($response, 'items.0.snippet.channelId'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.