public function saveUserAction($userId, $action) { switch ($action) { case static::ACTION_LIKE: case static::ACTION_FLAG: break; default: throw new \InvalidArgumentException("Invalid Action to Add"); } $actionModel = NoteMeta::where('annotation_id', '=', $this->id)->where('user_id', '=', $userId)->where('meta_key', '=', NoteMeta::TYPE_USER_ACTION)->take(1)->first(); if (is_null($actionModel)) { $actionModel = new NoteMeta(); $actionModel->meta_key = NoteMeta::TYPE_USER_ACTION; $actionModel->user_id = $userId; $actionModel->annotation_id = $this->id; } $actionModel->meta_value = $action; return $actionModel->save(); }
public static function addUserAction($note_id, $user_id, $action) { if ($note_id == null || $user_id == null || $action == null) { throw new Exception('Unable to add user action.'); } $toReturn = array('action' => null, 'likes' => -1, 'flags' => -1); $annotation = OldAnnotation::find($note_id); $meta = NoteMeta::where('user_id', $user_id)->where('annotation_id', '=', $note_id)->where('meta_key', '=', 'user_action'); //This user has no actions on this annotation if ($meta->count() == 0) { $meta = new NoteMeta(); $meta->user_id = Auth::user()->id; $meta->note_id = $note_id; $meta->meta_key = 'user_action'; $meta->meta_value = $action; $meta->save(); $toReturn['action'] = true; } elseif ($meta->count() == 1) { $meta = $meta->first(); //This user has already done this action. Removing the action if ($meta->meta_value == $action) { $meta->delete(); $toReturn['action'] = false; } else { $meta->meta_value = $action; $meta->save(); $toReturn['action'] = true; } } else { throw new Exception('Multiple user actions were found'); } $toReturn['likes'] = $annotation->likes(); $toReturn['flags'] = $annotation->flags(); return $toReturn; }