Updated gitignore to ignore log files, Refactored Authcontroller to use DB interface, Added Try/Catch to validation to catch uncaught error, Added username optional validation to Joi so it expects a username

This commit is contained in:
Alex Holliday committed 2024-05-14 11:12:22 -07:00
1 parent c5b93e7437
commit a4a61695b0
7 files changed
+137 -72

No files matched your search

+29
View File
@@ -21,8 +21,10 @@
// **************************
const Monitor = require("../models/Monitor");
const UserModel = require("../models/user");
const FAKE_MONITOR_DATA = [];
const USERS = [];
for (let i = 0; i < 10; i++) {
FAKE_MONITOR_DATA.push(
@@ -47,6 +49,31 @@ const connect = async () => {
}
};
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;
};
@@ -75,6 +102,8 @@ const getMonitorsByUserId = async (userId) => {
module.exports = {
connect,
insertUser,
getUserByEmail,
getAllMonitors,
getMonitorById,
getMonitorsByUserId,
+22
View File
@@ -1,5 +1,6 @@
const Monitor = require("../models/Monitor");
const mongoose = require("mongoose");
const UserModel = require("../models/user");
const connect = async () => {
try {
@@ -11,6 +12,25 @@ const connect = async () => {
}
};
const insertUser = async (req, res) => {
try {
const newUser = await UserModel.create({ ...req.body });
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 });
return user;
} catch (error) {
throw error;
}
};
// Gets all monitors
const getAllMonitors = async (req, res) => {
try {
@@ -43,6 +63,8 @@ const getMonitorsByUserId = async (req, res) => {
module.exports = {
connect,
insertUser,
getUserByEmail,
getAllMonitors,
getMonitorById,
getMonitorsByUserId,