/** * Execute the console command. * * @return mixed */ public function fire() { $filename = $this->argument('filename'); $stream = fopen($filename, 'r'); $this->user = User::whereEmail('*****@*****.**')->first(); try { $listener = new StreamingJsonListener(function ($entry) { $name = $this->clean($entry['author']); if (array_has($this->ignore, strtolower($name)) || str_contains(strtolower($name), 'quote')) { $quote = $this->clean($entry['text']); $this->output->write("{$this->count}: {$name} "); $this->error("{$quote}"); } else { $author = Author::firstOrCreate(['name' => $name]); if (!array_has($this->authors, strtolower($name))) { $this->authors[strtolower($name)] = true; } $quote = $this->clean($entry['text']); $q = new Quote(['content' => $quote, 'user_id' => $this->user->id, 'author_id' => $author->id, 'published' => true]); if ($q->save()) { $this->count++; $this->output->writeln("{$this->count}: {$name} <info>{$quote}</info>"); } else { $this->output->write("{$this->count}: {$name} "); $this->warn("{$quote}"); } } }); $parser = new \JsonStreamingParser_Parser($stream, $listener); $parser->parse(); $this->info("{$this->count} quotes pushed."); } catch (\JsonStreamingParser_ParsingError $e) { fclose($stream); } }
public function authors(Request $request) { $list = $request->has('list'); $limit = $this->getLimit($request); if ($list) { $authors = Author::orderBy('name')->lists('name'); return $this->response()->item($authors, function ($authors) { return $authors; }); } $authors = Author::orderBy('name')->paginate($limit); return $this->response()->paginator($authors, new AuthorTransformer()); }
/** * Display a listing of the resource. * * @param \Illuminate\Http\Request $request * * @return \Quoterr\Http\Controllers\Response */ public function index(Request $request) { $q = $request->get('q'); $tag = str_slug($q); $authors = Author::where('name', 'ilike', "%{$q}%")->take(20)->get(); $tags = Tag::where('name', 'ilike', "%{$tag}%")->take(20)->get(); $query = \DB::table(\DB::raw("quotes, to_tsvector(quotes.content) target, to_tsquery('english', ?) query"))->select(['quotes.*', \DB::raw('ts_rank_cd(target, query) as rank')])->setBindings([str_replace(' ', '|', $q)]); $quotes = Quote::query()->setQuery($query)->published()->whereRaw(\DB::raw('target @@ query'))->orderBy('rank', 'desc')->paginate(50); foreach ($quotes as $quote) { $quote->content = $this->highlight($quote->content, $q); } $quotes->load(['author']); $quotes->appends(\Input::except('page')); return view('quote.index', compact('authors', 'tags', 'quotes', 'q')); }
/** * Display a listing of the resource. * * @param null|string $query * * @return \Quoterr\Http\Controllers\Response * @internal param \Illuminate\Http\Request $request * */ public function index($query = null) { $quote = null; $meta = ['title' => 'Quoterr', 'description' => "Quoterr.me is an experience."]; $api = '/api/quotes?random=1'; if ($query !== null) { if (Uuid::isValid($query)) { $type = 'Press ‘space’ for next Quote'; flash('Copy link from address bar to share this quote.'); $quote = Quote::with('author')->whereUuid($query)->first(); $meta['title'] = "A quote by {$quote->author->name} on Quoterr"; $meta['description'] = "{$quote->content} Read awesome quotes on Quoterr."; } else { $author = Author::whereSlug($query)->first(); if ($author) { $type = "Showing quotes by {$author->name}"; $quote = $author->quotes()->published()->orderByRandom()->first(); $api = '/api/author?id=' . $query; $meta['title'] = "Quotes by {$quote->author->name} on Quoterr"; $meta['description'] = "A fine quotation is a diamond in the hand of a man of wit, and a pebble in the hand of a fool. Read awesome quotes on Quoterr."; } else { $tag = Tag::whereSlug($query)->first(); if ($tag) { $type = "Quotes in {$tag->name} category"; $quote = $tag->quotes()->published()->orderByRandom()->first(); $api = '/api/tag?id=' . $query; } } } } if (!$quote) { $type = 'Press ‘space’ for next Quote'; $quote = Quote::with('author')->published()->orderByRandom()->first(); } return view('welcome', compact('quote', 'api', 'type', 'meta')); }
public function transform(EloquentAuthor $author) { return $author->toArray(); }
/** * Display a listing of the resource. * * @return Response */ public function index() { $authors = Author::orderBy('name')->paginate(100); return view('author.index', compact('authors')); }