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

Nodemon issues #99

Open
wants to merge 4 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
37 changes: 37 additions & 0 deletions api/actions/actions-middlware.js
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
// add middlewares here related to actions


// add middlewares here related to actions

// [ ] Write at least two middleware functions for this API
//, and consume them in the proper places of your code.


const Action = require("./actions/actions-model")

// project action

function validationAction( req, res, next){
const{project_id, description, notes} = req.body;
if(!project_id || !description || !notes ){

next({ message: "missing creditials"})
}
}

function validateActionId(req, res, next){
Action.get(req.params.id)
.then(action => {
if(!action){
res.status(404).json({
message:"no id exists"
})
}
})
.catch(next)
}

module.exports = {
validationAction,
validateActionId

}
85 changes: 85 additions & 0 deletions api/actions/actions-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,86 @@
// Write your "actions" router here!


const Action = require("./actions-model");
const express = require("express")
const {vaildationAction, validateActionId} = require("./actions/middleware")


const router = express.Router()


//1

router.get("/", (req, res, next) => {
Action.get()
.then(actions => {
res.status(200).json(actions)
})
.catch(next)
})

//2
router.get("/:id",validateActionId, (req,res,next) => {
Action.get(req.params.id)
.then(actions => {
if(!actions){
res.status(404)
next()
} else{
res.status(200).json(actions)
}
})
.catch(next)
})

//3

router.post("/",validateActionId, vaildationAction, (req,res,next) => {
Action.insert(req.action, {notes: req.notes,
completed: req.body.completed})
.then(actions => {
if(!actions){
res.status(400)
}
else{
next({
status:200,
message: "Post has been found"
})
}
})
.catch(next)
})

//4

router.put("/:id",vaildationAction, (req,res,next) => {

Action.update(req.params.id)
.then(actions => {
if (!actions) {
res.status(404).json(actions)
}
else{
res.status(200).json(actions)
}
})
.catch(next)
})


router.delete("/:id", validateActionId , (req, res, next) => {
Actions.remove(req.params.id)
.then(actions => {
if(!actions){
res.status(404).json(actions)
}
else{
res.status(200).json(actions)
}
})
.catch(next)
})


module.exports = router
42 changes: 42 additions & 0 deletions api/projects/projects-middleware.js
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@

// add middlewares here related to projects

// [ ] Write at least two middleware functions for this API, and consume them in the proper places of your code.

const Project = require("./projects-model");
function vaildationProject(req, res, next) {
const { name, description } = req.body
if(!name || !description){
next({
message: "Provide name and descripton credintals in the queue",
status: 400
})
}
else{
req.name = name.trim()

}

}


function validateID(req, res, next){
Project.get(req.params.id || req.body.project_id)
.then(projects => {
if(projects){
res.status(404).json({
message:" no project by this id"
})
}
else{
req.action = action
next()
}
})
.catch(next)
}


module.exports = {
vaildationProject,
validateID
};
78 changes: 78 additions & 0 deletions api/projects/projects-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,79 @@
// Write your "projects" router here!

const express = require("express")

const Project = require("./projects-model")

const router = express.Router()

const {vaildationProject ,validateID } = require("./projects-middleware")




router.get("/", (req,res,next) =>{
Project.get()
.then(projects => {
res.status(200).json(projects)
})
.catch(next)
})

router.get("/:id",validateID,( (req,res, next) => {
Project.get(req.params.id)
.then(project => {
if(!project) {
res.status(404)
next()
}
else{

res.status(200).json(req.project)}
})
.catch(next)

}))



router.post("/",vaildationProject,validateID, (req,res,next)=> {
Project.insert(req.project)
.then(project =>{
res.status(200).json(project)
})
.catch(next)
})


router.put("/:id", vaildationProject, validateID,( (req,res,next) => {
Project.update(req.params.id, req.body)
.then(project => {
if(!project){
res.status(404)
next()
} else{
res.status(200).json(project)
}})

.catch(next)
}))

router.delete("./:id", validateID,(req, res, next) => {
Project.remove(req.params.id)
.then(deleted => {

res.status(200).json(deleted)
})
.catch(next)
})

router.get("/:id/actions", (req,res,next) => {
const {id} = req.params;
Project.getProjectActions(id)
.then(actions => {
res.status(200).json(actions)
})
.catch(next)
})

module.exports = router
23 changes: 22 additions & 1 deletion api/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
const express = require('express');
const projectRouter = require('./projects/projects-router')
const actionRouter = require('./actions/actions-router')
const server = express();

server.use(express.json())


// Configure your server here
// Build your actions router in /api/actions/actions-router.js
// Build your projects router in /api/projects/projects-router.js
// Do NOT `server.listen()` inside this file!

module.exports = server;
server.use('/api/projects', projectRouter)
server.use('/api/actions', actionRouter)


server.use(express.json());
server.use(helmet());
server.use(cors());


server.use((err, req, res, next) => {
res.status(err.status || 500).json({
message: err.message,

})
})

module.exports = server;
Loading