Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Load the AWS SDK for Node.js
- var AWS = require('aws-sdk');
- const axios = require('axios');
- // Set the regionsss
- AWS.config.update({ region: 'us-west-2' });
- const MongoClient = require("mongodb").MongoClient;
- const MONGODB_URI =
- "mongodb://machrus:Kolok8080@mapiddev-shard-00-00-n8uqn.mongodb.net:27017,mapiddev-shard-00-01-n8uqn.mongodb.net:27017,mapiddev-shard-00-02-n8uqn.mongodb.net:27017/mapiddb?ssl=true&replicaSet=MapidDev-shard-0&authSource=admin&retryWrites=true";
- exports.handler = function(event, context, callback) {
- var username = event.body.username;
- var userid = event.body.userid;
- var longitude = parseFloat(event.body.longitude);
- var latitude = parseFloat(event.body.latitude);
- var image_url = event.body.image_url;
- var images = event.body.images;
- var hashtags = event.body.hashtags;
- var project_id = event.body.project_id;
- var layer_id = event.body.layer_id;
- var cognitoid = event.body.cognitoid;
- var fields = event.body.fields;
- /*var username = "yacob89";
- var longitude = parseFloat("0.1");
- var latitude = parseFloat("0.2");
- var image_url = "dfaff";
- var hashtags = "coba coba aja #17anbersamaMAPID #woiwoi";
- var project_id = "5d6cf3fc2e1c920007ad7169";
- var layer_id = "5d88696a62a2810008e32099";*/
- var mongo = require('mongodb');
- var image1 = "-";
- var image2 = "-";
- var image3 = "-";
- var image4 = "-";
- var imagesmodel = [];
- var x;
- for (x = 0; x < images.length; x++) {
- var image_id = new mongo.ObjectID();
- var string_image_id = "feed_image_" + image_id;
- if (x === 0) {
- image1 = images[0];
- }
- if (x === 1) {
- image2 = images[1];
- }
- if (x === 2) {
- image3 = images[2];
- }
- if (x === 3) {
- image4 = images[3];
- }
- imagesmodel.push({ _id: string_image_id, createdAt: new Date(), image_url: images[x] });
- }
- if (image_url === "") {
- image_url = "-";
- }
- if (userid === "") {
- userid = "-";
- }
- var hashtagsArray = hashtags.match(/#[a-zA-Z0-9]+/gi);
- console.log("Hashtagarray: ", hashtagsArray);
- if (hashtagsArray === null) {
- hashtagsArray = [];
- }
- var res = {
- statusCode: 200,
- headers: {
- "Content-Type": "*/*",
- "Access-Control-Allow-Origin": "*", // Required for CORS support to work
- "Access-Control-Allow-Credentials": true // Required for cookies, authorization headers with HTTPS
- }
- };
- var projectid = new mongo.ObjectID(project_id);
- var layerid = new mongo.ObjectID(layer_id);
- var featureid = new mongo.ObjectID();
- if (project_id === "undefined" || layer_id === "undefined") {
- MongoClient.connect(MONGODB_URI, function(err, db) {
- if (err) {
- res.body = err;
- callback(null, res);
- }
- else {
- var dbo = db.db("mapiddb");
- // Insert a single document
- dbo.collection("feeds").insertOne({ createdAt: new Date(), description: hashtags, image_url: image_url, username: username, userid: userid, longitude: longitude, latitude: latitude, flag: 0, layer_id: layer_id, project_id: project_id, cognitoid: cognitoid, hashtags: hashtagsArray, images: imagesmodel, comments: [], location: { type: "Point", coordinates: [longitude, latitude] } }, function(err, result) {
- if (err) {
- console.log(err);
- res.body = { message: "error", error: JSON.stringify(err) };
- callback(null, res.body);
- db.close();
- }
- else {
- console.log(result);
- res.body = { message: "success", _id: result.insertedId };
- callback(null, res.body);
- db.close();
- }
- });
- }
- });
- }
- else {
- axios.get("https://hnjp62bwxh.execute-api.us-west-2.amazonaws.com/GeoDev/getprojectbyid", {
- params: {
- _id: project_id
- }
- }).then(res2 => {
- const stories = res2.data.layers;
- console.log("Jumlah data: ", res2.data.layers.length);
- var found = stories;
- var spss = found.find(obj => {
- return obj._id === layer_id
- })
- console.log("found: ", spss.geojson.features.length);
- var generated_id = spss.geojson.features.length;
- if (stories) {
- console.log("Ceritanya adalah: ", stories);
- MongoClient.connect(MONGODB_URI, function(err, db) {
- if (err) {
- res.body = err;
- callback(null, res);
- }
- else {
- var dbo = db.db("mapiddb");
- var insertedId;
- // Insert a single document
- dbo.collection("feeds").insertOne({ createdAt: new Date(), description: hashtags, fields: fields, image_url: image_url, username: username, userid: userid, longitude: longitude, latitude: latitude, flag: 0, cognitoid: cognitoid, layer_id: layer_id, project_id: project_id, hashtags: hashtagsArray, images: imagesmodel, comments: [], location: { type: "Point", coordinates: [longitude, latitude] } }, function(err, result) {
- if (err) {
- console.log(err);
- res.body = { message: "error", error: JSON.stringify(err) };
- callback(null, res.body);
- db.close();
- }
- else {
- console.log(result);
- insertedId = result.insertedId;
- // Setelah insert ke feeds, insert new layer di MDCs
- fields["mapid_id"] = generated_id;
- fields["feed_id"] = insertedId;
- fields["createdAt"] = new Date();
- fields["username"] = username;
- fields["icon-image"] = "star";
- fields["description"] = hashtags;
- fields["image1"] = image1;
- fields["image2"] = image2;
- fields["image3"] = image3;
- fields["image4"] = image4;
- var myquery = { "_id": projectid };
- var newvalues = { $push: { "layers.$[elem].geojson.features": { id: featureid, type: "Feature", properties: fields, geometry: { coordinates: [longitude, latitude], type: "Point" } } } };
- dbo.collection("projects").updateOne(myquery, newvalues, { arrayFilters: [{ "elem._id": { $eq: layerid } }] }, function(err, result) {
- if (err) {
- console.log(err);
- res.body = { message: "error", error: JSON.stringify(err) };
- callback(null, res.body);
- db.close();
- }
- else {
- console.log(result);
- res.body = { message: "success", _id: insertedId };
- callback(null, res.body);
- db.close();
- }
- });
- }
- });
- }
- });
- }
- else {
- res.body = { message: "error" };
- callback(null, res.body);
- }
- });
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement