diff --git a/.vscode/settings.json b/.vscode/settings.json index 7fa64d5..2ef2fce 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,4 @@ { - "editor.fontSize": 38, - "terminal.integrated.fontSize": 60 + "editor.fontSize": 12, + "terminal.integrated.fontSize": 12 } \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9e30a89 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Here are my slides: https://docs.google.com/presentation/d/1-2_fMAj9_Qx1CH1u_Ui0_uaM7g0607vvoeR2GVR1p7k/edit?usp=sharing diff --git a/config/database.js b/config/database.js index 42db1ac..8642350 100644 --- a/config/database.js +++ b/config/database.js @@ -1,7 +1,10 @@ +// using mongoose to connect to our mongo database const mongoose = require('mongoose') const connectDB = async () => { + // exporting an async function try { + // db_string variable is in my env file const conn = await mongoose.connect(process.env.DB_STRING, { useNewUrlParser: true, useUnifiedTopology: true, diff --git a/controllers/home.js b/controllers/home.js index e300159..31b1a1a 100644 --- a/controllers/home.js +++ b/controllers/home.js @@ -1,3 +1,5 @@ +// spitting out a object that has a method +// getIndex renders out index.ejs - gives us our HTML module.exports = { getIndex: (req,res)=>{ res.render('index.ejs') diff --git a/controllers/todos.js b/controllers/todos.js index b20b1a3..a7ada13 100644 --- a/controllers/todos.js +++ b/controllers/todos.js @@ -1,24 +1,36 @@ +// requiring our todo model const Todo = require('../models/Todo') +// object is made up of five methods of CRUD +// only thing that's talking to model module.exports = { + // get method getTodos: async (req,res)=>{ try{ + // using the Todo model to find() - don't have to db.collection.. etc etc const todoItems = await Todo.find() const itemsLeft = await Todo.countDocuments({completed: false}) + // padding in all the Todos in our database res.render('todos.ejs', {todos: todoItems, left: itemsLeft}) }catch(err){ console.log(err) } }, + // create method createTodo: async (req, res)=>{ try{ + // create method is like insert one + // create a document that has a todo property and a completed property + // req.body.todoItem came from the form (it has input name="todoItem") await Todo.create({todo: req.body.todoItem, completed: false}) console.log('Todo has been added!') + // reload after it's complete -> make a get request res.redirect('/todos') }catch(err){ console.log(err) } }, + // update method markComplete: async (req, res)=>{ try{ await Todo.findOneAndUpdate({_id:req.body.todoIdFromJSFile},{ @@ -30,6 +42,7 @@ module.exports = { console.log(err) } }, + // update method markIncomplete: async (req, res)=>{ try{ await Todo.findOneAndUpdate({_id:req.body.todoIdFromJSFile},{ @@ -41,6 +54,7 @@ module.exports = { console.log(err) } }, + // delete method deleteTodo: async (req, res)=>{ console.log(req.body.todoIdFromJSFile) try{ diff --git a/models/Todo.js b/models/Todo.js index 85753af..5dbd152 100644 --- a/models/Todo.js +++ b/models/Todo.js @@ -1,5 +1,7 @@ +// talk to db using mongoose const mongoose = require('mongoose') +// we have schema const TodoSchema = new mongoose.Schema({ todo: { type: String, @@ -11,4 +13,5 @@ const TodoSchema = new mongoose.Schema({ } }) +// exporting model module.exports = mongoose.model('Todo', TodoSchema) diff --git a/public/js/main.js b/public/js/main.js index b4cfee0..4e63b00 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -15,6 +15,7 @@ Array.from(todoComplete).forEach((el)=>{ }) async function deleteTodo(){ + // grab toDo from parent node (li) and it has a dataset (data-id) const todoId = this.parentNode.dataset.id try{ const response = await fetch('todos/deleteTodo', { diff --git a/routes/home.js b/routes/home.js index 29801ec..da6a0be 100644 --- a/routes/home.js +++ b/routes/home.js @@ -1,7 +1,10 @@ +// file sees the route and tells what controller to run +// look at url and determine what controller to run! const express = require('express') const router = express.Router() const homeController = require('../controllers/home') +// request coming in on main route and use homeController in controller file router.get('/', homeController.getIndex) module.exports = router \ No newline at end of file diff --git a/routes/todos.js b/routes/todos.js index 5d54fb6..0346907 100644 --- a/routes/todos.js +++ b/routes/todos.js @@ -1,7 +1,12 @@ +// file hears /todos and determines what controller to run +// CRUD!! +// go to todos controller const express = require('express') const router = express.Router() const todosController = require('../controllers/todos') +// don't need /todos/getTodos -> they already know it's talking about todos +// specific method for each CRUD request router.get('/', todosController.getTodos) router.post('/createTodo', todosController.createTodo) diff --git a/server.js b/server.js index edcb2f6..1d84b90 100644 --- a/server.js +++ b/server.js @@ -1,11 +1,16 @@ const express = require('express') const app = express() +// no more mongo client - we have mongoose - leads to database file const connectDB = require('./config/database') +// separate place for routes and this connects to it +// routes look at the request and they figure out which controller should be handling that request +// controller is the middle person that talkes to database, hands off info to views for rendering const homeRoutes = require('./routes/home') const todoRoutes = require('./routes/todos') require('dotenv').config({path: './config/.env'}) +// use this line to connect to database - calling connectDB() from database.js connectDB() app.set('view engine', 'ejs') @@ -13,9 +18,12 @@ app.use(express.static('public')) app.use(express.urlencoded({ extended: true })) app.use(express.json()) +// heard a request on '/' -> go to homeRoutes app.use('/', homeRoutes) +// heard a request on '/todos' -> go to todoRoutes app.use('/todos', todoRoutes) +//port is in env file app.listen(process.env.PORT, ()=>{ console.log('Server is running, you better catch it!') }) \ No newline at end of file diff --git a/views/todos.ejs b/views/todos.ejs index 6e5e061..70480d6 100644 --- a/views/todos.ejs +++ b/views/todos.ejs @@ -10,9 +10,18 @@

Todos