Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dbconnection #27

Merged
merged 9 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 22 additions & 37 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
require('dotenv').config();
const express = require('express');
const cors = require('cors')
const mongo = require("mongodb");
const mongoose = require("mongoose");
require('dotenv').config();


const app = express()
app.use(cors())
app.use(express.json())

const PORT = process.env.PORT || 4000;

mongoose.connect(process.env.MONGODB_URI)
.then(() => {
console.log('Successfully connected to MongoDB database:', mongoose.connection.name);
Expand Down Expand Up @@ -50,25 +48,29 @@ const UserSchema = new Schema({
password: { type: String, required: true},
isAdmin: { type: Boolean, required: true},
username: { type: String, required: true},
})
const User = mongoose.model("users", UserSchema)
}, { collection: 'users' })
const User = mongoose.model("User", UserSchema)

// Contact Schema

// TODO (Frank & Madeline): Create a ContactSchema
const ContactSchema = new Schema ({
name: { type: String, required: false },
email: { type: String, required: true},
subject: { type: String, required: true},
message: { type: String, required: true}
})
}, { collection: 'contacts' })

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

// Class Schema

// TODO (Claire & Fahim): Create a ClassSchema
const ClassSchema = new Schema({
_id: {type: String, required: true},
title: {type: String, required: true},
level: {type: String, required: true},
ageGroup: {type: String, required: true},
instructor: {type: String, required: true},
schedule: {type: [String], required:true},
}, { collection: 'classes' })
const Class = mongoose.model("Class", ClassSchema)


//------------------ ENDPOINTS ------------------//
Expand Down Expand Up @@ -115,13 +117,9 @@ app.post('/api/users', async (req, res) => {
});

// Login

// TODO (Donatello & John): Create an endpoint to receive login data and check if the user exists in the database
app.post('/api/login', async (req, res) => {
const { username, password } = req.body;

// DEBUG: console.log('Received login request:', username );

try {
const user = await User.findOne({ username });
console.log('Database query result:', user);
Expand All @@ -144,27 +142,7 @@ app.post('/api/login', async (req, res) => {
}
});


// Contact

// TODO (Frank & Madeline): Create an endpoint to receive and upload contact inquiries to the database
// async function uploadContact () {
// const article = new Contact({
// name: 'test user',
// email: '[email protected]',
// subject: 'Test Inquiry',
// message: 'Test message'

// });

// await article.save();
// const firstContact = await Contact.findOne({});
// console.log(firstContact);
// }
// uploadContact().catch(console.error);

// const firstArticle = await Contact.findOne({});
// console.log(firstArticle);
app.post('/api/contact', async (req, res) => {
const{ name, email, subject, message } = req.body
try {
Expand All @@ -185,5 +163,12 @@ app.post('/api/contact', async (req, res) => {
})

// Classes

// TODO (Claire & Fahim): Create an endpoint to retrieve class data from the database
app.get('/api/data', async (req, res) => {
try {
const data = await Class.find();
console.log(data);
res.json(data)
} catch (err) {
res.status(500).send(err);
}
})
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file removed src/Button.jsx
Empty file.
2 changes: 2 additions & 0 deletions src/components/NavBar/NavBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const NavBar = () => {
<div className="hidden sm:flex sm:items-center sm:space-x-4">
<NavLink href="/about" isMobile={false}>About</NavLink>
<NavLink href="/contact" isMobile={false}>Contact</NavLink>
<NavLink href="/classes" isMobile={false}>Classes</NavLink>
<NavLink href="/signup" isMobile={false}>Sign Up</NavLink>
{/* <LanguageSelector /> */}
</div>
Expand All @@ -42,6 +43,7 @@ const NavBar = () => {
<div className="pt-2 pb-3 space-y-1">
<NavLink href="/about" isMobile={true}>About</NavLink>
<NavLink href="/contact" isMobile={true}>Contact</NavLink>
<NavLink href="/classes" isMobile={true}>Classes</NavLink>
<NavLink href="/signup" isMobile={true}>Sign Up</NavLink>
</div>
<div className="pt-4 pb-3 border-t border-gray-200">
Expand Down
2 changes: 2 additions & 0 deletions src/components/PageRoutes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import About from '@/pages/About'
import Contact from '@/pages/Contact'
import SignUp from '@/pages/SignUp'
import Login from '@/pages/Login'
import Classes from '@/pages/Classes'

export default function PageRoutes() {
return (
Expand All @@ -13,6 +14,7 @@ export default function PageRoutes() {
<Route path="/contact" component={Contact} />
<Route path="/signup" component={SignUp} />
<Route path="/login" component={Login} />
<Route path="/classes" component={Classes} />
</div>
);
};
38 changes: 38 additions & 0 deletions src/pages/Classes.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import axios from 'axios';
import { useState, useEffect } from 'react';

const Classes = () => {
const [courses, setCourses] = useState([]);

useEffect(() => {
const fetchCourses = async () => {
try {
const response = await axios.get('http://localhost:4000/api/data');
setCourses(response.data);
console.log(response.data)
} catch (error) {
console.error('Error fetching courses:', error);
}
};
fetchCourses();
}, []);

return (
<div>
<h1>Course List</h1>
{courses.length === 0 ? (
<p>Loading courses...</p>
) : (
<ul>
{courses.map(course => (
<li key={course._id}>
<strong>{course.title}</strong> (Level: {course.level}, Instructor: {course.instructor})
</li>
))}
</ul>
)}
</div>
);
}

export default Classes
4 changes: 1 addition & 3 deletions src/pages/Contact.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ export default function Contact( ) {
};

const handleSubmit = async (e) => {
const { name, email, subject, message } = formData

e.preventDefault();
try {
const response = await fetch("http://localhost:4000/api/contact", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({name, email, subject, message})
body: JSON.stringify(formData)
})

if(!response.ok) {
Expand Down
1 change: 0 additions & 1 deletion src/pages/SignUp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useState } from "react";
import { Link } from 'wouter'
import axios from "axios";


export default function SignUp() {
const [formData, setFormData] = useState({
firstName: '',
Expand Down