Skip to content

Stan tudor #103

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 5 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
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ In meeting the minimum viable product (MVP) specifications listed below, your pr

### Task 1: Project Set Up

- [ ] Fork and clone this repository. **If you are repeating this Course, delete your old fork from Github and re-fork and re-clone.**
- [ ] Create a new branch: `git checkout -b <firstName-lastName>`.
- [ ] Implement the project on your newly created branch, committing changes regularly.
- [ ] Push commits: `git push origin <firstName-lastName>`.
- [ ] **RUN** `npm install` to install your dependencies.
- [x] Fork and clone this repository. **If you are repeating this Course, delete your old fork from Github and re-fork and re-clone.**
- [x] Create a new branch: `git checkout -b <firstName-lastName>`.
- [x] Implement the project on your newly created branch, committing changes regularly.
- [x] Push commits: `git push origin <firstName-lastName>`.
- [x] **RUN** `npm install` to install your dependencies.

### Task 2: CodeGrade Setup

- [ ] Follow [instructions](https://www.notion.so/lambdaschool/Submitting-an-assignment-via-Code-Grade-A-Step-by-Step-Walkthrough-07bd65f5f8364e709ecb5064735ce374) to set up Codegrade's Webhook and Deploy Key, making sure your deployment is set to your `<firstName-lastName>` branch.
- [ ] Make a commit and push it to Github.
- [ ] Check to see that Codegrade has accepted your git submission.
- [x] Follow [instructions](https://www.notion.so/lambdaschool/Submitting-an-assignment-via-Code-Grade-A-Step-by-Step-Walkthrough-07bd65f5f8364e709ecb5064735ce374) to set up Codegrade's Webhook and Deploy Key, making sure your deployment is set to your `<firstName-lastName>` branch.
- [x] Make a commit and push it to Github.
- [x] Check to see that Codegrade has accepted your git submission.

### Task 3: Project Requirements (MVP)

Expand Down Expand Up @@ -100,22 +100,22 @@ The description of the structure and extra information about each _resource_ sto

#### Projects

| Field | Data Type | Metadata |
| ----------- | --------- | --------------------------------------------------------------------------- |
| id | number | do not provide it when creating projects, the database will generate it |
| name | string | required |
| description | string | required |
| completed | boolean | not required, defaults to false when creating projects |
| Field | Data Type | Metadata |
| ----------- | --------- | ----------------------------------------------------------------------- |
| id | number | do not provide it when creating projects, the database will generate it |
| name | string | required |
| description | string | required |
| completed | boolean | not required, defaults to false when creating projects |

#### Actions

| Field | Data Type | Metadata |
| ----------- | --------- | ------------------------------------------------------------------------------------------------ |
| id | number | do not provide it when creating actions, the database will generate it |
| project_id | number | required, must be the id of an existing project |
| description | string | required, up to 128 characters long |
| notes | string | required, no size limit. Used to record additional notes or requirements to complete the action |
| completed | boolean | not required, defaults to false when creating actions |
| Field | Data Type | Metadata |
| ----------- | --------- | ----------------------------------------------------------------------------------------------- |
| id | number | do not provide it when creating actions, the database will generate it |
| project_id | number | required, must be the id of an existing project |
| description | string | required, up to 128 characters long |
| notes | string | required, no size limit. Used to record additional notes or requirements to complete the action |
| completed | boolean | not required, defaults to false when creating actions |

### Database Persistence Helpers

Expand Down
48 changes: 48 additions & 0 deletions api/actions/actions-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const Action = require("../actions/actions-model");

const validatePost = (req, res, next) => {
if (!req.body.notes || !req.body.description || !req.body.project_id) {
next({
status: 400,
message: "missing required field",
});
} else {
next();
}
};

const validateActionId = async (req, res, next) => {
try {
const { id } = req.params;
const action = await Action.get(id);
console.log(action);
if (action) {
req.action = action;
next();
} else {
next({
status: 404,
message: "action not found",
});
}
} catch (err) {
next(err);
}
};

const validateAction = async (req, res, next) => {
if (!req.body.notes || !req.body.description || !req.body.project_id) {
next({
status: 400,
message: "missing required field",
});
} else {
next();
}
};

module.exports = {
validatePost,
validateActionId,
validateAction,
};
1 change: 0 additions & 1 deletion api/actions/actions-middlware.js

This file was deleted.

50 changes: 49 additions & 1 deletion api/actions/actions-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
// Write your "actions" router here!
const express = require("express");

const {
validatePost,
validateActionId,
validateAction,
} = require("../actions/actions-middleware");

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

const router = express.Router();

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

router.get("/:id", validateActionId, (req, res) => {
res.json(req.action);
});

router.post("/", validatePost, (req, res, next) => {
Action.insert(req.body)
.then((action) => {
res.status(201).json(action);
})
.catch(next);
});

router.put("/:id", validateActionId, validateAction, (req, res, next) => {
Action.update(req.params.id, req.body)
.then((action) => {
res.status(200).json(action);
})
.catch(next);
});

router.delete("/:id", validateActionId, (req, res, next) => {
Action.remove(req.params.id)
.then((action) => { // eslint-disable-line
res.json(req.action);
})
.catch(next);
});

module.exports = router;
42 changes: 41 additions & 1 deletion api/projects/projects-middleware.js
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
// add middlewares here related to projects
const Project = require("../projects/projects-model");

async function validateProjectId(req, res, next) {
try {
const { id } = req.params;
const project = await Project.get(id);
if (project) {
req.project = project;
next();
} else {
next({
status: 404,
message: "project not found",
});
}
} catch (err) {
next(err);
}
}

async function validateProject(req, res, next) {
const { name, description, completed } = req.body;
if (!name || !name.trim()) {
res.status(400).json({
message: "missing required name field",
});
} else if (!description || !description.trim()) {
res.status(400).json({
message: "missing required description field",
});
} else {
req.name = name.trim();
req.description = description.trim();
req.completed = completed;
next();
}
}
module.exports = {
validateProjectId,
validateProject,
};
16 changes: 7 additions & 9 deletions api/projects/projects-model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// DO NOT MAKE CHANGES TO THIS FILE
const db = require("../../data/dbConfig.js");
const mappers = require('../../data/helpers/mappers');
const mappers = require("../../data/helpers/mappers");

module.exports = {
get,
Expand All @@ -18,7 +18,7 @@ function get(id) {

const promises = [query, getProjectActions(id)]; // [ projects, actions ]

return Promise.all(promises).then(function(results) {
return Promise.all(promises).then(function (results) {
let [project, actions] = results;

if (project) {
Expand All @@ -30,8 +30,8 @@ function get(id) {
}
});
} else {
return query.then(projects => {
return projects.map(project => mappers.projectToBody(project));
return query.then((projects) => {
return projects.map((project) => mappers.projectToBody(project));
});
}
}
Expand All @@ -46,17 +46,15 @@ function update(id, changes) {
return db("projects")
.where("id", id)
.update(changes)
.then(count => (count > 0 ? get(id) : null));
.then((count) => (count > 0 ? get(id) : null));
}

function remove(id) {
return db("projects")
.where("id", id)
.del();
return db("projects").where("id", id).del();
}

function getProjectActions(projectId) {
return db("actions")
.where("project_id", projectId)
.then(actions => actions.map(action => mappers.actionToBody(action)));
.then((actions) => actions.map((action) => mappers.actionToBody(action)));
}
67 changes: 66 additions & 1 deletion api/projects/projects-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
// Write your "projects" router here!
const express = require("express");
const Project = require("./projects-model");
const { validateProjectId, validateProject } = require("./projects-middleware");

const router = express.Router();

router.get("/", async (req, res, next) => {
try {
const users = await Project.get();
res.status(200).json(users);
} catch (error) {
next(error);
}
});

router.get("/:id", validateProjectId, (req, res, next) => {
try {
res.json(req.project);
} catch (error) {
next(error);
}
});

router.post("/", validateProject, async (req, res, next) => {
try {
const newProject = await Project.insert({
name: req.name,
description: req.description,
completed: req.completed,
});
res.status(201).json(newProject);
} catch (error) {
next(error);
}
});

router.put("/:id", validateProjectId, validateProject, (req, res, next) => {
Project.update(req.params.id, req.body)
.then((project) => {
res.status(400).json(project);
})
.catch(next);
});

router.delete("/:id", validateProjectId, async (req, res, next) => {
try {
await Project.remove(req.params.id);
res.json(res.Project);
} catch (error) {
next(error);
}
});

router.get("/:id/actions", validateProjectId, async (req, res, next) => {
Project.getProjectActions(req.params.id)
.then((actions) => {
if (actions.length > 0) {
res.status(200).json(actions);
} else {
res.status(404).json(actions);
}
})
.catch(next);
});

module.exports = router;
38 changes: 34 additions & 4 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
const express = require('express');
/* eslint-disable */
const express = require("express");
const server = express();

// 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!
server.use(express.json());

const actionsRouter = require("./actions/actions-router");
const projectsRouter = require("./projects/projects-router");

const { logger, errorHandling } = require("../middleware/middleware");

server.use("/api/actions", actionsRouter);
server.use("/api/projects", projectsRouter);

server.use("/", logger);
server.use(errorHandling);

server.get("/", (req, res) => {
res.send(`Server is up and running!`);
});
server.get("/api", (req, res) => {
res.json({ message: "api is working" });
});
server.get("/api/projects", (req, res) => {
res.json({ message: "projects here" });
});
server.get("/api/actions", (req, res) => {
res.json({ message: "actions here" });
});
server.use((err, req, res, next) => {
// eslint-disable-line
res.status(500).json({ message: err.message, stack: err.stack });
});
server.use(function (req, res) {
res.status(404).send("Page not found");
});

module.exports = server;
Binary file modified data/lambda.db3
Binary file not shown.
17 changes: 6 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
/*
play this: https://www.youtube.com/watch?v=d-diB65scQU
require("dotenv").config();

Sing along:
const server = require("./api/server");

here's a little code I wrote, please read the README word for word, don't worry, you got this
in every task there may be trouble, but if you worry you make it double, don't worry, you got this
ain't got no sense of what is REST? just concentrate on learning Express, don't worry, you got this
your file is getting way too big, bring a Router and make it thin, don't worry, be crafty
there is no data on that route, just write some code, you'll sort it out… don't worry, just hack it…
I need this code, but don't know where, perhaps should make some middleware, don't worry, just hack it
const port = process.env.PORT || 5000;

Pull your server into this file and start it!
*/
server.listen(port, () => {
console.log(`\n*** Server running on ${port} ***\n`);
});
Loading