Skip to content

All tests passing. #72

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 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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,16 @@ 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.
Node.js allows us to write servers using JavaScript, so we can use the same language for frontend and backend. Express gives us extra functionality such as parsing responses to JSON automatically or allowing us to use or write middleware.

1. Understand and explain the use of Middleware.
Middleware is code that is executed when an endpoint is called but before the code written in the endpoint itself. It can be used to log out information about an endpoint, verify data, and is a great way to keep our code DRY.

1. The basic principles of the REST architectural style.
RESTful api's are very browser friendly. They don't require the frontend to do anything that it hasn't already provided instructions and means to do.

1. Understand and explain the use of Express Routers.
Express routers are used to organize our code so we don't have all of our endpoints in the server.js file. We can use it to separate endpoints for different databases into their own folder and import them to server.js so they can still be used.

1. Describe tooling used to manually test the correctness of an API.
Tools such as Postman or Insomnia have an interface with all the possible api calls built in. You select one and insert the url and you can see how your server handles the call. They have additional fuctionallity for post or put requests so you can add or update a data body.
80 changes: 80 additions & 0 deletions api/actions/actions-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,81 @@
// Write your "actions" router here!
const express = require('express');
const router = express.Router();
const Actions = require('./actions-model');

// endpoints

// get
router.get('/api/actions', async (req, res) => {
try {
const actions = await Actions.get();
res.status(200).json(actions);
} catch (err) {
res.status(500).json({ Error: {err} });
}
});

router.get('/api/actions/:id', async (req, res) => {
const {id} = req.params;

try {
const action = await Actions.get(id);
if (!action) {
res.status(404).json({ message: "The action with the specified id does not exist" });
} else {
res.status(200).json(action);
}
} catch (err) {
res.status(500).json({ Error: {err} });
}
});

// post
router.post('/api/actions', async (req, res) => {
const body = req.body;
if (!body.description || !body.notes) {
res.status(400).json({ message: "please fill in all required fields" });
} else {
try {
const newAction = await Actions.insert(body);
res.status(201).json(newAction);
} catch (err) {
res.status(500).json({ Error: {err} });
}
}
});

// put
router.put('/api/actions/:id', async (req, res) => {
const {id} = req.params;
const body = req.body;

if (!body.description && !body.notes) {
res.status(400).json({ message: "please fill in all the required fields" });
} else {
try {
const updatedAction = await Actions.update(id, body);
res.status(200).json(updatedAction);
} catch (err) {
res.status(500).json({ Error: {err} });
}
};
});

// delete
router.delete('/api/actions/:id', async (req, res) => {
const {id} = req.params;

try {
const deletedAction = await Actions.remove(id);
if (!deletedAction) {
res.status(404).json({ message: "The action with the specified id does not exist" });
} else {
res.status(200).json(deletedAction);
}
} catch (err) {
res.status(500).json({ Error: {err} });
}
});

module.exports = router;
92 changes: 92 additions & 0 deletions api/projects/projects-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,93 @@
// Write your "projects" router here!
const express = require('express');
const router = express.Router();
const Projects = require('./projects-model');

// endpoints

// get
router.get('/api/projects', async (req, res) => {
try {
const projects = await Projects.get();
res.status(200).json(projects);
} catch (err) {
res.status(500).json({ Error: {err} });
}
});

router.get('/api/projects/:id', async (req, res) => {
const {id} = req.params;

try {
const project = await Projects.get(id);
if (!project) {
res.status(404).json({ message: "The project with the specified id does not exist" });
} else {
res.status(200).json(project);
}
} catch (err) {
res.status(500).json({ Error: {err} });
}
});

router.get('/api/projects/:id/actions', async (req, res) => {
const {id} = req.params;

try {
const projectActions = await Projects.getProjectActions(id);
res.status(200).json(projectActions);
} catch (err) {
res.status(500).json({ Error: {err} });
}
});

// post
router.post('/api/projects', async (req, res) => {
const body = req.body;

if (!body.name || !body.description) {
res.status(400).json({ message: "Please provide name and description" });
} else {
try {
const newProject = await Projects.insert(body);
res.status(201).json(newProject);
} catch (err) {
res.status(500).json({ Error: {err} });
}
}
});

// put
router.put('/api/projects/:id', async (req, res) => {
const {id} = req.params;
const body = req.body;

if (!body.name && !body.description) {
res.status(400).json({ message: "Please fill out the required fields" });
} else {
try {
const updatedProject = await Projects.update(id, body);
res.status(200).json(updatedProject);
} catch (err) {
res.status(500).json({ Error: {err} });
}
}
});

// delete
router.delete('/api/projects/:id', async (req, res) => {
const {id} = req.params;

try {
const deletedProject = await Projects.remove(id);
if (!deletedProject) {
res.status(404).json({ message: "The project with the given id does not exist" });
} else {
res.status(200).json(deletedProject);
}
} catch (err) {
res.status(500).json({ Error: {err} });
}
});

module.exports = router;
6 changes: 6 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@ const server = express();

// Complete your server here!
// Do NOT `server.listen()` inside this file!
const actionsRouter = require('./actions/actions-router');
const projectsRouter = require('./projects/projects-router');

server.use(express.json());
server.use(actionsRouter);
server.use(projectsRouter);

module.exports = server;
Binary file modified data/lambda.db3
Binary file not shown.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ I need this code, but don't know where, perhaps should make some middleware, don

Go code!
*/
const server = require('./api/server');

const port = 5000;

server.listen(port, () => {
console.log(`\n *** server listening on port ${port} *** \n`);
});
Loading