/**
  * 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");
         }
     }
 }
Example #2
0
 public function select_unique_tagged_fact($user_id, $tag_id, $excludes)
 {
     $facts = Fact::with(array('taggedFact' => function ($query) use($tag_id) {
         $query->where('tag_id', '=', $tag_id);
     }))->where('user_id', '=', $user_id)->whereNotIn('id', $excludes)->get();
     return $this->transformAndReturnFact($facts);
 }
 public function testIf_fact_and_question_returned()
 {
     //arrange
     $fact_id = Fact::create(['user_id' => 1, 'fact' => 'I hope you learn to make it on your own'])->id;
     $questions = $this->getSampleQuestions($fact_id);
     Question::insert($questions);
     //act
     $questionRandomizer = new QuestionSelector();
     $fact_with_question = $questionRandomizer->getRandomQuestion($fact_id);
     //assert
     $this->assertArrayHasKeys(['fact_id', 'user_id', 'question_title', 'question_type', 'fact'], $fact_with_question);
 }
 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;
     }
 }
Example #5
0
 public function testIf_user_edits_unauthorized_resource_throw_error()
 {
     //arrange
     // User is trying to edit a resource he doesn't posses
     $this->authorizeTestUser();
     // user_id = 1
     $fact_id = Fact::create(['user_id' => 10, 'fact' => 'If I give you the funk, you gon take it?'])->id;
     $url = "api/v1/user/1/fact/{$fact_id}/tag";
     $tag = ['tag_name' => "This is a test tag"];
     // act
     $this->getJson($url, "POST", $tag);
     //assert
     $this->assertResponseStatus('403');
 }
 public function insertAnswerAsFactWithDefaultQuestions($answer)
 {
     $fact_id = Fact::create(['user_id' => $this->user_id, 'fact' => $answer])->id;
     $questions = [['fact_id' => $fact_id, 'question_title' => 'Can you create a test question from this fact?', 'question_type' => 1], ['fact_id' => $fact_id, 'question_title' => 'Write this fact in your own words', 'question_type' => 2], ['fact_id' => $fact_id, 'question_title' => 'Write a paragraph about the fact and its related implications', 'question_type' => 3], ['fact_id' => $fact_id, 'question_title' => 'How can you apply this to your day, or life in general?', 'question_type' => 4], ['fact_id' => $fact_id, 'question_title' => 'Can you find a related fact to this fact', 'question_type' => 5]];
     Question::insert($questions);
 }
 /**
  * @param $user_id
  * @return null
  */
 protected function getUserFacts($user_id)
 {
     if (!is_null($user_id)) {
         $user = User::find($user_id);
         $user ? $facts = $user->facts : ($facts = null);
         return $facts;
     }
     $facts = Fact::all();
     return $facts;
 }
 /**
  * @param $user
  */
 public function tagMockFacts($user)
 {
     // create two tags
     $tag_id = Tag::create(['tag_name' => 'Test Tag1'])->id;
     $tag_id2 = Tag::create(['tag_name' => 'Test Tag2'])->id;
     $facts = Fact::where('user_id', $user->id)->get();
     foreach ($facts as $fact) {
         // Basically a 50/50 coin flip whether a tag is going to get tag 1 or tag 2
         rand(0, 1) ? \App\Models\TaggedFact::create(['fact_id' => $fact->id, 'tag_id' => $tag_id]) : \App\Models\TaggedFact::create(['fact_id' => $fact->id, 'tag_id' => $tag_id2]);
     }
 }
Example #9
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;
 }