Каркас Laravel-приложения: Breeze+Sanctum, схема БД, модели, Docker
Laravel 13 в laravel-app/, Breeze (Blade) для сессионного логина + Sanctum для API-токенов (задел под Vue/Flutter на этапе 3). Схема Postgres перенесена из сырого SQL в Laravel-миграции (владелец схемы теперь Laravel, Go-сервисы как читали/писали эти таблицы, так и продолжают). Роли/категории/ операторы условий — закрытые PHP-enum'ы (UserRole, DeviceCategory, ConditionOperator); action_type правил намеренно остаётся строкой — открытый, управляемый данными список, а не код. Сидер device_types с growbox-типами как пример данных, не бизнес-логика. Docker: один контейнер (php-fpm + nginx через supervisord, multi-stage сборка vendor/assets). Образ пока не --no-dev — сидер использует fake() для демо-аккаунта. Проверено: миграции применяются на реальном Postgres (в докере и локально), дефолтные тесты Breeze проходят (25/25), Breeze-логин подтверждён вживую через браузер, Sanctum корректно отдаёт 401 на защищённый /api/user без токена.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\ConditionOperator;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable([
|
||||
'zone_id',
|
||||
'target_device_id',
|
||||
'condition_source_device_id',
|
||||
'condition_sensor_type',
|
||||
'condition_operator',
|
||||
'condition_value',
|
||||
'action_type',
|
||||
'action_params',
|
||||
'is_active',
|
||||
])]
|
||||
class AutomationRule extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'condition_operator' => ConditionOperator::class,
|
||||
'condition_value' => 'float',
|
||||
'action_params' => 'array',
|
||||
'is_active' => 'boolean',
|
||||
'created_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function zone(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Zone::class);
|
||||
}
|
||||
|
||||
public function targetDevice(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Device::class, 'target_device_id');
|
||||
}
|
||||
|
||||
public function conditionSourceDevice(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Device::class, 'condition_source_device_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable(['zone_id', 'device_type_id', 'name', 'external_id', 'protocol', 'status'])]
|
||||
class Device extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['created_at' => 'datetime'];
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function zone(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Zone::class);
|
||||
}
|
||||
|
||||
public function deviceType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(DeviceType::class);
|
||||
}
|
||||
|
||||
/** Rules that this device is the target action of. */
|
||||
public function targetedRules(): HasMany
|
||||
{
|
||||
return $this->hasMany(AutomationRule::class, 'target_device_id');
|
||||
}
|
||||
|
||||
/** Rules that watch this device's readings. */
|
||||
public function watchingRules(): HasMany
|
||||
{
|
||||
return $this->hasMany(AutomationRule::class, 'condition_source_device_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\DeviceCategory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable(['code', 'category', 'capabilities'])]
|
||||
class DeviceType extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'category' => DeviceCategory::class,
|
||||
'capabilities' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function devices(): HasMany
|
||||
{
|
||||
return $this->hasMany(Device::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use App\Enums\UserRole;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
#[Fillable(['name', 'email', 'password', 'role'])]
|
||||
#[Hidden(['password', 'remember_token'])]
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasApiTokens, HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
'role' => UserRole::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function isOwner(): bool
|
||||
{
|
||||
return $this->role === UserRole::Owner;
|
||||
}
|
||||
|
||||
public function zones()
|
||||
{
|
||||
return $this->hasMany(Zone::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable(['name', 'description'])]
|
||||
class Zone extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['created_at' => 'datetime'];
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function devices(): HasMany
|
||||
{
|
||||
return $this->hasMany(Device::class);
|
||||
}
|
||||
|
||||
public function automationRules(): HasMany
|
||||
{
|
||||
return $this->hasMany(AutomationRule::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user