Skip to content

Commit

Permalink
Add validation layer for storing channel and depend on link not ident…
Browse files Browse the repository at this point in the history
…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
AhmedHelalAhmed committed Sep 4, 2021
1 parent 2e2f03d commit f8e7262
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 266 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
QUEUE_CONNECTION=database
SESSION_DRIVER=database
SESSION_LIFETIME=120

Expand Down Expand Up @@ -53,4 +53,6 @@ YOUTUBE_BASE_URL_API="https://www.googleapis.com/youtube/v3/"
# To get API key for Youtube https://developers.google.com/youtube/v3/getting-started
YOUTUBE_API_KEY=
YOUTUBE_CHANNEL_URL_API="${YOUTUBE_BASE_URL_API}channels"
YOUTUBE_CHANNEL_videos_URL_API="${YOUTUBE_BASE_URL_API}playlistItems"
YOUTUBE_VIDEO_BASE_LINK="https://www.youtube.com/watch?v="
YOUTUBE_CHANNEL_SEARCH_API="${YOUTUBE_BASE_URL_API}search"
10 changes: 8 additions & 2 deletions app/Http/Controllers/IndexingChannelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ class IndexingChannelController
{
public function __invoke()
{

return inertia()->render('Channels/index', ['channels' => Channel::all()]);
return inertia()->render(
'Channels/index',
[
'channels' => Channel::where('user_id', auth()->id())
->withCount('videos')
->paginate()
]
);
}
}
6 changes: 3 additions & 3 deletions app/Http/Controllers/StoringChannelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
namespace App\Http\Controllers;


use App\Http\Requests\StoreChannelRequest;
use App\Models\Channel;
use Illuminate\Support\Arr;

class StoringChannelController
{
public function __invoke()
public function __invoke(StoreChannelRequest $request)
{
Channel::create(array_merge(request()->only(['name', 'remote_identifier']),['user_id'=>auth()->id()]));
Channel::create($request->validated());

return back();
}
Expand Down
56 changes: 56 additions & 0 deletions app/Http/Requests/StoreChannelRequest.php
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'
];
}
}
28 changes: 28 additions & 0 deletions app/Services/GettingChannelIdByLinkService.php
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');
}
}
1 change: 1 addition & 0 deletions config/youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'channel' => env('YOUTUBE_CHANNEL_URL_API'),
'videos' => env('YOUTUBE_CHANNEL_videos_URL_API'),
'video_base_link' => env('YOUTUBE_VIDEO_BASE_LINK'),
'search' => env('YOUTUBE_CHANNEL_SEARCH_API'),
],


Expand Down
Loading

0 comments on commit f8e7262

Please sign in to comment.