diff --git a/models/topic b/api/getTopic.js similarity index 100% rename from models/topic rename to api/getTopic.js diff --git a/models/topic.js b/models/topic.js index fea7356..2aa1e27 100644 --- a/models/topic.js +++ b/models/topic.js @@ -5,7 +5,7 @@ const { Schema } = mongoose; const schema = new Schema({ subjectName:{ type:String, - require + required: true }, beforeclass: Number, afterClass: Number, diff --git a/routes/index.js b/routes/index.js index e2eb4e2..2ea4d9a 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,11 +1,21 @@ const express = require('express'); const router = express.Router(); const fs = require('fs'); +const mongoose = require('mongoose'); +const MongoClient = require('mongodb').MongoClient; +const ObjectID = require('mongodb').ObjectID; +const bodyParser = require('body-parser') +router.use(bodyParser.json()); +const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/progressTracker'; +/* GET home page. */ +const Topic = require('../models/topic'); -/* GET home page. */ +router.get('/', function (req, res) { + res.render('index'); +}) router.get('/add', function (req, res) { res.render('add'); @@ -14,63 +24,21 @@ router.get('/add', function (req, res) { router.get('/edit', function (req, res) { res.render('edit'); }); -router.get('/', function (req, res, next) { +//router.get('/', function (req, res, next) { - /** +/** * Define a callback function to render the - * homepage once the topics data has been loaded - */ - const renderTopics = function (error, file) { - - if (error) { - throw error; - } - - const fileData = file.toString(); - const topicsData = JSON.parse(fileData); - res.render('index', { - title: 'Progress Tracker', - description: 'Keep track of your progress learning new topics. Add a topic below and rate your knowledge of the topic between 1 (no knowledge) and 10 (very confident).', - columns: [ - { - label: 'Topic', - hideLabel: true - }, - { - label: 'Before Class', - }, - { - label: 'After Class', - }, - { - label: 'Monday', - }, - { - label: 'Tuesday', - }, - { - label: 'Wednesday', - }, - { - label: 'Thursday', - }, - { - label: 'Friday', - }, - { - label: 'Edit', - hideLabel: true - } - ], - topics: topicsData, - }); - }; - - /** - * Load the topics file - */ - const topicsFilePath = __dirname + '/../data/topics.json'; - fs.readFile(topicsFilePath, renderTopics); + * homepage once the topics data has been loaded + */ + +router.get('/topics', (req, res, next) => { + mongoose.connect(mongoConnection); + Topic.find({}, (error, topics) => { + res.render('details', { + topics: topics + }) + }); }); + module.exports = router; diff --git a/views/index.handlebars b/views/index.handlebars index 725f74a..2a1a1b8 100644 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -1,4 +1,5 @@ + {{#each columns}}