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

link to slides in read me #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"editor.fontSize": 38,
"terminal.integrated.fontSize": 60
"editor.fontSize": 12,
"terminal.integrated.fontSize": 12
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Here are my slides: https://docs.google.com/presentation/d/1-2_fMAj9_Qx1CH1u_Ui0_uaM7g0607vvoeR2GVR1p7k/edit?usp=sharing
3 changes: 3 additions & 0 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 2 additions & 0 deletions controllers/home.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
14 changes: 14 additions & 0 deletions controllers/todos.js
Original file line number Diff line number Diff line change
@@ -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},{
Expand All @@ -30,6 +42,7 @@ module.exports = {
console.log(err)
}
},
// update method
markIncomplete: async (req, res)=>{
try{
await Todo.findOneAndUpdate({_id:req.body.todoIdFromJSFile},{
Expand All @@ -41,6 +54,7 @@ module.exports = {
console.log(err)
}
},
// delete method
deleteTodo: async (req, res)=>{
console.log(req.body.todoIdFromJSFile)
try{
Expand Down
3 changes: 3 additions & 0 deletions models/Todo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// talk to db using mongoose
const mongoose = require('mongoose')

// we have schema
const TodoSchema = new mongoose.Schema({
todo: {
type: String,
Expand All @@ -11,4 +13,5 @@ const TodoSchema = new mongoose.Schema({
}
})

// exporting model
module.exports = mongoose.model('Todo', TodoSchema)
1 change: 1 addition & 0 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
3 changes: 3 additions & 0 deletions routes/home.js
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions routes/todos.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 8 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
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')
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!')
})
11 changes: 10 additions & 1 deletion views/todos.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@
<body>
<h1>Todos</h1>
<ul>
<!-- to dos is all the objects i got back -> loop through the array of objects
el is the element for the cycle of the for each
pull off first element to build first li and keep on going-->
<!-- el = each document from our database-->
<% todos.forEach( el => { %>
<!-- using data-id as a data set and it's grabbing the id of the document from the database. it's unique for each item-->
<!-- data set is how you grab any data attribute in your db-->
<li class='todoItem' data-id='<%=el._id%>'>
<span class='<%= el.completed === true ? 'completed' : 'not'%>'><%= el.todo %></span>
<!-- a conditional to trigger completed or not-->
<span class='<%= el.completed === true ? 'completed' : 'not'%>'>
<!-- plug in to do text-->
<%= el.todo %></span>
<span class='del'> Delete </span>
</li>
<% }) %>
Expand Down