Delete Server directory

This commit is contained in:
Mohammad Khalilzadeh authored and GitHub committed 2024-05-18 09:04:41 +03:30
1 parent b2c8e50043
commit 3a94abb649
17 files changed
-3147

No files matched your search

-110
View File
@@ -1,110 +0,0 @@
// **************************
// The idea here is to provide a layer of abstraction between the database and whoever is using it.
// Instead of directly calling mongoose methods, we can call the methods on the DB object.
// If this were Typescript or Java or Golang an interface would be implemented to ensure the methods are available.
// But we do the best we can with Javascript.
//
// If the methods are consistent all we have to do to swap out one DB for another is simply change the import.
//
// Example:
// We start with the fake DB:
//
// const db = require("../db/FakeDb");
// const monitors = await db.getAllMonitors();
//
// And when we want to swtich to a real DB, all we have to do is swap the import
//
// const db = require("../db/MongoDb");
// const monitors = await db.getAllMonitors();
//
// The rest of the code is the same, as all the `db` methods are standardized.
// **************************
const Monitor = require("../models/Monitor");
const UserModel = require("../models/user");
const FAKE_MONITOR_DATA = [];
const USERS = [];
for (let i = 0; i < 1; i++) {
FAKE_MONITOR_DATA.push(
new Monitor({
userId: i % 2 === 0 ? 1 : 2,
name: `Monitor ${i}`,
description: `Description for Monitor ${i}`,
url: `https://monitor${i}.com`,
isActive: true,
interval: Math.floor(Math.random() * 10) + 1,
updated_at: new Date(),
created_at: new Date(),
})
);
}
const connect = async () => {
try {
await console.log("Connected to FakeDB");
} catch (error) {
console.error(error);
}
};
const insertUser = async (req, res) => {
try {
const newUser = new UserModel({ ...req.body });
USERS.push(newUser);
return newUser;
} catch (error) {
throw error;
}
};
const getUserByEmail = async (req, res) => {
const email = req.body.email;
try {
const idx = USERS.findIndex((user) => {
return user.email === email;
});
if (idx === -1) {
return null;
}
return USERS[idx];
} catch (error) {
throw new Error(`User with email ${email} not found`);
}
};
const getAllMonitors = async () => {
return FAKE_MONITOR_DATA;
};
const getMonitorById = async (monitorId) => {
const idx = FAKE_MONITOR_DATA.findIndex((monitor) => {
return monitor.id === monitorId;
});
if (idx === -1) {
throw new Error(`Monitor with id ${monitorId} not found`);
}
return FAKE_MONITOR_DATA[idx];
};
const getMonitorsByUserId = async (userId) => {
const userMonitors = FAKE_MONITOR_DATA.filter((monitor) => {
return monitor.userId === userId;
});
if (userMonitors.length === 0) {
throw new Error(`Monitors for user ${userId} not found`);
}
return userMonitors;
};
module.exports = {
connect,
insertUser,
getUserByEmail,
getAllMonitors,
getMonitorById,
getMonitorsByUserId,
};
-71
View File
@@ -1,71 +0,0 @@
const Monitor = require("../models/Monitor");
const mongoose = require("mongoose");
const UserModel = require("../models/user");
const connect = async () => {
try {
await mongoose.connect(process.env.DB_CONNECTION_STRING);
console.log("Connected to MongoDB");
} catch (error) {
console.error("Failed to connect to MongoDB");
throw error;
}
};
const insertUser = async (req, res) => {
try {
const newUser = await UserModel.create({ ...req.body }).select('-password');
return newUser;
} catch (error) {
throw error;
}
};
const getUserByEmail = async (req, res) => {
try {
// Returns null if no user is found
const user = await UserModel.findOne({ email: req.body.email }).select('-password');
return user;
} catch (error) {
throw error;
}
};
// Gets all monitors
const getAllMonitors = async (req, res) => {
try {
const monitors = await Monitor.find();
return monitors;
} catch (error) {
throw error;
}
};
// Get a monitor by ID
const getMonitorById = async (req, res) => {
try {
const monitor = await Monitor.findById(req.params.monitorId);
return monitor;
} catch (error) {
throw error;
}
};
// Gets a monitor by user ID
const getMonitorsByUserId = async (req, res) => {
try {
const monitors = await Monitor.find({ userId: req.params.userId });
return monitors;
} catch (error) {
throw error;
}
};
module.exports = {
connect,
insertUser,
getUserByEmail,
getAllMonitors,
getMonitorById,
getMonitorsByUserId,
};