Skip to content

Commit

Permalink
Merge branch 'main' into con-page-email-conn
Browse files Browse the repository at this point in the history
  • Loading branch information
myix765 committed Dec 7, 2024
2 parents 526be29 + e4f418e commit 42fba83
Show file tree
Hide file tree
Showing 38 changed files with 1,203 additions and 615 deletions.
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Issue ticket number and link

## Does your code meet the acceptance criteria?
- [] Yes
- [] Yes # put an x in the brackets to checkmark!

## Testing done and screenshots (if relevant)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ npm run client
To run just server:
```
npm run server # in root folder
npm run dev # in backend folder
npm run dev # in api folder
```

## Git Commands Guide
Expand Down
154 changes: 138 additions & 16 deletions backend/server.js → api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ const validateInput = (input, allowedFields) => {

for (const key in input) {
if (allowedFields.includes(key)) {
filteredInput[key] = query[key]
// capitalize the value
filteredInput[key] = input[key].charAt(0).toUpperCase() + input[key].slice(1)
}
}

Expand All @@ -74,10 +75,12 @@ const UserSchema = new Schema({
password: { type: String, required: true },
isAdmin: { type: Boolean, required: true },
username: { type: String, required: true },
enrolledClasses: { type: [Schema.Types.ObjectId], default: [] }
}, { collection: 'users' })

const User = mongoose.model("User", UserSchema)


// Contact Schema
const ContactSchema = new Schema({
name: { type: String, required: true },
Expand All @@ -88,33 +91,50 @@ const ContactSchema = new Schema({

const Contact = mongoose.model('Contact', ContactSchema);

// Class Schema

// Schedule Schema
const ScheduleSchema = new Schema({
day: { type: String, required: true },
time: { type: String, required: true },
})

// Class Schema
const ClassSchema = new Schema({
title: { type: String, required: true },
level: { type: String, required: true },
ageGroup: { type: String, required: true },
instructor: { type: String, required: true },
schedule: { type: [ScheduleSchema], required: true, default: [] },
roster: { type: [Schema.Types.ObjectId], default: [] }
}, { collection: 'classes' })

const Class = mongoose.model("Class", ClassSchema)


// Conversation Schema
const ConversationSchema = new Schema({
instructor: { type: String, required: true },
ageGroup: { type: String, required: true },
schedule: { type: [ScheduleSchema], required: true, default: [] },
roster: { type: [Schema.Types.ObjectId], default: [] }
}, { collection: 'conversations' })

const Conversation = mongoose.model("Conversation", ConversationSchema)


// Level Schema
const InstructorSchema = new Schema({ name: { type: String, required: true } })
const LevelSchema = new Schema({
level: { type: Number, required: true },
name: { type: String, required: true },
instructors: { type: [InstructorSchema], required: true, default: [] },
instructors: { type: [String], required: true, default: [] },
}, { collection: 'levels' })

const Level = mongoose.model("Level", LevelSchema)

//------------------ ENDPOINTS ------------------//

/* USER RELATED ENDPOINTS */

// Sign up
app.post('/api/users', async (req, res) => {
try {
Expand Down Expand Up @@ -163,11 +183,10 @@ app.post('/api/login', async (req, res) => {
try {
const user = await User.findOne({ username });
console.log('Database query result:', user);

if (user) {
if (user.password === password) {
console.log('Login successful for user:', username);
res.status(200).json({user});
res.status(200).json({ user });
} else {
console.log('Login failed: Incorrect password.');
res.status(401).send('Invalid password.');
Expand All @@ -185,16 +204,17 @@ app.post('/api/login', async (req, res) => {
// Get Users
app.get('/api/users', async (req, res) => {
try {
console.log("getting the users");
const users = await User.find();
return res.status(200).json(users);
} catch (err) {
res.status(500).send(err);
}

})

// Contact

/* CONTACT RELATED ENDPOINTS */

// Post Contact
app.post('/api/contact', async (req, res) => {
const { name, email, subject, message } = req.body

Expand Down Expand Up @@ -223,7 +243,7 @@ app.post('/api/contact', async (req, res) => {
console.log('Transporter is ready to send emails', success);
}
});


const mailOptions = {
from: email,
Expand All @@ -248,17 +268,119 @@ app.post('/api/contact', async (req, res) => {
}
});

// Classes
// TODO (Donatello, Claire, Yi): Modify the endpoint to take in query params and filter classes with them

/* CLASS RELATED ENDPOINTS */

// Get Classes
app.get('/api/classes', async (req, res) => {
try {
const data = await Class.find();
console.log(data);
const allowedFields = ['level', 'instructor', 'ageGroup'];
const filters = validateInput(req.query, allowedFields);

//apply the filters directly to the database query
const data = await Class.find(filters);
res.json(data);
} catch (err) {
res.status(500).send(err);
}
})

// Get Levels
app.get("/api/levels", async (req, res) => {
try {
const allowedFields = ['level'];
const filters = validateInput(req.query, allowedFields);
const data = await Level.find(filters);
res.json(data);
} catch (err) {
res.status(500).send(err);
}
})

// Get Conversation classes
app.get("/api/conversations", async (req, res) => {
try {
const data = await Conversation.find();
res.status(200).json(data);
} catch (error) {
res.status(500).send(err);
}
})

// Get Student's classes
app.get('/api/students-classes', async (req, res) => {
try {
const allowedFields = ['_id'];
const filters = validateInput(req.query, allowedFields);

//apply the filters directly to the database query
const data = await User.findOne(filters, { enrolledClasses: 1, _id: 0 });
res.json(data);

} catch (err) {
res.status(500).send(err);
}
})

// Get class by ID
app.get('/api/class', async (req, res) => {
try {
const allowedFields = ['_id'];
const filters = validateInput(req.query, allowedFields);

//apply the filters directly to the database query
const data = await Class.findOne(filters);
res.json(data)

} catch (err) {
res.status(500).send(err);
}
})

// Levels
// TODO (Fahim & Frank): Get the levels data from the database
// Enroll in a class
app.put('/api/users/:id/enroll', async (req, res) => {
const { classId } = req.body
const studentId = new mongoose.Types.ObjectId('671edb6d31e448b23d0dc384') // hardcode userId
try {
// add class id to user's classes
await User.findByIdAndUpdate(
studentId,
{ $addToSet: { enrolledClasses: classId } },
{ new: true }
)

// add student id to class's roster
await Class.findByIdAndUpdate(
classId,
{ $addToSet: { roster: studentId } },
{ new: true }
)
res.status(201).json({ message: 'Enrolled successfully!' })
} catch (err) {
console.error('Error enrolling into class:', err);
res.status(500).json({ message: 'Error enrolling into class' })
}
})

// Unenroll in a class
app.put('/api/users/:id/unenroll', async (req, res) => {
const { classId } = req.body
const studentId = new mongoose.Types.ObjectId('671edb6d31e448b23d0dc384') // hardcode userId
try {
// remove class id from user's classes
await User.findByIdAndUpdate(
studentId,
{ $pull: { enrolledClasses: classId } },
)

// remove student id from class's roster
await Class.findByIdAndUpdate(
classId,
{ $pull: { roster: studentId } },
)
res.status(201).json({ message: 'Unenrolled successfully!' })
} catch (err) {
console.error('Error unenrolling into class:', err);
res.status(500).json({ message: 'Error unenrolling into class' })
}
})
10 changes: 5 additions & 5 deletions backend/package.json → api/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "backend",
"name": "api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"dev": "nodemon server"
"start": "node index.js",
"dev": "nodemon index"
},
"keywords": [],
"author": "",
Expand All @@ -22,4 +22,4 @@
"devDependencies": {
"nodemon": "^3.1.7"
}
}
}
Loading

0 comments on commit 42fba83

Please sign in to comment.