Skip to content

Commit

Permalink
Merge pull request #8 from BUMETCS673/hw97_create_dummy_user_data
Browse files Browse the repository at this point in the history
HW97 Create user and goal data based on UML
  • Loading branch information
ccerav-bu authored Sep 21, 2024
2 parents 4a5df3c + 01cc79a commit 71f2162
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
27 changes: 24 additions & 3 deletions code/server/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,29 @@ let Schema = mongoose.Schema

mongoose.Promise = global.Promise;

let progressSchema = new Schema({
date: { type: Date, required: true },
value: { type: Number, required: true }
});

let goalSchema = new Schema({
goalId: { type: String, required: true },
type: { type: String, required: true },
targetValue: { type: String, required: true },
unit: { type: String, required: true },
createdAt: { type: Date, default: Date.now },
progress: {type: [progressSchema], required: false } //type: [goalSchema] once that is created
}, {
collection: 'goals'
})

let userSchema = new Schema({
firstname: { type: String, required: true },
lastname: { type: String, required: true }
userId: { type: String, required: true },
email: { type: String, required: true },
passwordHashed: { type: String, required: true },
name: { type: String, required: true },
createdAt: { type: Date, default: Date.now },
goals: {type: [goalSchema], required: false } //type: [goalSchema] once that is created
}, {
collection: 'users'
})
Expand All @@ -25,7 +45,8 @@ module.exports = {
connection = mongoose.createConnection(credentials.MONGO_URI)
console.log("Connected to MongoDB!")
userModel = connection.model("User", userSchema);
models = {userModel: userModel}
goalModel = connection.model("Goal", goalSchema);
models = {userModel: userModel, goalModel: goalModel}
}
return models
},
Expand Down
52 changes: 35 additions & 17 deletions code/server/initdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,58 @@
const db = require("./db.js")

const User = db.getModel().userModel
const Goal = db.getModel().goalModel

async function init() {

try {

// //Option to delete existing data for full reset
// Option to delete existing data for full reset
// await User.deleteMany({})

let user3 = new User({
firstname: "Rebecca",
lastname: "Albright"
// await Goal.deleteMany({})

let goal1 = new Goal({
goalId: "G10001",
type: "sleep",
targetValue: 8,
unit: "hours",
createdAt: "2024-09-10",
progress: [{date: new Date("2024-09-11"), value: 7.5},
{date: new Date("2024-09-12"), value: 8}]
})

let user1 = new User({
userId: "U10001",
email: "[email protected]",
passwordHashed: "pa55w0rd",
name: "John Smith",
createdAt: "2024-09-01",
goals: [goal1]
})
let user4 = new User({
firstname: "James",
lastname: "Madison"

let user2 = new User({
userId: "U10002",
email: "[email protected]",
passwordHashed: "b0st0nuniversity",
name: "Jane Doe",
// createdAt: "2024-09-01", // testing the default param in schema works
goals: []
})

await Promise.all([
user3.save(),
user4.save()
user1.save(),
user2.save(),
goal1.save()
])

let users = await User.find({})

console.log(users)

} catch(error) {
let goals = await Goal.find({})
console.log(goals)

} catch(error) {
console.log(error)

} finally {

db.closeConnection()

}
}

Expand Down

0 comments on commit 71f2162

Please sign in to comment.