Skip to content

Commit

Permalink
custom count start + pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ChhatreshKhatri committed Nov 15, 2023
1 parent 9d20cbf commit f386052
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 49 deletions.
38 changes: 23 additions & 15 deletions database/mongo.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
const visitsBadgeSchema = require('./schema.js');

module.exports.visitsBadge = async function (uniqueID) {
const badge = await visitsBadgeSchema.findOneAndUpdate(
{ uniqueID: uniqueID },
{
$setOnInsert: {
uniqueID: uniqueID,
},
$inc: { visitsCount: 1 },
},
{ new: true, upsert: true }
).select({ visitsCount: 1 });
return badge.visitsCount;
}
const visitsBadgeSchema = require("./schema.js");

module.exports.visitsBadge = async function (uniqueID, setIDCount, passKey) {
try {
const filter = { uniqueID: uniqueID };
let existingDocument = await visitsBadgeSchema.findOne({ uniqueID: uniqueID });
const update = {
$setOnInsert: { uniqueID: uniqueID, passKey: passKey },
$set: setIDCount !== "0" && passKey === existingDocument?.passKey ? { visitsCount: parseInt(setIDCount) } : {},
$inc: setIDCount === "0" || passKey !== existingDocument?.passKey ? { visitsCount: 1 } : {},
};

const options = { new: true, upsert: true };

const updatedDocument = await visitsBadgeSchema.findOneAndUpdate(filter, update, options).select({ visitsCount: 1 });

return updatedDocument.visitsCount;
} catch (error) {
console.error("Error in visitsBadge:", error);
throw error;
}
};

1 change: 1 addition & 0 deletions database/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const mongoose = require("mongoose");

module.exports = mongoose.model("visitsBadge", new mongoose.Schema({
uniqueID: String,
passKey: String,
visitsCount: {
type: Number,
default: 0,
Expand Down
70 changes: 36 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,56 @@ const cors = require("cors");
const svgBadge = require("./svgBadge");
// const { port, mongourl } = require('./config.json');

require('dotenv').config();
const port=process.env.PORT;
const mongourl=process.env.MONGODB_URI;
require("dotenv").config();
const port = process.env.PORT;
const mongourl = process.env.MONGODB_URI;

// Create an express instance and setup middlewares
const app = express();
app.use(express.json());
app.use(cors());
// app.use(express.static(__dirname + '/frontend/build'));


// Initilize mongoDB connection
const mongoose = require('mongoose');
const database = require('./database/mongo.js');
mongoose.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB.'))
.catch((err) => console.log('Unable to connect to MongoDB.\nError: ' + err));
const mongoose = require("mongoose");
const database = require("./database/mongo.js");
mongoose
.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Connected to MongoDB."))
.catch((err) => console.log("Unable to connect to MongoDB.\nError: " + err));

// Disable caching
app.use(function (req, res, next) {
res.header('Cache-Control', 'private, no-cache, no-store, must-revaluniqueIDate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next()
res.header("Cache-Control", "private, no-cache, no-store, must-revaluniqueIDate");
res.header("Expires", "-1");
res.header("Pragma", "no-cache");
next();
});

async function processSVG(req, res) {
// Get values from query and parameter
const labelBGColor = req.query.labelBGColor || "484848";
const countBGColor = req.query.countBGColor || "1CA2F1";
const labelTextColor = req.query.labelTextColor || "FFFFFF";
const countTextColor = req.query.countTextColor || "FFFFFF";
const shadow = req.query.shadow || "1";
const shadowOpacity = req.query.shadowOpacity || "30";
const label = req.query.label || "VISITS";
const uniqueID = req.params.uniqueID;
const swap = req.query.swap || "0";

// Get the current visits count
const visits = await database.visitsBadge(uniqueID);

// Create the SVG Badge
let svg = svgBadge(label, shadow, shadowOpacity, swap, labelBGColor, countBGColor, labelTextColor, countTextColor, visits);

// Send the SVG Badge
res.setHeader("Content-Type", "image/svg+xml");
res.send(svg);
// Get values from query and parameter
const labelBGColor = req.query.labelBGColor || "484848";
const countBGColor = req.query.countBGColor || "1CA2F1";
const labelTextColor = req.query.labelTextColor || "FFFFFF";
const countTextColor = req.query.countTextColor || "FFFFFF";
const shadow = req.query.shadow || "1";
const shadowOpacity = req.query.shadowOpacity || "30";
const label = req.query.label || "VISITS";
const uniqueID = req.params.uniqueID;
const swap = req.query.swap || "0";
const passKey = req.query.passKey || uniqueID;
const setIDCount = req.query.setIDCount || "0";

// Get the current visits count
const visits = await database.visitsBadge(uniqueID, setIDCount, passKey);

// Create the SVG Badge
let svg = svgBadge(label, shadow, shadowOpacity, swap, labelBGColor, countBGColor, labelTextColor, countTextColor, visits);

// Send the SVG Badge
res.setHeader("Content-Type", "image/svg+xml");
res.send(svg);
}

app.get("/:uniqueID", (req, res) => processSVG(req, res));
app.listen(port, () => console.log(`Ready on port ${port}.`));
app.listen(port, () => console.log(`Ready on port ${port}.`));

0 comments on commit f386052

Please sign in to comment.