86 lines
2.5 KiB
PHP
86 lines
2.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Lucent\Http\Controller;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Lucent\Account\AccountService;
|
||
|
|
use Lucent\Account\AuthService;
|
||
|
|
use Lucent\Core\Repository\FieldRepo;
|
||
|
|
use Lucent\Core\Repository\SchemaRepo;
|
||
|
|
use Lucent\Core\Schema\Data\Field;
|
||
|
|
use Lucent\Core\Schema\Data\FieldProp\FieldProp;
|
||
|
|
use Lucent\Core\Schema\Data\Schema;
|
||
|
|
use Lucent\Id\Id;
|
||
|
|
use Lucent\Svelte\Svelte;
|
||
|
|
use Illuminate\Support\Facades\Validator;
|
||
|
|
|
||
|
|
class FieldController extends Controller
|
||
|
|
{
|
||
|
|
public function __construct(private readonly Svelte $svelte) {}
|
||
|
|
|
||
|
|
public function create(Request $request)
|
||
|
|
{
|
||
|
|
$schemaId = $request->input("schema");
|
||
|
|
$fieldType = $request->input("type");
|
||
|
|
|
||
|
|
$schemas = SchemaRepo::all();
|
||
|
|
$schema = collect($schemas)->firstWhere("id", $schemaId);
|
||
|
|
|
||
|
|
if (empty($schema)) {
|
||
|
|
return response()->json(["error" => "Schema not found"], 404);
|
||
|
|
}
|
||
|
|
if (empty($fieldType)) {
|
||
|
|
return response()->json(["error" => "Field type not found"], 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
// $fieldProps = FieldProp::fromType($fieldType);
|
||
|
|
|
||
|
|
return $this->svelte->render(
|
||
|
|
view: "fieldCreate",
|
||
|
|
title: "Create Field",
|
||
|
|
data: [
|
||
|
|
"schemas" => $schemas,
|
||
|
|
"schema" => $schema,
|
||
|
|
// "fieldProps" => $fieldProps,
|
||
|
|
"type" => $fieldType,
|
||
|
|
],
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function postCreate(Request $request)
|
||
|
|
{
|
||
|
|
$schemaId = $request->input("schemaId");
|
||
|
|
$name = $request->input("name");
|
||
|
|
$alias = $request->input("alias");
|
||
|
|
$fieldType = $request->input("fieldType");
|
||
|
|
|
||
|
|
$validator = Validator::make($request->all(), [
|
||
|
|
"name" => "required|string|max:30|min:2",
|
||
|
|
"alias" => "required|alpha_dash:ascii|max:30|min:2",
|
||
|
|
"schemaId" => "required",
|
||
|
|
"fieldType" => "required",
|
||
|
|
]);
|
||
|
|
if ($validator->fails()) {
|
||
|
|
return response()->json(["errors" => $validator->errors()], 422);
|
||
|
|
}
|
||
|
|
|
||
|
|
$schemas = SchemaRepo::all();
|
||
|
|
$schema = collect($schemas)->firstWhere("id", $schemaId);
|
||
|
|
|
||
|
|
$fieldProps = FieldProp::fromType($fieldType);
|
||
|
|
|
||
|
|
$field = new Field(
|
||
|
|
id: Id::new(),
|
||
|
|
schemaId: $schemaId,
|
||
|
|
name: $name,
|
||
|
|
alias: $alias,
|
||
|
|
type: $fieldType,
|
||
|
|
props: $fieldProps,
|
||
|
|
);
|
||
|
|
|
||
|
|
FieldRepo::insert($field);
|
||
|
|
return response()->json(["field" => $field], 201);
|
||
|
|
}
|
||
|
|
}
|