Esempio n. 1
0
 /**
  * Create New Collection
  *
  * @param String $name
  * @param Request $request
  * @return Response
  */
 public function postCollection($name, Request $request)
 {
     if (!$this->appKeyAvailable($request)) {
         return $this->notAuthorized($request);
     }
     if (!$this->setSessionUser($request)) {
         $this->setResultError("Not logged in", 401);
     } elseif (!$this->isAdmin()) {
         $this->setResultError("Unauthorized action", 403);
     } elseif ($this->isCollection($name)) {
         $this->setResultError("Collection name already exists", 409);
     } else {
         Collection::create(["name" => $name]);
         $this->setResultOk();
     }
     return $this->setResponse();
 }
Esempio n. 2
0
 public function collection(Request $request, $id)
 {
     if (!$this->login_user) {
         $result = ['status' => 201, 'msg' => '请先登录'];
     } else {
         $query = Collection::where('user_id', $this->login_user->id)->where('collection_type', 'article')->where('collection_id', $id);
         if ($query->first()) {
             $query->delete();
             Article::find($id)->decrement('collections', 1);
             $result = ['status' => 200, 'type' => 'cancel', 'msg' => '已取消收藏'];
         } else {
             Collection::create(['user_id' => $this->login_user->id, 'collection_type' => 'article', 'collection_id' => $id]);
             Article::find($id)->increment('collections', 1);
             $result = ['status' => 200, 'type' => 'success', 'msg' => '收藏成功'];
         }
     }
     return response()->json($result);
 }