{"size": 2173, "ext": "js", "max_stars_repo_path": "server/routes/parser.js", "max_stars_repo_name": "shizukesa92/URL-Shortener-Microservice", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/routes/parser.js", "max_issues_repo_name": "shizukesa92/URL-Shortener-Microservice", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/routes/parser.js", "max_forks_repo_name": "shizukesa92/URL-Shortener-Microservice", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.4157303371, "max_line_length": 172, "alphanum_fraction": 0.5485503912}
const mongo = require("mongodb");
const shortid = require("shortid");
const validUrl = require("valid-url");
const dotenv = require("dotenv").config({
	path: "./.env" // .env not located in root
});

const uri = process.env.MONGOLAB_URI;

module.exports = {

	// Handles shortlink requests
	generate: (req, res) => {
		mongo.MongoClient.connect(uri, (err, db) => {
			if (err) {
				console.log("Unable to connect to server", err);
			} else {
				const database = db.db("url-shortener-microservice"); // Cannot use db.collection because of an express update. Note "..." must first be created as a database in server
				const collection = database.collection("urls"); // Note "urls" must first be created as a collection in server
				const url = req.body.url;
				const host = req.get("host") + "/api/shorturl/";

				const generateLink = (db, callback) => {
					if (validUrl.isUri(url)) { // Checks if URL is valid
						collection.findOne({
							"url": url
						}, {
							"short": 1,
							"_id": 0
						}, (err, doc) => {
							if (doc != null) {
								res.json({
									"original_url": url,
									"short_url": host + doc.short
								});
							} else {
								const shortCode = shortid.generate(); // Shortcode generator
								const newUrl = {
									url: url,
									short: shortCode
								};
								collection.insert([newUrl]);
								res.json({
									"original_url": url,
									"short_url": host + shortCode
								});
							}
						});
					} else {
						res.json({
							"error": "Invalid URL"
						});
					}
				};

				generateLink(db, () => {
					db.close();
				});
			}
		});
	},

	// Handles redirection requests
	redirect: (req, res) => {
		mongo.MongoClient.connect(uri, (err, db) => {
			const database = db.db("url-shortener-microservice");
			const collection = database.collection('urls');
			const short = req.params.short;

			collection.findOne({
				"short": short
			}, {
				"url": 1,
				"_id": 0
			}, (err, doc) => {
				if (doc != null) {
					res.redirect(doc.url);
				} else {
					res.json({
						error: "Shortlink not found in the database."
					});
				}
			});

			db.close();
		});
	}
};
