-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #388 from Sawan-Kushwah/feat/totalVisitorCount
Implemented CURD backend Api for total visitor count #388
- Loading branch information
Showing
6 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
// Function to increment the visitor count | ||
async function incrementVisitorCount() { | ||
try { | ||
const response = await fetch("http://localhost:5000/api/visitor/increment", { | ||
method: "POST", | ||
}); | ||
const data = await response.json(); | ||
console.log("Visitor count incremented:", data.count); | ||
return data.count; | ||
} catch (error) { | ||
console.error("Error incrementing visitor count:", error); | ||
return null; | ||
} | ||
} | ||
|
||
// Function to get the current visitor count | ||
async function getVisitorCount() { | ||
try { | ||
const response = await fetch("http://localhost:5000/api/visitor/count", { | ||
method: "GET", | ||
}); | ||
const data = await response.json(); | ||
return data.count; | ||
} catch (error) { | ||
console.error("Error retrieving visitor count:", error); | ||
return null; | ||
} | ||
} | ||
|
||
const VisitorCounter = () => { | ||
const [visitorCount, setVisitorCount] = useState(null); | ||
|
||
useEffect(() => { | ||
// Increment and retrieve visitor count on component mount | ||
const fetchAndIncrementVisitorCount = async () => { | ||
// First, increment the visitor count | ||
const newCount = await incrementVisitorCount(); | ||
if (newCount !== null) { | ||
setVisitorCount(newCount); | ||
} else { | ||
// If increment fails, fetch the latest count | ||
const currentCount = await getVisitorCount(); | ||
setVisitorCount(currentCount); | ||
} | ||
}; | ||
|
||
fetchAndIncrementVisitorCount(); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
{visitorCount !== null ? ( | ||
<p className="font-bold text-2xl text-gray-300">Total Visitors: {visitorCount}</p> | ||
) : ( | ||
<p>Loading visitor count...</p> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default VisitorCounter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const VisitorCount = require("../Models/Visitor.js"); | ||
|
||
// Increment visitor count | ||
exports.incrementVisitorCount = async (req, res) => { | ||
try { | ||
let visitorCount = await VisitorCount.findOne({}); | ||
if (!visitorCount) { | ||
visitorCount = new VisitorCount({ count: 1 }); | ||
} else { | ||
visitorCount.count += 1; | ||
} | ||
await visitorCount.save(); | ||
res.json({ count: visitorCount.count }); | ||
} catch (error) { | ||
console.error("Error incrementing visitor count:", error); | ||
res.status(500).json({ error: "Error incrementing visitor count" }); | ||
} | ||
}; | ||
|
||
// Get current visitor count | ||
exports.getVisitorCount = async (req, res) => { | ||
try { | ||
const visitorCount = await VisitorCount.findOne({}); | ||
res.json({ count: visitorCount ? visitorCount.count : 0 }); | ||
} catch (error) { | ||
console.error("Error retrieving visitor count:", error); | ||
res.status(500).json({ error: "Error retrieving visitor count" }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// models/VisitorCount.js | ||
const mongoose = require("mongoose"); | ||
|
||
const visitorCountSchema = new mongoose.Schema({ | ||
count: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
}); | ||
|
||
module.exports = mongoose.model("VisitorCount", visitorCountSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const visitorController = require("../Controllers/visitorController"); | ||
|
||
// Route to get current visitor count | ||
router.get("/count", visitorController.getVisitorCount); | ||
|
||
// Route to increment visitor count | ||
router.post("/increment", visitorController.incrementVisitorCount); | ||
|
||
module.exports = router; |