Skip to content

Commit

Permalink
use database in controller for index and show
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleinen committed Dec 8, 2020
1 parent d5d3ad1 commit 99f7d25
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions controllers/todosController.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
'use strict'

const todos = require('../models/todos')
const Todo = require('../models/todos')

exports.index = (req, res, next) => {
res.render('todos/index', {todos: todos})
Todo.find()
.sort({ 'name': 'asc' })
.then(todos => {
res.render('todos/index', {todos: todos})
})
.catch(error => {
console.log(`Error fetching todos: ${error.message}`)
next(error)
})
}

exports.show = (req, res, next) => {
const todoId = req.params.id
res.render('todos/show', {todo: todos[todoId]})
Todo.findById(todoId)
.then( todo =>{
res.render('todos/show', {todo: todo})
})
.catch(error => {
console.log(`Error fetching todo by ID: ${error.message}`)
next(error)
})

}


exports.create = (req, res, next) => {
const newTodo = getParams(req.body)
res.send({todo: newTodo, body: req.body})
Expand Down

0 comments on commit 99f7d25

Please sign in to comment.