public function actionLike() { if (!\Yii::$app->getRequest()->getIsPost()) { return 'try agaen'; } if (\Yii::$app->getRequest()->getHeaders()->get('x-like') !== 'True') { return 'try agaen'; } $pictureId = \Yii::$app->getRequest()->post('p'); $hash = \Yii::$app->getRequest()->post('h'); $picture = Picture::find()->where(['id' => $pictureId])->one(); /* @var Picture $picture */ if ($picture == null) { return 'try agaen'; } if ($picture->getLikeHash() !== $hash) { return 'try agaen'; } $like = Like::find()->where(['ip' => \Yii::$app->getRequest()->getUserIP(), 'pictureId' => $picture->getPrimaryKey()])->count(); if ($like > 0) { return 'pls stap'; } $like = new Like(); $like->ip = \Yii::$app->getRequest()->getUserIP(); $like->pictureId = $picture->getPrimaryKey(); $like->save(); $picture->likeCount += 1; $picture->save(); return '+' . $picture->likeCount; }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $like = Like::find($id); if ($like) { if ($like->delete()) { return response(['message' => 'successfull']); } else { return $this->errorDelete(); } } else { return $this->notFoundResponse(); } }
/** * Добавить лайк к сообщению * @return mixed */ public function actionNewLike() { $msg = Yii::$app->request->post(); $user_set_like = (int) Like::find()->where(['message_id' => $msg['message_id'], 'user_name' => $this->current_user])->count(); if ($user_set_like === 0) { $like = new Like(); $like->message_id = $msg['message_id']; $like->user_name = $msg['user_name']; $like->save(); return Like::find()->where(['message_id' => $msg['message_id']])->count(); } else { return false; } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Like::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query]); $this->author_id = Yii::$app->user->isGuest ? 0 : \Yii::$app->user->id; $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere(['like', 'id', $this->id])->andFilterWhere(['like', 'apartment_id', $this->apartment_id])->andFilterWhere(['like', 'url', $this->url])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'author_id', $this->author_id])->andFilterWhere(['like', 'updater_id', $this->updater_id])->andFilterWhere(['like', 'created_at', $this->created_at])->andFilterWhere(['like', 'updated_at', $this->updated_at]); return $dataProvider; }