diff --git a/README.md b/README.md index 4eecf00..556d54a 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ class TopicController extends Controller return [ 'do' => [ 'class' => ActionAction::className(), + 'pairsType' => ['want','own'], + 'counterType' => ['apply'], ] ]; } @@ -263,7 +265,7 @@ class ActionStoreSearch extends ActionStore get user model_id count ```php -ActionStore::resetCounter( +ActionStore::getCounter( ActionStoreSearch::FAVORITE_TYPE, ['model' => Company::tableName(), 'model_id' => $company->id, 'user_id' => \Yii::$app->user->id] ); @@ -272,7 +274,7 @@ ActionStore::resetCounter( get all model_id count ```php -ActionStore::resetCounter( +ActionStore::getCounter( ActionStoreSearch::FAVORITE_TYPE, ['model' => Company::tableName(), 'model_id' => $company->id] ); diff --git a/src/actions/ActionAction.php b/src/actions/ActionAction.php index 4fe44fc..c19e632 100644 --- a/src/actions/ActionAction.php +++ b/src/actions/ActionAction.php @@ -8,6 +8,7 @@ namespace yiier\actionStore\actions; use Yii; +use yii\db\Exception; use yii\helpers\Json; use yii\web\Response; use yiier\actionStore\models\ActionStore; @@ -19,6 +20,16 @@ class ActionAction extends \yii\base\Action */ public $actionClass = '\yiier\actionStore\models\ActionStore'; + /** + * @var string Appear in pairs, only one will be recorded + */ + public $pairsType = ['like', 'dislike']; + + /** + * @var array counter type no delete + */ + public $counterType = ['clap', 'view']; + public function init() { parent::init(); @@ -33,11 +44,55 @@ public function run() Yii::$app->response->format = Response::FORMAT_JSON; /** @var ActionStore $model */ $model = Yii::createObject($this->actionClass); + $model-> $model->load(array_merge(Yii::$app->request->getQueryParams(), ['user_id' => Yii::$app->user->id]), ''); + if ($model->validate()) { - return ['code' => 200, 'data' => ActionStore::createUpdateAction($model), 'message' => 'success']; + return ['code' => 200, 'data' => $this->createUpdateAction($model), 'message' => 'success']; } return ['code' => 500, 'data' => '', 'message' => Json::encode($model->errors)]; } } + + + /** + * @param $model ActionStore + * @return int + * @throws Exception + * @throws \Throwable + */ + protected function createUpdateAction($model) + { + $conditions = array_filter($model->attributes); + $pairsType0 = $this->pairsType[0]; + $pairsType1 = $this->pairsType[1]; + switch ($model->type) { + case $pairsType0: + $model::find()->where(array_merge(['type' => $pairsType1], $conditions))->one()->delete(); + $data = array_merge(['type' => $pairsType0], $conditions); + break; + case $pairsType1: + $model::find()->where(array_merge(['type' => $pairsType0], $conditions))->one()->delete(); + $data = array_merge(['type' => $pairsType1], $conditions); + break; + + default: + $data = array_merge(['type' => $model->type], $conditions); + break; + } + if ($didModel = $model::find()->filterWhere($data)->one()) { + if (!in_array($didModel->type, [$this->counterType])) { + return $didModel->delete(); + } else { + $model = $didModel; + $data['value'] = $didModel->value + 1; + } + } + $model->setAttributes($data); + if ($model->save()) { + unset($conditions['id'], $conditions['created_at'], $conditions['updated_at'], $conditions['value']); + return $model::getCounter($model->type, $conditions); + } + throw new Exception(json_encode($model->errors)); + } } \ No newline at end of file diff --git a/src/models/ActionStore.php b/src/models/ActionStore.php index 12ef9c5..3fd280e 100644 --- a/src/models/ActionStore.php +++ b/src/models/ActionStore.php @@ -4,7 +4,6 @@ use Yii; use yii\behaviors\TimestampBehavior; -use yii\db\Exception; /** * This is the model class for table "{{%action_store}}". @@ -21,26 +20,6 @@ */ class ActionStore extends \yii\db\ActiveRecord { - /** - * @var string - */ - const LIKE_TYPE = 'like'; - - /** - * @var string - */ - const DISLIKE_TYPE = 'dislike'; - - /** - * @var string - */ - const CLAP_TYPE = 'clap'; - - /** - * @var string - */ - const VIEW_TYPE = 'view'; - /** * @inheritdoc */ @@ -91,51 +70,12 @@ public function attributeLabels() /** - * @param $model ActionStore - * @return int - * @throws Exception - */ - public static function createUpdateAction($model) - { - $conditions = array_filter($model->attributes); - switch ($model->type) { - case self::LIKE_TYPE: - self::findOne(array_merge(['type' => self::DISLIKE_TYPE], $conditions))->delete(); - $data = array_merge(['type' => self::LIKE_TYPE], $conditions); - break; - case self::DISLIKE_TYPE: - self::findOne(array_merge(['type' => self::LIKE_TYPE], $conditions))->delete(); - $data = array_merge(['type' => self::DISLIKE_TYPE], $conditions); - break; - - default: - $data = array_merge(['type' => $model->type], $conditions); - break; - } - if ($didModel = $model::find()->filterWhere($data)->one()) { - if (!in_array($didModel->type, [self::CLAP_TYPE, self::VIEW_TYPE])) { - return $didModel->delete(); - } else { - $model = $didModel; - $data['value'] = $didModel->value + 1; - } - } - $model->setAttributes($data); - if ($model->save()) { - unset($conditions['id'], $conditions['created_at'], $conditions['updated_at'], $conditions['value']); - return self::resetCounter($model->type, $conditions); - } - throw new Exception(json_encode($model->errors)); - } - - - /** - * 返回计数器 + * 获取计数器 * @param $type * @param $conditions * @return int */ - public static function resetCounter($type, $conditions) + public static function getCounter($type, $conditions) { return (int)self::find() ->filterWhere(array_merge(['type' => $type], $conditions))