/** * Execute the console command. * * @return mixed */ public function handle() { $now = Carbon::now(); $min = 1; $ignoredWords = array('job', 'jobs', 'hiring', 'careerarc'); echo "starting"; $results = DB::connection('pgsql')->select(DB::raw("SELECT LOWER(hashtag) as hashtag, COUNT(*) as count FROM \"tagQueue\".\"tagQueue\" WHERE hashtag ~ E'[a-z0-9]' AND is_processed = B'0' AND created < now() GROUP BY hashtag", array('now' => $now, 'min' => $min))); echo "got new tags"; $hashtags = []; foreach (Hashtag::where('is_archived', false)->get() as $hashtag) { if (!in_array($hashtag->tag, $ignoredWords)) { $hashtags[$hashtag->tag] = $hashtag->id; } } $newhashtags = []; foreach ($results as $tag) { if (!array_key_exists($tag->hashtag, $hashtags)) { $model = new Hashtag(); $model->tag = $tag->hashtag; if (!in_array($model->tag, $ignoredWords)) { $newhashtags[] = array('tag' => $tag->hashtag, 'current_price' => 1, 'is_active' => false, 'created_at' => $now, 'updated_at' => $now); } } } echo "inserting new hashtags"; Hashtag::insert($newhashtags); foreach (Hashtag::get() as $hashtag) { if (!array_key_exists($hashtag->hashtag, $hashtags)) { $hashtags[$hashtag->tag] = $hashtag->id; } } $counts = []; foreach ($results as $tag) { if (array_key_exists($tag->hashtag, $hashtags)) { $counts[] = array('hashtag_id' => $hashtags[$tag->hashtag], 'count' => $tag->count, 'created_at' => $now, 'updated_at' => $now); } } echo "inserting counts"; HashtagCount::insert($counts); $results = DB::connection('pgsql')->update(DB::raw("UPDATE \"tagQueue\".\"tagQueue\" SET is_processed = B'1' WHERE is_processed = B'0' AND created < ':now';", array('now' => $now))); $results = DB::connection('pgsql')->update(DB::raw("DELETE FROM \"tagQueue\".\"tagQueue\" WHERE is_processed = B'1'")); }
/** * Run the database seeds. * * @return void */ public function run() { // first, create an array of all the users we want to associate hashtags // with // the *key* will be the user email and the *value* will be an array of // hashtags $users = ['*****@*****.**' => ['taylorswift', 'katyperry', 'mileycyrus'], '*****@*****.**' => ['adele', 'taylorswift', 'carrieunderwood']]; // now loop through the above array, creating a new pivot for each user // to hashtag foreach ($users as $email => $hashtag_terms) { // first get the user $user = \App\User::where('email', 'like', $email)->first(); // now loop through each hashtag for this user, adding the pivot foreach ($hashtag_terms as $term) { $hashtag = \App\Hashtag::where('term', 'like', $term)->first(); // associate this hashtag to this user $user->hashtags()->save($hashtag); } } }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { Log::debug('tagcontroller destroy'); //Get Hashtag using text from input given $hashtag = Hashtag::where('name', $id)->firstOrFail(); //Get user from session $user = Auth::user(); $relationExists = count(DB::select('select * from hashtag_user where hashtag_id = ? and user_id = ?', [$hashtag->id, $user->id])); //Check relation does exist if ($relationExists > 0) { //If relation exists //Delete relation user-hashtag $hashtag->users()->detach($user->id); } else { //if it does not exist //Macro format JSON response return response()->api(400, 'No', 'Hashtag already detached from User, hashtag_user non existent.', ''); } //refresh $hashtag = Hashtag::where('name', $id)->firstOrFail(); //check if any user has it related to his account if ($hashtag->users->count() > 0) { //Don't delete from table hashtags, at least one user has it return response()->api(200, 'yes', 'Success detaching hashtag from user.', ''); } else { //No user has it try { //Delete hashtag from mysql db $hashtag->delete(); return response()->api(200, 'yes', 'Success deleting hashtag from db.', ''); } catch (Exception $e) { Log::error("Failed deleting hashtagh from hashtags"); Log::error($e->getMessage()); return response()->api(400, 'no', 'Failed deleting hashtag from hashtags', ''); } } }