예제 #1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  Request  $request
  * @param URI int $fact_id
  * @return Response
  */
 public function store(Request $request, $user_id = null, $fact_id = null)
 {
     if (!$request->input('tag_name')) {
         return $this->respondUnprocessed();
     }
     $fact = Fact::find($fact_id);
     if ($user_id) {
         $authUser = Auth::ID();
         if ($authUser != $user_id) {
             return $this->respondForbidden("Unauthorized: Must be logged to access endpoint");
         }
         if ($fact->user_id != $user_id) {
             return $this->respondForbidden("Unauthorized: Verify you still have access to resource");
         }
     }
     if (!$fact) {
         return $this->respondNotFound("Fact Not found");
     } else {
         $tag_name = $request->input('tag_name');
         $tag = Tag::firstOrCreate(['tag_name' => $tag_name]);
         if ($tag) {
             TaggedFact::create(['fact_id' => $fact_id, 'tag_id' => $tag->id]);
             $metadata = ['tag_id' => $tag->id];
             return $this->respondCreated("Request Successful", $metadata);
         } else {
             return $this->respondUnprocessed("Unable to tag the fact");
         }
     }
 }
예제 #2
0
 public function getRandomQuestion($fact_id)
 {
     $fact = Fact::find($fact_id);
     $questions = $fact->questions;
     if ($questions) {
         // Gets a random question from 0 to max number of questions available
         $random_question = $questions[mt_rand(0, count($questions) - 1)];
         $fact_with_question = $this->returnFactWithQuestion($fact, $random_question);
         return $fact_with_question;
     } else {
         return FALSE;
     }
 }
예제 #3
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     //
     $fact = Fact::find($id);
     $fact->taggedFact()->forceDelete();
     $fact->questions()->forceDelete();
     $fact->forceDelete();
     return $this->respondCreated();
 }
예제 #4
0
 /**
  * @param $factId
  * @return \Illuminate\Database\Eloquent\Collection|static[]
  */
 public function getTags($factId)
 {
     if (!is_null($factId)) {
         $fact = Fact::find($factId);
         $fact ? $tags = $fact->tags : ($tags = null);
         return $tags;
     }
     $tags = Tag::all();
     return $tags;
 }