Skip to content

Commit

Permalink
add video section to admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Silent-Watcher committed Aug 6, 2023
1 parent 8d0c195 commit 85ee8ac
Show file tree
Hide file tree
Showing 3,149 changed files with 745 additions and 46,789 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function uploadVideos($file)
$videoPath = "/upload/videos/{$year}/{$month}/";
$filename = $file->getClientOriginalName();

$file->move(public_path($videoPath), $filename);
$file-> move(public_path($videoPath), $filename);
$file = $videoPath . $filename;
return $file;
}
Expand Down
106 changes: 106 additions & 0 deletions app/Http/Controllers/Admin/EpisodeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Episode;
use App\Http\Requests\EpisodeRequest;
use App\Http\Controllers\Admin\AdminController;
use Illuminate\Http\Request;

class EpisodeController extends AdminController
{
public function __construct(){
$this->middleware('can:create_episode')->only(['create']);
$this->middleware('can:delete_episode')->only(['destroy']);
$this->middleware('can:edit_episode')->only(['edit','update']);
$this->middleware('can:show_episodes')->only(['index']);
}

/**
* Display a listing of the resource.
*/
public function index()
{
$episodes = Episode::query();
if($keyword = request('search')){
$episodes-> where('title','like',"%$keyword%")->orWhere('body','like',"%$keyword%")
->orWhere('id',$keyword);
}

$episodes = $episodes->latest()->paginate(100);
return view('admin.episodes.index',compact('episodes'));
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('admin.episodes.create');
}

/**
* Store a newly created resource in storage.
*/
public function store(EpisodeRequest $request)
{
$videoUrl = $this->uploadVideos($request->file('videos'));
if ($videoUrl) {
$episodes = Episode::create(array_merge($request->all(), ['videos' => $videoUrl]));
} else {
$episodes = Episode::create($request->all());
}

$this->setCourseTime($episodes);

return redirect(url('/admin/episodes'));
}

/**
* Display the specified resource.
*/
public function show(Episode $episode)
{
//
}

/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$episode = Episode::find($id);
return view('admin.episodes.edit', compact('episode'));
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
$file = $request->file('videos');
$episode = Episode::find($id);

$inputs = $request->all();
if ($file) {
$inputs['videos'] = $this->uploadVideos($request->file('videos'));
}

$episode->update($inputs);
$this->setCourseTime($episode);

return redirect(route('admin.episodes.index'));
}

/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$episode = Episode::find($id);
$episode-> delete();

return redirect(route('admin.episodes.index'));
}
}
36 changes: 36 additions & 0 deletions app/Http/Requests/EpisodeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class EpisodeRequest 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 [
'title'=>'required',
'type'=>'required',
'body'=>'required',
'slug'=>'required',
'number'=>'required',
'time'=>'required',
'tags'=>'required',
];
}
}
31 changes: 31 additions & 0 deletions app/Models/Course.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Episode;

class Course extends Model
{
Expand All @@ -12,4 +13,34 @@ class Course extends Model
protected $casts = [
'images' => 'array'
];


public function episode()
{
return $this->hasMany(Episode::class);
}


public function download()
{
if (! auth()->check()) return '#';
$status = false;
switch ($this->type){
case 'free' :
$status = true;
break;
case 'vip' :
if (auth()->user()->isActive()) $status = true;
break;
case 'cash' :
if (auth()->user()->checkLearning($this->course)) $status = true;
break;
}

$timestamp = Carbon::now()->addHour(5)->timestamp;
$hash = Hash::make('t@d@d@t@sj45n43js43dn#*d'.$this->id.request()->ip().$timestamp);

return $status ? "/download/$this->id?mac=$hash&t=$timestamp" : "#";

}
}
43 changes: 43 additions & 0 deletions app/Models/Episode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Episode extends Model
{
protected $fillable = ['course_id','title', 'type', 'slug', 'body', 'videoUrl', 'tags', 'download_count', 'videos', 'time', 'view_count', 'comment_count', 'number'];

public function course()
{
return $this->belongsTo(Course::class);
}

protected $casts = [
'videos'=>'array'
];

public function download()
{
if (! auth()->check()) return '#';
$status = false;
switch ($this->type){
case 'free' :
$status = true;
break;
case 'vip' :
if (auth()->user()->isActive()) $status = true;
break;
case 'cash' :
if (auth()->user()->checkLearning($this->course)) $status = true;
break;
}

$timestamp = Carbon::now()->addHour(5)->timestamp;
$hash = Hash::make('t@d@d@t@sj45n43js43dn#*d'.$this->id.request()->ip().$timestamp);

return $status ? "/download/$this->id?mac=$hash&t=$timestamp" : "#";

}
}
4 changes: 4 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use App\Models\Permission;
use App\Models\Role;
use App\Models\Course;
use App\Models\Episode;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -97,5 +98,8 @@ public function hasPermission($permission){
public function course(){
return $this->hasMany(Course::class);
}
public function episode(){
return $this->hasMany(Episode::class);
}

}
41 changes: 41 additions & 0 deletions database/migrations/2023_08_05_004417_create_episodes_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('episodes', function (Blueprint $table) {
$table->id();
$table->bigInteger('course_id')->unsigned()-> index()-> nullable();
$table->foreign('course_id')->references('id')->on('courses')->onUpdate('cascade');
$table->string('title');
$table->string('type',10);
$table->string('slug');
$table->text('body');
$table->text('videos')->nullable();
$table->text('videoUrl')->nullable();
$table->string('tags');
$table->string('time',15)->default('00:00:00');
$table->integer('number');
$table->string('view_count')->default(0);
$table->string('comment_count')->default(0);
$table->string('download_count')->default(0);
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('episodes');
}
};
1 change: 1 addition & 0 deletions public/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

</IfModule>
Loading

0 comments on commit 85ee8ac

Please sign in to comment.