Skip to content

Commit

Permalink
Connecttion Etabalished and Create View, Routes, Controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekraoas committed May 24, 2024
1 parent 22f0c51 commit e6e9a65
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 4 deletions.
9 changes: 9 additions & 0 deletions connectionDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const mongoose = require("mongoose");

async function connectToMongoDB(url){
return mongoose.connect(url);
}

module.exports = {
connectToMongoDB,
}
37 changes: 37 additions & 0 deletions controllers/url.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const shortid = require("shortid");
const URL = require("../models/url.models");

async function handleGenerateNewShortURL(req, res){
const body = req.body;
if(!body.url){
return res.status(400).json({
message: "redirectURL is required",
});
}
const shortID = shortid();

await URL.create({
shortId: shortID,
redirectURL: body.url,
visitHistory: [],
});

return res.json({id: shortID});

}


async function handleGetAnalytics(req, res){
const shortId = req.params.shortId;
const result = await URL.findOne({shortId});
return res.json({
totalClicks : result.visitHistory.length,
analytics: result.visitHistory,
});

}

module.exports={
handleGenerateNewShortURL,
handleGetAnalytics,
}
6 changes: 4 additions & 2 deletions models/url.models.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ const urlSchema = new mongoose.Schema(
type: String,
required: true,
},
visitHistory: [{ timestap: { type: Number } }],
visitHistory: [{ timestamp: { type: Number } }],
},
{
timestamps: true,
}
);

const URL = mongoose.model("URL", urlSchema);
const URL = mongoose.model("URL", urlSchema);

module.exports = URL;
Loading

0 comments on commit e6e9a65

Please sign in to comment.