From 62da52adc0fff270b5445c5633bab9084f698213 Mon Sep 17 00:00:00 2001 From: iv Date: Tue, 25 Mar 2025 23:36:53 +0300 Subject: [PATCH] feat: add threshold controller --- controllers/AccountThresholdController.php | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 controllers/AccountThresholdController.php diff --git a/controllers/AccountThresholdController.php b/controllers/AccountThresholdController.php new file mode 100644 index 0000000..0eb2f6f --- /dev/null +++ b/controllers/AccountThresholdController.php @@ -0,0 +1,91 @@ +findAccount($account_id); + return $this->render('index', [ + 'account' => $account, + ]); + } + + public function actionCreate($account_id) + { + $account = $this->findAccount($account_id); + $model = new AccountThreshold(['account_id' => $account->id]); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + $account->refresh(); + $account->checkAndSendNotifications(); + Yii::$app->session->setFlash('success', 'Порог добавлен'); + return $this->redirect(['index', 'account_id' => $account->id]); + } + + return $this->render('create', ['model' => $model]); + } + + public function actionUpdate($account_id, $id) + { + $account = $this->findAccount($account_id); + $threshold = $this->findThreshold($id); + + if ($threshold->account_id != $account->id) { + throw new NotFoundHttpException('Данный порог не принадлежит аккаунту'); + } + + if ($threshold->load(Yii::$app->request->post()) && $threshold->save()) { + $account->refresh(); + $account->checkAndSendNotifications(); + Yii::$app->session->setFlash('success', 'Порог обновлен'); + return $this->redirect(['index', 'account_id' => $account->id]); + } + + return $this->render('update', ['model' => $threshold]); + } + + public function actionDelete($account_id, $id) + { + $account = $this->findAccount($account_id); + $threshold = $this->findThreshold($id); + + if ($threshold->account_id != $account->id) { + throw new NotFoundHttpException('Данный порог не принадлежит аккаунту'); + } + + $threshold->delete(); + $account->refresh(); + $account->checkAndSendNotifications(); + + Yii::$app->session->setFlash('success', 'Порог удален'); + return $this->redirect(['index', 'account_id' => $account->id]); + } + + // Вспомогательные методы + protected function findAccount($id) + { + if (($model = Account::findOne($id)) !== null) { + return $model; + } + throw new NotFoundHttpException('Аккаунт не найден'); + } + + protected function findThreshold($id) + { + if (($model = AccountThreshold::findOne($id)) !== null) { + return $model; + } + throw new NotFoundHttpException('Порог не найден'); + } +} \ No newline at end of file