mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-15 21:41:03 +08:00
improve migration file + seedbox management
This commit is contained in:
@@ -46,6 +46,8 @@ use Nexus\Imdb\Imdb;
|
||||
use NexusPlugin\PostLike\PostLikeRepository;
|
||||
use NexusPlugin\StickyPromotion\Models\StickyPromotion;
|
||||
use NexusPlugin\StickyPromotion\Models\StickyPromotionParticipator;
|
||||
use PhpIP\IP;
|
||||
use PhpIP\IPBlock;
|
||||
use Rhilip\Bencode\Bencode;
|
||||
|
||||
class Test extends Command
|
||||
@@ -81,11 +83,10 @@ class Test extends Command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$r = Str::of(class_basename(\App\Models\AgentAllow::class))
|
||||
->plural()
|
||||
->kebab()
|
||||
->slug();
|
||||
|
||||
$ip = '116.30.133.129';
|
||||
// $ip = '240e:3a1:680c:bb11:211:32ff:fe2c:a603';
|
||||
$ipObj = IPBlock::create($ip);
|
||||
$r = $ipObj->getFirstIp();
|
||||
dd($r);
|
||||
}
|
||||
|
||||
|
||||
87
app/Filament/Resources/System/SeedBoxRecordResource.php
Normal file
87
app/Filament/Resources/System/SeedBoxRecordResource.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\System;
|
||||
|
||||
use App\Filament\Resources\System\SeedBoxRecordResource\Pages;
|
||||
use App\Filament\Resources\System\SeedBoxRecordResource\RelationManagers;
|
||||
use App\Models\SeedBoxRecord;
|
||||
use Filament\Forms;
|
||||
use Filament\Resources\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Resources\Table;
|
||||
use Filament\Tables;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
|
||||
class SeedBoxRecordResource extends Resource
|
||||
{
|
||||
protected static ?string $model = SeedBoxRecord::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-archive';
|
||||
|
||||
protected static ?string $navigationGroup = 'System';
|
||||
|
||||
protected static ?int $navigationSort = 98;
|
||||
|
||||
protected static function getNavigationLabel(): string
|
||||
{
|
||||
return __('admin.sidebar.seedbox_records');
|
||||
}
|
||||
|
||||
public static function getBreadcrumb(): string
|
||||
{
|
||||
return self::getNavigationLabel();
|
||||
}
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('operator')->label(__('label.seedbox_record.operator')),
|
||||
Forms\Components\TextInput::make('bandwidth')->label(__('label.seedbox_record.bandwidth'))->integer(),
|
||||
Forms\Components\TextInput::make('ip_begin')->label(__('label.seedbox_record.ip_begin')),
|
||||
Forms\Components\TextInput::make('ip_end')->label(__('label.seedbox_record.ip_end')),
|
||||
Forms\Components\TextInput::make('ip')->label(__('label.seedbox_record.ip'))->helperText(__('label.seedbox_record.ip_help')),
|
||||
Forms\Components\Textarea::make('comment')->label(__('label.comment')),
|
||||
])->columns(1);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('id'),
|
||||
Tables\Columns\TextColumn::make('typeText')->label(__('label.seedbox_record.type')),
|
||||
Tables\Columns\TextColumn::make('user.username')->label(__('label.username')),
|
||||
Tables\Columns\TextColumn::make('operation')->label(__('label.seedbox_record.operator')),
|
||||
Tables\Columns\TextColumn::make('bandwidth')->label(__('label.seedbox_record.bandwidth')),
|
||||
Tables\Columns\TextColumn::make('ip')->label(__('label.seedbox_record.ip'))->formatStateUsing(fn ($record) => $record->ip ?: sprintf('%s ~ %s', $record->ip_begin, $record->ip_end)),
|
||||
Tables\Columns\TextColumn::make('comment')->label(__('label.comment')),
|
||||
])
|
||||
->filters([
|
||||
//
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListSeedBoxRecords::route('/'),
|
||||
'create' => Pages\CreateSeedBoxRecord::route('/create'),
|
||||
'edit' => Pages\EditSeedBoxRecord::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\System\SeedBoxRecordResource\Pages;
|
||||
|
||||
use App\Filament\Resources\System\SeedBoxRecordResource;
|
||||
use App\Models\SeedBoxRecord;
|
||||
use App\Repositories\SeedBoxRepository;
|
||||
use Filament\Pages\Actions;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class CreateSeedBoxRecord extends CreateRecord
|
||||
{
|
||||
protected static string $resource = SeedBoxRecordResource::class;
|
||||
|
||||
protected function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
$data['uid'] = auth()->id();
|
||||
$data['type'] = SeedBoxRecord::TYPE_ADMIN;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function handleRecordCreation(array $data): Model
|
||||
{
|
||||
$seedBoxRep = new SeedBoxRepository();
|
||||
try {
|
||||
return $seedBoxRep->store($data);
|
||||
} catch (\Exception $exception) {
|
||||
//this wont work...
|
||||
$this->notify('danger', $exception->getMessage());
|
||||
die();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\System\SeedBoxRecordResource\Pages;
|
||||
|
||||
use App\Filament\Resources\System\SeedBoxRecordResource;
|
||||
use Filament\Pages\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditSeedBoxRecord extends EditRecord
|
||||
{
|
||||
protected static string $resource = SeedBoxRecordResource::class;
|
||||
|
||||
protected function getActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\System\SeedBoxRecordResource\Pages;
|
||||
|
||||
use App\Filament\PageList;
|
||||
use App\Filament\Resources\System\SeedBoxRecordResource;
|
||||
use Filament\Pages\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListSeedBoxRecords extends PageList
|
||||
{
|
||||
protected static string $resource = SeedBoxRecordResource::class;
|
||||
|
||||
protected function getActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
31
app/Models/SeedBoxRecord.php
Normal file
31
app/Models/SeedBoxRecord.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
|
||||
class SeedBoxRecord extends NexusModel
|
||||
{
|
||||
protected $table = 'seedbox_records';
|
||||
|
||||
protected $fillable = ['type', 'uid', 'operator', 'bandwidth', 'ip', 'ip_begin', 'ip_end', 'ip_begin_numeric', 'ip_end_numeric', 'comment'];
|
||||
|
||||
public $timestamps = true;
|
||||
|
||||
const TYPE_USER = 1;
|
||||
const TYPE_ADMIN = 2;
|
||||
|
||||
protected function typeText(): Attribute
|
||||
{
|
||||
return new Attribute(
|
||||
get: fn($value, $attributes) => __("seedbox.type_text." . $attributes['type'])
|
||||
);
|
||||
}
|
||||
|
||||
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'uid');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
66
app/Repositories/SeedBoxRepository.php
Normal file
66
app/Repositories/SeedBoxRepository.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\Poll;
|
||||
use App\Models\SeedBoxRecord;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use PhpIP\IP;
|
||||
use PhpIP\IPBlock;
|
||||
|
||||
class SeedBoxRepository extends BaseRepository
|
||||
{
|
||||
public function getList(array $params)
|
||||
{
|
||||
$query = Poll::query();
|
||||
list($sortField, $sortType) = $this->getSortFieldAndType($params);
|
||||
$query->orderBy($sortField, $sortType);
|
||||
return $query->paginate();
|
||||
}
|
||||
|
||||
public function store(array $params)
|
||||
{
|
||||
if (!empty($params['ip']) && empty($params['ip_begin']) && empty($params['ip_end'])) {
|
||||
if (str_contains($params['ip'], '/')) {
|
||||
$ipBlock = IPBlock::create($params['ip']);
|
||||
$params['ip_begin_numeric'] = $ipBlock->getFirstIp()->numeric();
|
||||
$params['ip_end_numeric'] = $ipBlock->getLastIp()->numeric();
|
||||
} else {
|
||||
$ip = IP::create($params['ip']);
|
||||
$params['ip_begin_numeric'] = $ip->numeric();
|
||||
$params['ip_end_numeric'] = $ip->numeric();
|
||||
}
|
||||
} elseif (empty($params['ip']) && !empty($params['ip_begin']) && !empty($params['ip_end'])) {
|
||||
$ipBegin = IP::create($params['ip_begin']);
|
||||
$params['ip_begin_numeric'] = $ipBegin->numeric();
|
||||
|
||||
$ipEnd = IP::create($params['ip_end']);
|
||||
$params['ip_end_numeric'] = $ipEnd->numeric();
|
||||
} else {
|
||||
throw new \InvalidArgumentException("Require ip or ip_begin + ip_end");
|
||||
}
|
||||
|
||||
return SeedBoxRecord::query()->create($params);
|
||||
}
|
||||
|
||||
public function update(array $params, $id)
|
||||
{
|
||||
$model = Poll::query()->findOrFail($id);
|
||||
$model->update($params);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function getDetail($id)
|
||||
{
|
||||
$model = Poll::query()->findOrFail($id);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function delete($id, $uid)
|
||||
{
|
||||
return SeedBoxRecord::query()->whereIn('id', Arr::wrap($id))->where('uid', $uid)->delete();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user