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("dotenv").config()
const express = require('express')
const app = express();
const tasks = require("./routes/tasks")
const port = 5000
const {connectDB} = require("./db/connect")
const notFound = require("./middleware/not-found")
const start = async() =>{
try {
await connectDB(process.env.MONGO_URI)
console.log("DB connected")
} catch (error) {
console.log(error)
}
}
app.use(express.json())
app.get('/',(req,res)=>{
res.send("Hello")
})

app.use('/api/v1/tasks',tasks)
app.use(notFound)

app.listen(port,console.log(`Server Is Listening on port ${port}...`))
start()
30 changes: 30 additions & 0 deletions 03-task-manager/starter/controller/tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const TaskSchema = require('../model/Task')
const asyncWrapper = require('../middleware/async-wrapper')
const getAllTasks = asyncWrapper(async(req,res) =>{
const {name,pass} = req.body
const data = await TaskSchema.find({name:name,pass:pass})
res.status(200).json({nbhits:data.length,data})
})
const createTask = asyncWrapper(async(req,res)=>{
const data = await TaskSchema.create(req.body);
res.status(201).json({data})

})
const getTask = asyncWrapper(async(req,res)=>{
const taskID = req.params.id
const data = await TaskSchema.findById({_id:taskID})
res.json({data})
})
const updateTask = (req,res)=>{
res.send('create Task')
}
const deleteTask = (req,res)=>{
res.send('create Task')
}
module.exports = {
getAllTasks,
createTask,
getTask,
updateTask,
deleteTask
}
5 changes: 5 additions & 0 deletions 03-task-manager/starter/db/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const mongoose = require("mongoose")
const connectDB = (url) =>{
return mongoose.connect(url)
}
module.exports = {connectDB}
10 changes: 10 additions & 0 deletions 03-task-manager/starter/middleware/async-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const asyncWrapper = (fn)=>{
return async(req,res,next) =>{
try {
await fn(req,res)
} catch (error) {
next(error)
}
}
}
module.exports = asyncWrapper
3 changes: 3 additions & 0 deletions 03-task-manager/starter/middleware/not-found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = notFound = async (req,res)=>{
res.status(404).send("NOT FOUND")
}
11 changes: 11 additions & 0 deletions 03-task-manager/starter/model/Task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { Schema, mongoose } = require("mongoose")
const TaskSchema = new Schema({
name:{
type:String,
required:[true,"Please Provide a Name"],
maxlength:[20,"Please Provide Smaller name"]
},
pass:Boolean
})

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