Skip to content

Commit c022528

Browse files
committed
nodemon issues
1 parent 83b95f7 commit c022528

File tree

7 files changed

+1841
-9
lines changed

7 files changed

+1841
-9
lines changed

api/actions/actions-middlware.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
11
// add middlewares here related to actions
2+
3+
4+
// add middlewares here related to actions
5+
6+
// [ ] Write at least two middleware functions for this API
7+
//, and consume them in the proper places of your code.
8+
9+
10+
const Action = require("./actions/actions-model")
11+
12+
// project action
13+
14+
function validationAction( req, res, next){
15+
const{project_id, description, notes} = req.body;
16+
if(!project_id || !description || !notes ){
17+
18+
next({ message: "missing creditials"})
19+
}
20+
}
21+
22+
function validateActionId(req, res, next){
23+
Action.get(req.params.id)
24+
.then(action => {
25+
if(!action){
26+
res.status(404).json({
27+
message:"no id exists"
28+
})
29+
}
30+
})
31+
.catch(next)
32+
}
33+
34+
module.exports = {
35+
validationAction,
36+
validateActionId
37+
38+
}

api/actions/actions-router.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,86 @@
11
// Write your "actions" router here!
2+
3+
4+
const Action = require("./actions-model");
5+
const express = require("express")
6+
const {vaildationAction, validateActionId} = require("./actions/middleware")
7+
8+
9+
const router = express.Router()
10+
11+
12+
//1
13+
14+
router.get("/", (req, res, next) => {
15+
Action.get()
16+
.then(actions => {
17+
res.status(200).json(actions)
18+
})
19+
.catch(next)
20+
})
21+
22+
//2
23+
router.get("/:id",validateActionId, (req,res,next) => {
24+
Action.get(req.params.id)
25+
.then(actions => {
26+
if(!actions){
27+
res.status(404)
28+
next()
29+
} else{
30+
res.status(200).json(actions)
31+
}
32+
})
33+
.catch(next)
34+
})
35+
36+
//3
37+
38+
router.post("/",validateActionId, vaildationAction, (req,res,next) => {
39+
Action.insert(req.action, {notes: req.notes,
40+
completed: req.body.completed})
41+
.then(actions => {
42+
if(!actions){
43+
res.status(400)
44+
}
45+
else{
46+
next({
47+
status:200,
48+
message: "Post has been found"
49+
})
50+
}
51+
})
52+
.catch(next)
53+
})
54+
55+
//4
56+
57+
router.put("/:id",vaildationAction, (req,res,next) => {
58+
59+
Action.update(req.params.id)
60+
.then(actions => {
61+
if (!actions) {
62+
res.status(404).json(actions)
63+
}
64+
else{
65+
res.status(200).json(actions)
66+
}
67+
})
68+
.catch(next)
69+
})
70+
71+
72+
router.delete("/:id", validateActionId , (req, res, next) => {
73+
Actions.remove(req.params.id)
74+
.then(actions => {
75+
if(!actions){
76+
res.status(404).json(actions)
77+
}
78+
else{
79+
res.status(200).json(actions)
80+
}
81+
})
82+
.catch(next)
83+
})
84+
85+
86+
module.exports = router

api/projects/projects-middleware.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
1+
12
// add middlewares here related to projects
3+
4+
// [ ] Write at least two middleware functions for this API, and consume them in the proper places of your code.
5+
6+
const Project = require("./projects-model");
7+
function vaildationProject(req, res, next) {
8+
const { name, description } = req.body
9+
if(!name || !description){
10+
next({
11+
message: "Provide name and descripton credintals in the queue",
12+
status: 400
13+
})
14+
}
15+
else{
16+
req.name = name.trim()
17+
18+
}
19+
20+
}
21+
22+
23+
function validateID(req, res, next){
24+
Project.get(req.params.id || req.body.project_id)
25+
.then(projects => {
26+
if(projects){
27+
res.status(404).json({
28+
message:" no project by this id"
29+
})
30+
}
31+
else{
32+
req.action = action
33+
next()
34+
}
35+
})
36+
.catch(next)
37+
}
38+
39+
40+
module.exports = {
41+
vaildationProject,
42+
validateID
43+
};

api/projects/projects-router.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,79 @@
11
// Write your "projects" router here!
2+
3+
const express = require("express")
4+
5+
const Project = require("./projects-model")
6+
7+
const router = express.Router()
8+
9+
const {vaildationProject ,validateID } = require("./projects-middleware")
10+
11+
12+
13+
14+
router.get("/", (req,res,next) =>{
15+
Project.get()
16+
.then(projects => {
17+
res.status(200).json(projects)
18+
})
19+
.catch(next)
20+
})
21+
22+
router.get("/:id",validateID,( (req,res, next) => {
23+
Project.get(req.params.id)
24+
.then(project => {
25+
if(!project) {
26+
res.status(404)
27+
next()
28+
}
29+
else{
30+
31+
res.status(200).json(req.project)}
32+
})
33+
.catch(next)
34+
35+
}))
36+
37+
38+
39+
router.post("/",vaildationProject,validateID, (req,res,next)=> {
40+
Project.insert(req.project)
41+
.then(project =>{
42+
res.status(200).json(project)
43+
})
44+
.catch(next)
45+
})
46+
47+
48+
router.put("/:id", vaildationProject, validateID,( (req,res,next) => {
49+
Project.update(req.params.id, req.body)
50+
.then(project => {
51+
if(!project){
52+
res.status(404)
53+
next()
54+
} else{
55+
res.status(200).json(project)
56+
}})
57+
58+
.catch(next)
59+
}))
60+
61+
router.delete("./:id", validateID,(req, res, next) => {
62+
Project.remove(req.params.id)
63+
.then(deleted => {
64+
65+
res.status(200).json(deleted)
66+
})
67+
.catch(next)
68+
})
69+
70+
router.get("/:id/actions", (req,res,next) => {
71+
const {id} = req.params;
72+
Project.getProjectActions(id)
73+
.then(actions => {
74+
res.status(200).json(actions)
75+
})
76+
.catch(next)
77+
})
78+
79+
module.exports = router

api/server.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
const express = require('express');
2+
const projectRouter = require('./projects/projects-router')
3+
const actionRouter = require('./actions/actions-router')
24
const server = express();
35

6+
server.use(express.json())
7+
8+
49
// Configure your server here
510
// Build your actions router in /api/actions/actions-router.js
611
// Build your projects router in /api/projects/projects-router.js
712
// Do NOT `server.listen()` inside this file!
813

9-
module.exports = server;
14+
server.use('/api/projects', projectRouter)
15+
server.use('/api/actions', actionRouter)
16+
17+
18+
server.use(express.json());
19+
server.use(helmet());
20+
server.use(cors());
21+
22+
23+
server.use((err, req, res, next) => {
24+
res.status(err.status || 500).json({
25+
message: err.message,
26+
27+
})
28+
})
29+
30+
module.exports = server;

0 commit comments

Comments
 (0)