Skip to content

Project completed #66

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

Open
wants to merge 1 commit 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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,22 @@ After finishing your required elements, you can push your work further. These go
Be prepared to demonstrate your understanding of this week's concepts by answering questions on the following topics. You might prepare by writing down your own answers before hand.

1. The core features of Node.js and Express and why they are useful.



1. Understand and explain the use of Middleware.
Middleware sits between an operating system and the applications that run on it



1. The basic principles of the REST architectural style.



1. Understand and explain the use of Express Routers.
Routing refers to how an application’s endpoints (URIs) respond to client requests. For an introduction to routing



1. Describe tooling used to manually test the correctness of an API.
API testing is a set of quality assurance actions that include sending calls to the API, getting output, and validating the system’s response against the defined input parameters
8 changes: 7 additions & 1 deletion api/actions/actions-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
// Write your "actions" router here!
// Write your "actions" router hereconst express = require("express");
const router = express.Router();
const actions = require("../helpers/actionModel");



module.exports = router;
132 changes: 131 additions & 1 deletion api/projects/projects-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,131 @@
// Write your "projects" router here!

const express = require("express");
const router = express.Router();

const projectDb = require("../helpers/projectModel");

router.get("/", (req, res) => {
projectDb
.get()
.then(project => {
res.status(200).json(project);
})
.catch(err =>
res.status(500).json({
message: "Error retrieving projects."
})
);
});

router.get("/:id", checkProjectId(), (req, res) => {
projectDb
.get(req.params.id)
.then(project => {
res.json(project);
})
.catch(err => {
res.status(500).json({
message: "Project not found."
});
});
});


router.get("/:id/actions", checkProjectId(), (req, res) => {
projectDb
.getProjectActions(req.params.id)
.then(action => {
res.json(action);
})
.catch(err => {
console.log(err);
res.status(500).json({
message: "There was an error retrieving the actions."
});
});
});


router.post("/", checkRequestBody(), (req, res) => {
projectDb
.insert(req.body)
.then(project => {
res.json(project);
})
.catch(error => {
console.log(error);
res.status(500).json({
message: "There was an error adding your project."
});
});
});


router.put("/:id", checkProjectId(), checkRequestBody(), (req, res) => {
if (req.body.completed !== true || req.body.completed !== "false") {
res.status(400).json({
message: "Please send a completed status"
});
}
if (req.body.completed === "false") {
req.body.completed = false;
}
projectDb
.update(req.params.id, req.body)
.then(project => {
res.json(project);
})
.catch(err => {
console.log(err);
res.status(500).json({
message: "There was an error updating your project"
});
});
});




function checkProjectId() {
return (req, res, next) => {
projectDb
.get(req.params.id)
.then(project => {
if (project) {
next();
} else {
res.status(404).json({
message: "The project does not exist"
});
}
})
.catch(err => {
console.log(err);
res.status(500).json({
message: "There was an error retrieving the project."
});
});
};
}

function checkRequestBody() {
return (req, res, next) => {
console.log(req.method);
if (
req.method === "POST" &&
(!req.body.name || !req.body.description)
) {
return res.status(400).json({
message: "Please provide a name and description"
});
}
if (req.method === "PUT" && !req.body.completed) {
return res.status(500).json({
message: "Please provide a completed status"
});
}
next();
};
}

module.exports = router;
15 changes: 14 additions & 1 deletion api/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
const express = require('express');
const express = require("express");
const server = express();

// Complete your server here!
// Do NOT `server.listen()` inside this file!

const projectRouter = require("./data/routers/projectsRouter");
const actionRouter = require("./data/routers/actionsRouter");

const port = 5000;

server.use(express.json());
server.use("/api/projects", projectRouter);
server.use("/api/aciotns", actionRouter);

server.get("/", (req, res) => {
res.send("Hello from Express");
});

module.exports = server;
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ I need this code, but don't know where, perhaps should make some middleware, don

Go code!
*/

const server = require("./api/server")

server.listen(port, () => {
console.log(`Server is running http://localhost:${port}`)
})
Loading