Skip to content

Commit 80a2b92

Browse files
committed
Code improvements and bug fixes.
1 parent e65ee36 commit 80a2b92

File tree

19 files changed

+1519
-2524
lines changed

19 files changed

+1519
-2524
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "laradock"]
2+
path = laradock
3+
url = https://github.com/laradock/laradock.git

app/Category.php

-16
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,4 @@
77
class Category extends Model
88
{
99

10-
/**
11-
* Get all of the videos that are assigned this category.
12-
*/
13-
public function videos()
14-
{
15-
return $this->morphedByMany('App\Video', 'categorizable');
16-
}
17-
18-
/**
19-
* Get the parent of the category.
20-
*/
21-
public function parentable()
22-
{
23-
return $this->morphedByMany();
24-
}
25-
2610
}

app/Console/Commands/setVideoCategories.php

-87
This file was deleted.

app/Http/Controllers/VideoController.php

+22-16
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,31 @@
33
namespace App\Http\Controllers;
44

55
use Illuminate\Http\Request;
6+
use Illuminate\Http\JsonResponse;
67
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
79

810
use Config;
9-
use App\Video;
11+
use App\Media;
1012
use App\QueryRule;
1113
use App\CategoryRule;
1214

1315
class VideoController extends Controller
1416
{
1517
/**
16-
* API Resource for Video Model
18+
* API Resource for Media Model
1719
*
18-
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
20+
* @param Request $request
21+
* @return LengthAwarePaginator
1922
*/
2023
public function index(Request $request)
2124
{
2225
// Limit
2326
$limit = $request->has('limit') ? (int) $request->input('limit') : 50;
2427

2528
// Sorting
26-
if ($request->has('sortby')) {
27-
switch ($request->input('sortby')) {
29+
if ($request->has('sort_by')) {
30+
switch ($request->input('sort_by')) {
2831
case 'most_views':
2932
$sort = 'views';
3033
break;
@@ -39,7 +42,7 @@ public function index(Request $request)
3942

4043
// Model by ID
4144
if ($request->has('id')) {
42-
$data = Video::where('id', $request->input('id'))->firstOrFail();
45+
$data = Media::where('id', $request->input('id'))->firstOrFail();
4346

4447
// TODO: Transform data at insert.
4548
preg_match(config('regex.domain'), $data->embed, $url);
@@ -54,20 +57,20 @@ public function index(Request $request)
5457
// Collection of models
5558
if ($request->has('collection')) {
5659
$collection = explode(',', $request->input('collection'));
57-
return Video::whereIn('id', $collection)->get();
60+
return Media::whereIn('id', $collection)->get();
5861
}
5962

6063
// Category model listing
6164
if ($request->has('category')) {
62-
return Video::search($request->input('category'))
65+
return Media::search($request->input('category'))
6366
->rule(CategoryRule::class)
6467
->orderBy('views', 'DESC')
6568
->paginate($limit);
6669
}
6770

6871
// Search model listing
6972
if ($request->has('q') && !empty($request->input('q'))) {
70-
$data = Video::search($request->input('q'))->rule(QueryRule::class);
73+
$data = Media::search($request->input('q'))->rule(QueryRule::class);
7174
$data->whereNotMatch('categories', config('const.excluded_cats'));
7275

7376
if ($request->has('exclude')) {
@@ -83,24 +86,26 @@ public function index(Request $request)
8386

8487
// Most Views
8588
if ($request->has('most_viewed')) {
86-
return Video::search('*')
89+
return Media::search('*')
8790
->orderBy('views', 'desc')
8891
->paginate($limit);
8992
}
9093

9194
// Default model listing
92-
return Video::search('*')
95+
return Media::search('*')
9396
->whereNotMatch('categories', config('const.excluded_cats'))
9497
->orderby('views', 'desc')
9598
->paginate($limit);
9699
}
97100

98101
/**
99102
* Return top models by week
103+
*
104+
* @return LengthAwarePaginator
100105
*/
101106
public function best()
102107
{
103-
return Video::search('*')
108+
return Media::search('*')
104109
->where(
105110
'created_at',
106111
'>=',
@@ -112,23 +117,24 @@ public function best()
112117
/**
113118
* Increment likes column on respective model
114119
*
115-
* @return json
120+
* @return JsonResponse
116121
*/
117122
public function like($id)
118123
{
119-
if (Video::where('id', $id)->increment('likes')) {
124+
if (Media::where('id', $id)->increment('likes')) {
120125
return response()->json(['success' => 1], 200);
121126
}
122127
}
123128

124129
/**
125130
* Increment dislikes column on respective model
126131
*
127-
* @return json
132+
* @return JsonResponse
133+
* @mixin Builder
128134
*/
129135
public function dislike($id)
130136
{
131-
if (Video::where('id', $id)->increment('dislikes')) {
137+
if (Media::where('id', $id)->increment('dislikes')) {
132138
return response()->json(['success' => 1], 200);
133139
}
134140
}

app/Http/Resources/DataCollection.php

-20
This file was deleted.

app/Video.php app/Media.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// use Laravel\Scout\Searchable;
1010

11-
class Video extends Model
11+
class Media extends Model
1212
{
1313
use Searchable;
1414

@@ -27,14 +27,6 @@ class Video extends Model
2727
],
2828
];
2929

30-
/**
31-
* Get the categories for the video.
32-
*/
33-
// public function categories()
34-
// {
35-
// return $this->morphToMany('App\Category', 'categorizable');
36-
// }
37-
3830
/**
3931
* Get the index name for the model.
4032
*
@@ -46,13 +38,13 @@ public function searchableAs()
4638
}
4739

4840
/**
49-
* Get the indexable data array for the model.
41+
* Get the index data array for the model.
5042
*
5143
* @return array
5244
*/
5345
public function toSearchableArray()
5446
{
55-
$array = $this->only([
47+
return $this->only([
5648
'title',
5749
'categories',
5850
'views',
@@ -63,7 +55,5 @@ public function toSearchableArray()
6355
'created_at',
6456
'updated_at',
6557
]);
66-
67-
return $array;
6858
}
6959
}

app/VideosIndex.php app/MediaIndex.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use ScoutElastic\IndexConfigurator;
66
use ScoutElastic\Migratable;
77

8-
class VideosIndex extends IndexConfigurator
8+
class MediaIndex extends IndexConfigurator
99
{
1010
use Migratable;
1111

12-
// Index name
13-
protected $name = 'videos_idx';
12+
// Index name for Elastic search index
13+
protected $name = 'media_idx';
1414

1515
/**
1616
* Elastic Search settings

database/migrations/2020_04_13_073834_videos.php database/migrations/2020_04_13_073834_media.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class VideoData extends Migration
7+
class Media extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -14,7 +14,7 @@ class VideoData extends Migration
1414
public function up()
1515
{
1616

17-
Schema::create('videos', function (Blueprint $table) {
17+
Schema::create('media', function (Blueprint $table) {
1818

1919
$table->id();
2020
$table->text('embed');
@@ -40,6 +40,6 @@ public function up()
4040
*/
4141
public function down()
4242
{
43-
Schema::dropIfExists('videos');
43+
Schema::dropIfExists('media');
4444
}
4545
}

database/migrations/2020_04_21_134200_categorizables.php

-35
This file was deleted.

laradock

Submodule laradock added at 89c9cfe

0 commit comments

Comments
 (0)