From ea749d41e4ea7398e3e0f3634793e8bad8c49028 Mon Sep 17 00:00:00 2001 From: Yujin Hong Date: Mon, 15 Apr 2024 21:08:46 -0400 Subject: [PATCH] survey posts to database --- back-end/App.js | 83 +++++++++++++++++++++++++++++--------- back-end/mockDatabase.json | 54 +++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 20 deletions(-) diff --git a/back-end/App.js b/back-end/App.js index 55bef62..88f85e5 100644 --- a/back-end/App.js +++ b/back-end/App.js @@ -10,7 +10,8 @@ const fs = require("fs"); const path = require("path"); const compat = require("./Compatibility") -const newUser = mongoose.model('User'); +const User = mongoose.model('User'); +const newUser = new User({}); const messageSchema = new mongoose.Schema({ sender: String, @@ -27,6 +28,7 @@ const dbPath = path.join(__dirname, 'mockDatabase.json'); // Import user data const userData = require('./mockDatabase.json'); +const { profile } = require('console'); app.use(cors()); // allow cross-origin resource sharing @@ -48,15 +50,8 @@ app.use(function (req, res, next) { }); console.log("created backend server!!!!!!!!!!!!!!!!"); -let surveyDataArray = []; //This will store new incoming survey data. Its purpose is to simuate the new survey data being sent to the backend let edit_profile_array = []; -//placeholder variables until authentication code is complete -let user; -let pw; -let userList = []; -let unsortedMatches = [] -let sortedMatches = []; // Function to load the current database state function loadDatabase() { @@ -98,11 +93,9 @@ app.post('/login', (req, res) => { if (foundUser) { console.log('Login successful for:', username); // Debug - //this part is a placeholder code until authentication is complete - req.session.user = username; - user = username; - pw = password; - console.log('setting req.session.user to be', req.session.user); //debug + //placeholder code until authentication is complete + logindict = {username: username, password: password}; + newUser.login = logindict; res.json({ message: "Login successful" }); } else { @@ -120,6 +113,7 @@ app.post('/register', (req, res) => { return res.status(400).json({ message: "Password does not meet criteria." }); } + const usersDb = loadDatabase(); // Check if username already exists @@ -138,9 +132,9 @@ app.post('/register', (req, res) => { // Save the updated database state saveDatabase(usersDb); - //this part is a placeholder code until authentication code is complete - user = username; - pw = password; + //placeholder code + logindict = {username: username, password: password}; + newUser.login = logindict; res.json({ message: "Signup successful." }); }); @@ -149,16 +143,65 @@ app.post('/register', (req, res) => { //should push to surveyData arr app.post('/survey', (req, res) => { const surveyData = req.body; - surveyDataArray.push(surveyData); - console.log('Backend has received new survey data:', surveyData);//We should see a message on the backend console with the data that was sent - res.sendStatus(200); //Now tell the frontend that it is safe to proceed (the frontend survey.js will navigate to matches after this) + //console.log('Backend has received new survey data:', surveyData);//We should see a message on the backend console with the data that was sent + + profiledict = {name: surveyData.name, year: surveyData.year, bio:""} + answersdict = { + gender: surveyData.genderAns, year: surveyData.year, pets: surveyData.petsAns, + guests: surveyData.guestsAns, smoke: surveyData.smokeAns, drink: surveyData.drinkAns, + rent_max: surveyData.maxRent, rent_min: surveyData.minRent, + bedtime: surveyData.bedAns, quietness: surveyData.quietAns, cleanliness: surveyData.cleanAns + } + preferencesdict = { + gender: surveyData.genderPref, year: surveyData.yearPref, pets: surveyData.petsPref, + guests: surveyData.guestsPref, smoke: surveyData.smokePref, drink: surveyData.drinkPref, + bedtime: surveyData.bedPref, quietness: surveyData.quietPref, cleanliness: surveyData.cleanPref +} + + newUser.profile = profiledict; + newUser.answers = answersdict; + newUser.preferences = preferencesdict + + // newUser.answers.gender = surveyData.genderAns; + // newUser.answers.year = surveyData.year; + // newUser.answers.pets = surveyData.petsAns; + // newUser.answers.guests = surveyData.guestsAns; + // newUser.answers.smoke = surveyData.smokeAns; + // newUser.answers.drink = surveyData.drinkAns; + // newUser.answers.rent_max = surveyData.maxRent; + // newUser.answers.rent_min = surveyData.minRent; + // newUser.answers.bedtime = surveyData.bedAns; + // newUser.answers.quietness = surveyData.quietAns; + // newUser.answers.cleanliness = surveyData.cleanAns; + + // newUser.preferences.gender = surveyData.genderPref; + // newUser.preferences.year = surveyData.yearPref; + // newUser.preferences.pets = surveyData.petsPref; + // newUser.preferences.guests = surveyData.guestsPref; + // newUser.preferences.smoke = surveyData.smokePref; + // newUser.preferences.drink = surveyData.drinkPref; + // newUser.preferences.bedtime = surveyData.bedPref; + // newUser.preferences.quietness = surveyData.quietPref; + // newUser.preferences.cleanliness = surveyData.cleanPref; + + newUser.save() + .then(() => { + console.log("saved user info into database"); + res.sendStatus(200); + }) + .catch(err => { + console.log(err); + res.status(500).send('server error'); + }); + + //Now tell the frontend that it is safe to proceed (the frontend survey.js will navigate to matches after this) }); app.get('/matches', async (req, res) => { console.log(req.session.user) try { - newUser.find() + User.find() .then(foundUser => { //jsonArray.push(foundUser); console.log("HERE!") diff --git a/back-end/mockDatabase.json b/back-end/mockDatabase.json index 363c5b8..168540e 100644 --- a/back-end/mockDatabase.json +++ b/back-end/mockDatabase.json @@ -160,5 +160,59 @@ "profile": {}, "answers": {}, "preferences": {} + }, + "sarah": { + "login": { + "username": "sarah", + "password": "Sarah123!" + }, + "profile": {}, + "answers": {}, + "preferences": {} + }, + "sarah1": { + "login": { + "username": "sarah1", + "password": "Sarah123!" + }, + "profile": {}, + "answers": {}, + "preferences": {} + }, + "sarahh": { + "login": { + "username": "sarahh", + "password": "Sarah12!" + }, + "profile": {}, + "answers": {}, + "preferences": {} + }, + "sarahhh": { + "login": { + "username": "sarahhh", + "password": "Sarah12!" + }, + "profile": {}, + "answers": {}, + "preferences": {} + }, + "sarahhhh": { + "login": { + "username": "sarahhhh", + "password": "Sarah123!" + }, + "profile": {}, + "answers": {}, + "preferences": {} + }, + "katniss": { + "login": { + "username": "katniss", + "password": "Katniss12!" + }, + "profile": {}, + "answers": {}, + "preferences": {} } } \ No newline at end of file