瀏覽代碼

契約新規作成機能 追加

develop
sosuke.iwabuchi 2 年之前
父節點
當前提交
57f1b02ae3
共有 4 個檔案被更改,包括 142 行新增0 行删除
  1. +45
    -0
      app/Http/Controllers/Web/Contract/CreateController.php
  2. +23
    -0
      app/Http/Controllers/Web/Contract/CreateParam.php
  3. +60
    -0
      app/Logic/Contract/ContractManager.php
  4. +14
    -0
      app/Logic/Contract/CreateManager.php

+ 45
- 0
app/Http/Controllers/Web/Contract/CreateController.php 查看文件

@@ -0,0 +1,45 @@
<?php

namespace App\Http\Controllers\Web\Contract;

use App\Codes\UserRole;
use App\Http\Controllers\Web\WebController;
use App\Logic\Contract\CreateManager;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateController extends WebController
{
public function name(): string
{
return "契約新規登録";
}

public function description(): string
{
return "契約を新規登録する";
}

public function __construct(
protected CreateParam $param,
private CreateManager $manager
) {
parent::__construct();
$this->roleAllow(UserRole::SUPER_ADMIN);
}

protected function run(Request $request): JsonResponse
{
$param = $this->param;

$messages = $this->manager->initForCreate()
->fill($param->toArray())
->create();

if (count($messages) !== 0) {
return $this->failedResponse([], $messages);
}

return $this->successResponse();
}
}

+ 23
- 0
app/Http/Controllers/Web/Contract/CreateParam.php 查看文件

@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers\Web\Contract;

use App\Http\Controllers\Web\BaseParam;
use App\Models\Contract;

/**
* @property string $name
* @property ?string $custom
*/

class CreateParam extends BaseParam
{
public function rules(): array
{
return
[
Contract::COL_NAME_NAME => $this->str(),
Contract::COL_NAME_CUSTOM => $this->str(),
];
}
}

+ 60
- 0
app/Logic/Contract/ContractManager.php 查看文件

@@ -0,0 +1,60 @@
<?php

namespace App\Logic\Contract;

use App\Models\Contract;
use LogicException;

abstract class ContractManager
{

// ------------MEMBER----
protected Contract $contract;
protected bool $initialized = false;


// -------------PUBLIC--------
public function initById(string $id): static
{
$contract = Contract::findOrFail($id);
$this->contract = $contract;
$this->initialized = true;
return $this;
}

public function initForCreate(): static
{
$contract = new Contract();
$contract->setId();

$this->contract = $contract;
$this->initialized = true;
return $this;
}

public function fill(array $attr): static
{
if (!$this->initialized) {
throw new LogicException("初期化ミス");
}

$this->contract->fill($attr);
return $this;
}

public function id(): string
{
if (!$this->initialized) {
throw new LogicException("初期化ミス");
}
return $this->contract->id;
}


// ----------------PROTECTED----------
protected function save(): static
{
$this->contract->save();
return $this;
}
}

+ 14
- 0
app/Logic/Contract/CreateManager.php 查看文件

@@ -0,0 +1,14 @@
<?php

namespace App\Logic\Contract;

class CreateManager extends ContractManager
{
public function create(): array
{
$messages = [];
$this->save();

return $messages;
}
}

Loading…
取消
儲存