/
var
/
www
/
html
/
cravings
/
app
/
Http
/
Controllers
/
API
/
Upload File
HOME
<?php namespace App\Http\Controllers\API; use App\Http\Controllers\Controller; use App\Http\Services\ImageManager; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class ExploreController extends Controller { private $db; const EXPLORE_TAGS = [ 'breakfast' => 'Breakfast', 'grill' => 'Grill', 'pizza' => 'Pizza', 'chinese' => 'Chinese', 'indian' => 'Indian', 'italian' => 'Italian', 'asian' => 'Asian', 'fast-food' => 'Fast Food', 'appetizers' => 'Appetizers', 'desserts' => 'Desserts', 'salads' => 'Salads', 'sandwiches' => 'Sandwiches', 'burgers' => 'Burgers', 'seafood' => 'Seafood', 'soups' => 'Soups', 'rice' => 'Rice', 'pasta' => 'Pasta', 'vegetarian' => 'Vegetarian', 'swallow' => 'Swallow', 'nigerian' => 'Nigerian', 'mediterranean' => 'Mediterranean', 'japanese' => 'Japanese' ]; const PLACE_ATTRIBUTES = [ ]; function __construct() { $this->db = DB::getMongoDB(); } public function explore(Request $request) { $db = DB::getMongoDB(); $query = $request->query('q', ''); $user = $request->user(); $pictures = $this->apiExplore($request); $results = true; if (count($pictures) === 0) { $results = false; $projection = [ '$project' => [ '_id' => ['$toString' => '$_id'], 'place_slug' => 1, 'place_name' => 1, 'place_address' => 1, 'image' => 1, 'resized' => 1, 'resized_mobile' => 1, 'food_id' => 1, 'food' => 1, 'likes' => 1 ] ]; if($user) { $projection['$project']['liked'] = ['$in' => [(string) $user->_id, '$likes']]; }else { $projection['$project']['liked'] = ['$in' => [1, '$likes']]; } $pictures = $db->food_explore->aggregate([ ['$sample' => ['size' => 40]], $projection ])->toArray(); } return view('places.explore', [ 'pictures' => $pictures, 'results' => $results, 'page' => 'Explore', 'query' => $query, 'attrs' => static::PLACE_ATTRIBUTES ]); } public function exploreApi(Request $request) { $pictures = $this->apiExplore($request); return response()->json(['data' => $pictures]); } public function apiExplore(Request $request) { $db = DB::getMongoDB(); $user = $request->user(); $query = $request->query('q', ''); $page = $request->query('page', 1); $perPage = $request->query('perPage', 24); $search = ['$regex' => "$query", '$options' => 'i']; $filter = $search ? ['$or' => [ ['food' => $search], ['category' => $search], ['place_name' => $search], ['tags' => $search], ]] : []; $skip = ($page-1)*$perPage; $projection = [ '$project' => [ '_id' => ['$toString' => '$_id'], 'place_slug' => 1, 'place_name' => 1, 'place_address' => 1, 'image' => 1, 'resized' => 1, 'resized_mobile' => 1, 'food_id' => 1, 'food' => 1, 'likes' => 1 ] ]; if($user) { $projection['$project']['liked'] = ['$in' => [(string) $user->_id, '$likes']]; }else { $projection['$project']['liked'] = ['$in' => [1, '$likes']]; } return $db->food_explore->aggregate([ ['$match' => $filter], ['$sort' => ['created_at' => -1]], ['$skip' => $skip], ['$limit' => $perPage], $projection ])->toArray(); } public function filterExplore($attr) { $db = DB::getMongoDB(); $pictures = $db->food_explore->find(['$or' => [['tags' => $attr], ['category' => $attr], ['type' => $attr]]])->toArray(); return response()->json($pictures); } public function addExploreFood() { $tags = self::EXPLORE_TAGS; sort($tags, SORT_STRING); return view('food.add_picture', ['tags' => $tags, 'page' => 'Explore']); } public function storeExploreFood(Request $request) { $data = $_POST; $food = null; $food_id = null; if (!$data['id']) { $food = $this->db->fooding->findOne(['name' => $data['foodname'], 'place_slug' => $data['place']]); }else { $food = $this->db->fooding->findOne(['_id' => new \MongoDB\BSON\ObjectId($data['id'])]); } if (!$food && $data['place']) { $food = [ 'name' => $data['foodname'], 'place_slug' => $data['place'], 'place_name' => $data['placename'], 'price' => 0, 'tags' => $data['foodtag'], 'created_at' => new \MongoDB\BSON\UTCDateTime() ]; $res = $this->db->fooding->insertOne($food); $food_id = $res->getInsertedId(); }else { $this->db->fooding->updateOne(['name' => $data['foodname'], 'place_slug' => $data['place']], ['$set' => ['tags' => $data['foodtag'] ?? []]]); } $ts = $food['tags'] ?? []; $tts = $data['foodtag'] ?? []; $tgs = array_unique(array_merge($ts, $tts)); $f = [ 'place_slug' => $data['place'] ?? '', 'place_name' => $data['placename'], 'food' => $data['foodname'], 'food_id' => $data['id'] || $food_id, 'image' => $data['image'], 'tags' => $tgs, 'likes' => [], 'category' => isset($food['category']) ? [$food['category']] : [], 'created_at' => new \MongoDB\BSON\UTCDateTime() ]; $credentials = array( "service" => "s3", "aws_access_key_id" => env('S3_KEY'), "aws_secret_access_key" => env('S3_SECRET'), "region" => env('S3_REGION') ); $uniq_id = uniqid(); $im = new ImageManager(); $source = $im->fromUrl($data['image']); $base_image = '/explore/images/' . $uniq_id . '.jpg'; $path = env('S3_BUCKET') . $base_image; $credentials['path'] = $path; $resized = $source->resize(array( "method" => "cover", "width" => 500, "height" => 500 )); $resized->store($credentials); $image_path = env('S3_BASE_URL') . $base_image; $f['resized'] = $image_path; $res = $this->db->food_explore->insertOne($f); return redirect()->route('explore'); } public function updateExploreTags() { $db = DB::getMongoDB(); $foods = $db->fooding->find( [ '$or' => [ ['tags' => ['$exists' => true, '$ne' => []]], ['category' => ['$exists' => true, '$ne' => ""]] ] ] )->toArray(); foreach ($foods as $food) { $tags = $food['tags'] ?? []; $category = isset($food['category']) && $food['category'] ? [$food['category']] : []; // var_dump(new \MongoDB\BSON\ObjectId($food['_id'])); $db->food_explore->updateOne( ['food_id' => (string) $food['_id']], ['$set' => ['tags' => $tags, 'category' => $category]] ); } $places = $db->food_explore->find( [ 'food' => ['$exists' => false] ] )->toArray(); foreach ($places as $place) { $pl = $db->places->findOne(['slug' => $place['place_slug'], '$or' => [ ['tags' => ['$exists' => true, '$ne' => []]], ['category' => ['$exists' => true, '$ne' => []]] ]]); if ($pl) { $db->food_explore->updateOne( ['place_slug' => $place['place_slug']], ['$set' => ['tags' => $pl['tags'], 'category' => $pl['category'], 'type' => $pl['type']]] ); } } return response()->json([]); } public function like(Request $request, $id) { $db = DB::getMongoDB(); // $place = $db->places->findOne(['slug' => $slug]); $user = $request->user(); $liked = $db->food_explore->updateOne( [ "_id" => new \MongoDB\BSON\ObjectId($id) ], [ '$addToSet' => [ "likes" => (string) $user->_id ] ], [ 'upsert' => true ] ); $type = 'forward'; if ($liked->getModifiedCount() < 1) { $liked = $db->food_explore->updateOne( [ "_id" => new \MongoDB\BSON\ObjectId($id) ], [ '$pull' => [ "likes" => (string) $user->_id ] ] ); $type = 'reverse'; } $stat = $liked->isAcknowledged() ? 'success' : 'failed'; return response()->json(['status' => $stat, 'type' => $type]); } public function likes($id) { $db = $this->getMongoDB(); $explore = $db->food_explore->findOne(['_id' => new \MongoDB\BSON\ObjectId($id)]); if($explore && isset($explore['likes'])){ return response()->json(['status' => 'success', 'likes' => $explore['likes']]); } return response()->json(['status' => 'error']); } } ?>