| @@ -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(); | |||||
| } | |||||
| } | |||||
| @@ -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(), | |||||
| ]; | |||||
| } | |||||
| } | |||||
| @@ -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; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,14 @@ | |||||
| <?php | |||||
| namespace App\Logic\Contract; | |||||
| class CreateManager extends ContractManager | |||||
| { | |||||
| public function create(): array | |||||
| { | |||||
| $messages = []; | |||||
| $this->save(); | |||||
| return $messages; | |||||
| } | |||||
| } | |||||