Skip to content

Commit

Permalink
Add the feature to Set Up Database for Course Information (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artlfmj authored Oct 1, 2023
2 parents f9f03e5 + a60ab9b commit f89a63c
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 7 deletions.
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"mongodb_uri": "YOUR_MONGODB_URI_HERE",
"secret_key": "YOUR_SECRET_KEY_HERE"
}

157 changes: 156 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"dependencies": {
"ejs": "^3.1.9",
"express": "^4.18.2",
"mongodb": "^6.1.0"
"mongodb": "^6.1.0",
"mongoose": "^7.5.3"
}
}
58 changes: 53 additions & 5 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
const express = require("express");
const express = require('express');
const mongoose = require('mongoose');
const fs = require('fs');

const app = express();

app.get("/", (req, res) => {
res.send("Hello World!");
// Read configuration from config.json
let config;
try {
const configData = fs.readFileSync('config.json');
config = JSON.parse(configData);
} catch (err) {
console.error('Error reading config file:', err);
process.exit(1);
}

// Connect to MongoDB using the configuration
mongoose.connect(config.mongodb_uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('Connected to MongoDB');
// Start your application logic here
})
.catch((err) => {
console.error('Error connecting to MongoDB:', err);
process.exit(1);
});

// Simulated user data (replace with actual user data from your database)
const users = [
{ username: 'user1', password: 'password1' },
{ username: 'user2', password: 'password2' },
];

// Define your login function here
function login(username, password) {
const user = users.find((u) => u.username === username && u.password === password);
return !!user; // Return true if the user exists, false otherwise
}

// Example usage of the login function
app.get('/login', (req, res) => {
const username = req.query.username;
const password = req.query.password;

if (login(username, password)) {
res.send('Login successful');
} else {
res.status(401).send('Login failed');
}
});

app.listen(3000, () => {
console.log("Server is up on port 3000");
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
30 changes: 30 additions & 0 deletions src/db/courseDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Import the mongoose library
const mongoose = require('mongoose');

// Define a schema for the course
const courseSchema = new mongoose.Schema({
title: {
type: String,
required: true,
trim: true, // Remove whitespace from the beginning and end of the string
},
description: {
type: String,
required: true,
},
duration: {
type: Number,
required: true,
},
difficulty: {
type: String,
required: true,
},
// Add more fields as needed
});

// Create a model using the schema
const Course = mongoose.model('Course', courseSchema);

// Export the Course model
module.exports = Course;

0 comments on commit f89a63c

Please sign in to comment.