Skip to content
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
26 changes: 25 additions & 1 deletion 03-task-manager/starter/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
console.log('Task Manager App')
require('./db/connect')
const express = require('express')
const app = express()
const tasks = require('./routes/tasks')
const port = 8080
const connectDB = require('./db/connect')
require('dotenv').config()
//middleware
app.use(express.json())

//routes
app.get('/', (req, res) => {
res.send('app is running')
})
app.use('/api/v1/tasks', tasks)

const start = async () => {
try {
await connectDB(process.env.MONGO_URI)
app.listen(port, console.log(`app is running on http://localhost:${port}`))
} catch (error) {
console.log(error)
}
}
start()
44 changes: 44 additions & 0 deletions 03-task-manager/starter/controllers/tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const Task = require('../models/task')
//get all tasks
const getAllTasks = async (req, res) => {
try {
const tasks = await Task.find({})
res.status(200).json({ tasks })
} catch (error) {
res.status(500).json({ msg: error })
}
}
//create tasks
const createTask = async (req, res) => {
try {
const task = await Task.create(req.body)
res.status(201).json({ task })
} catch (error) {
res.status(500).json({ msg: error })
}
}

//get single task
const getTask = async (req, res) => {
try {
const { id: taskID } = req.params
const task = await Task.findOne({ _id: taskID })
res.status(200).json({ task })
if (!task) {
return res.status(404).json({ msg: `No task with id: ${taskID}` })
}
} catch (error) {
res.status(500).json({ msg: error })
}
}

//update task
const updateTask = (req, res) => {
res.send('update task')
}
//delete task
const deleteTask = (req, res) => {
res.send('delete task')
}

module.exports = { getAllTasks, createTask, getTask, updateTask, deleteTask }
12 changes: 12 additions & 0 deletions 03-task-manager/starter/db/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const mongoose = require('mongoose')

const connectDB = async (url) => {
return mongoose.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useUnifiedTopology: true,
})
}

module.exports = connectDB
16 changes: 16 additions & 0 deletions 03-task-manager/starter/models/Task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const mongoose = require('mongoose')

const TaskSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Please provide a name'],
trim: true,
maxlength: [20, 'Name cannot be more than 20 characters'],
},
completed: {
type: Boolean,
default: false,
},
})

module.exports = mongoose.model('Task', TaskSchema)
Loading