use App\Events\TagsVerifiedByAdmin;
use App\Models\LitterTags;
use App\Models\User\User;
use App\Models\Photo;
use App\Models\Location\Country;
use App\Models\Location\State;
use App\Models\Location\City;
use App\Litterrata;
use App\Events\ImageUploaded;
use App\Events\PhotoVerifiedByAdmin;
use Illuminate\Support\Facades\Redis;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class UploadData implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $request;
public $userId;
public function __construct($request, $userId)
{
$this->request = $request;
$this->userId = $userId;
}
public function handle()
{
$user = User::find($this->userId);
$photo = Photo::find($this->request['photo_id']);
$schema = LitterTags::INSTANCE()->getDecodedJSON();
$litterTotal = 0;
foreach ($this->request['litter'] as $category => $values)
{
foreach ($values as $column => $quantity) // butts => 3
{
$id_table = $schema->$category->id_table;
$class = 'App\\Models\\Litter\\Categories\\'.$schema->$category->class;
if (is_null($photo->$id_table))
{
$row = $class::create();
$photo->$id_table = $row->id;
$photo->save();
}
else $row = $class::find($photo->$id_table);
$row->$column = $quantity;
$row->save();
if (($user->show_name == 1) || ($user->show_username == 1))
{
$country = Country::find($photo->country_id);
$state = State::find($photo->state_id);
$city = City::find($photo->city_id);
Redis::zadd($country->country.':Leaderboard', $user->xp, $user->id);
Redis::zadd($country->country.':'.$state->state.':Leaderboard', $user->xp, $user->id);
Redis::zadd($country->country.':'.$state->state.':'.$city->city.':Leaderboard', $user->xp, $user->id);
}
$litterTotal += $quantity;
}
$photo->total_litter = $litterTotal;
if ($user->verification_required == 0)
{
$photo->verification = 1;
$photo->verified = 2;
event(new TagsVerifiedByAdmin($photo->id));
}
else
{
$photo->verification = 0.1;
}
$photo->save();
}
$user->xp += $litterTotal;
$user->save();
}
}