|
| 1 | +const express = require("express"); |
| 2 | +const mongoose = require("mongoose"); |
| 3 | +const bodyParser = require("body-parser"); |
| 4 | +const Logger = require("./controller/logger"); |
| 5 | +const app = express(); |
| 6 | +const cors = require("cors"); |
| 7 | +require("dotenv").config(); |
| 8 | +const { USER, LOG } = require("./models/TrackerDatabaseModel"); |
| 9 | +const { response } = require("express"); |
| 10 | +// --- Setting Up PORT && MONGOOSE CONFIG --- // |
| 11 | +const MONGO_URI = process.env.MONGO_URI; |
| 12 | +const PORT = process.env.PORT; |
| 13 | +app.use(Logger); |
| 14 | +~app.use(cors()); |
| 15 | +app.use(bodyParser.urlencoded({ extended: false })); |
| 16 | +app.use(express.json()); |
| 17 | + |
| 18 | +app.use(express.static("./public")); |
| 19 | + |
| 20 | +// --- Middlewares --- // |
| 21 | +// --- database connection --- // |
| 22 | +async function ConnectDB() { |
| 23 | + return await mongoose.connect(MONGO_URI, { |
| 24 | + dbName: "db", |
| 25 | + useNewUrlParser: true, |
| 26 | + serverSelectionTimeoutMS: 5000, |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +const connection = mongoose.connection; |
| 31 | +connection.on("error", (error) => { |
| 32 | + console.log(error); |
| 33 | +}); |
| 34 | +connection.on("connecting", (data) => { |
| 35 | + console.log("Connecting to mongoose database......"); |
| 36 | +}); |
| 37 | +connection.on("open", (data) => { |
| 38 | + console.log("Connection to mongoose database established successfully."); |
| 39 | +}); |
| 40 | +// --- Home Route Api * Index.html File --- // |
| 41 | +app.get("/", (req, res) => { |
| 42 | + res.sendFile(__dirname + "/views/index.html"); |
| 43 | +}); |
| 44 | + |
| 45 | +// --- Get All Users * Array Result --- // |
| 46 | +app.get("/api/users", async (req, res) => { |
| 47 | + const users = await USER.find(); |
| 48 | + res.json(users); |
| 49 | +}); |
| 50 | +// --- Create New User Route Api --- // |
| 51 | +app.post("/api/users", async (req, res) => { |
| 52 | + const data = req.body; |
| 53 | + const username = data?.username; |
| 54 | + // --- Username Nan? --- // |
| 55 | + if (!username) { |
| 56 | + console.log("Username not provided"); |
| 57 | + return res.json({ |
| 58 | + error: "Username must be provided!", |
| 59 | + }); |
| 60 | + } |
| 61 | + // --- New User Creation --- // |
| 62 | + console.log("user: " + username + " requested admission."); |
| 63 | + // --- New User Options --- // |
| 64 | + const _user = new USER({ |
| 65 | + username: username, |
| 66 | + }); |
| 67 | + // --- New User Created --- // |
| 68 | + await _user.save(); |
| 69 | + console.log(_user); |
| 70 | + res.json({ |
| 71 | + username: _user.username, |
| 72 | + _id: _user._id, |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +app.post("/api/users/:_id/exercises", async (req, res) => { |
| 77 | + console.log(req.body); |
| 78 | + // --- User id To Insert Data--- // |
| 79 | + const data = req.body; |
| 80 | + const { _id } = req.params; |
| 81 | + const description = data?.description; |
| 82 | + const duration = data?.duration; |
| 83 | + const date = data?.date; |
| 84 | + if (!_id) { |
| 85 | + return res.send("please provide user id"); |
| 86 | + } |
| 87 | + if (!description || !duration) { |
| 88 | + return res.send("please provide description and duration"); |
| 89 | + } |
| 90 | + // --- User Validation --- // |
| 91 | + const _user = await USER.findById(_id); |
| 92 | + console.log(_user); |
| 93 | + if (!_user) { |
| 94 | + // --- No User Found --- // |
| 95 | + console.log(`No User Found! with id:${_id}`); |
| 96 | + return res.send("No user found!"); |
| 97 | + } |
| 98 | + // --- User Found Creating Log --- // |
| 99 | + let _userLog = LOG({ |
| 100 | + createdBy: _user._id, |
| 101 | + description: description, |
| 102 | + duration: Number(duration), |
| 103 | + }); |
| 104 | + if (date) { |
| 105 | + _userLog.date = date; |
| 106 | + } |
| 107 | + await _userLog.save(); |
| 108 | + let response = { |
| 109 | + username: _user.username, |
| 110 | + _id: _user._id, |
| 111 | + description: _userLog.description, |
| 112 | + duration: _userLog.duration, |
| 113 | + date: _userLog.date, |
| 114 | + }; |
| 115 | + // console.log(response); |
| 116 | + res.json(response); |
| 117 | +}); |
| 118 | +// --- Get User All Excercise Logs || Route Api --- // |
| 119 | +app.get("/api/users/:_id/logs", async (req, res) => { |
| 120 | + // --- Get User ID From req.params (:_id) --- // |
| 121 | + const { _id } = req.params; |
| 122 | + console.log(_id); |
| 123 | + let _user = await USER.findById(_id); |
| 124 | + if (!_user) { |
| 125 | + console.log("No User Found!"); |
| 126 | + return res.send("No User Found"); |
| 127 | + } |
| 128 | + let _userLogs = await LOG.find({ createdBy: _user._id }) |
| 129 | + .select("-createdBy") |
| 130 | + .select("-__v") |
| 131 | + .select("-_id"); |
| 132 | + let logArray = []; |
| 133 | + logArray.push(_userLogs); |
| 134 | + // --- Generating Response --- // |
| 135 | + let _response = { |
| 136 | + username: _user.username, |
| 137 | + _id: _user._id, |
| 138 | + count: _userLogs.length, |
| 139 | + log: _userLogs, |
| 140 | + }; |
| 141 | + |
| 142 | + res.json(_response); |
| 143 | +}); |
| 144 | + |
| 145 | +// --- Server Starts Here --- // |
| 146 | + |
| 147 | +async function START_SERVER() { |
| 148 | + await ConnectDB(); |
| 149 | + //--- Server Listening --- // |
| 150 | + const listener = await app.listen(process.env.PORT || 3000, () => { |
| 151 | + console.log("Your app is listening on port " + listener.address().port); |
| 152 | + }); |
| 153 | +} |
| 154 | +//--- Starting Server && Mongoo Connection --- // |
| 155 | +START_SERVER(); |
0 commit comments