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

code review sprint challenge web api #124

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
...progress
  • Loading branch information
beefybroccoli committed Oct 29, 2021
commit 4f32cee6c6132d64f2a75ab858a4b3a3e1b982e5
16 changes: 16 additions & 0 deletions api/projects/projects-middleware.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
// add middlewares here related to projects
const Project = require("./projects-model");

async function validateId(req, res, next) {
const { id } = req.params;
const project = await Project.get(id);
if (!project) {
res.status(404).json({
message: `project ${id} not found`,
});
} else {
req.project = project;
next();
}
}

module.exports = { validateId };
12 changes: 6 additions & 6 deletions api/projects/projects-model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// DO NOT MAKE CHANGES TO THIS FILE
const db = require("../../data/dbConfig.js");
const Project = require("../../data/dbConfig.js");
const mappers = require('../../data/helpers/mappers');

module.exports = {
Expand All @@ -11,7 +11,7 @@ module.exports = {
};

function get(id) {
let query = db("projects as p");
let query = Project("projects as p");

if (id) {
query.where("p.id", id).first();
Expand All @@ -37,26 +37,26 @@ function get(id) {
}

function insert(project) {
return db("projects")
return Project("projects")
.insert(project)
.then(([id]) => get(id));
}

function update(id, changes) {
return db("projects")
return Project("projects")
.where("id", id)
.update(changes)
.then(count => (count > 0 ? get(id) : null));
}

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

function getProjectActions(projectId) {
return db("actions")
return Project("actions")
.where("project_id", projectId)
.then(actions => actions.map(action => mappers.actionToBody(action)));
}
54 changes: 43 additions & 11 deletions api/projects/projects-router.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,85 @@
// Write your "projects" router here!
const express = require("express");
const Projects = require("./projects-model");
// const {} = require("./projects-middleware");
const Project = require("./projects-model");
const { validateId } = require("./projects-middleware");
const router = express.Router();

//{name:___, description:____, completed:____}
// - [ ] `[GET] /api/projects`
// - Returns an array of projects as the body of the response.
// - If there are no projects it responds with an empty array.
router.get("/", async (req, res) => {
const projects = await Projects.get();
res.status(200).json({ projects });
router.get("/", async (req, res, next) => {
try {
const projects = await Project.get();
res.status(200).json(projects ? [...projects] : []);
} catch (err) {
next(err);
}
});

// - [ ] `[GET] /api/projects/:id`
// - Returns a project with the given `id` as the body of the response.
// - If there is no project with the given `id` it responds with a status code 404.
router.get("/:id", async (req, res) => {
res.status(200).json({ message: `[GET] /api/projects/${req.params.id}` });
router.get("/:id", validateId, async (req, res, next) => {
try {
res.status(200).json({ ...req.project });
} catch (err) {
next(err);
}
});

// - [ ] `[POST] /api/projects`
// - Returns the newly created project as the body of the response.
// - If the request body is missing any of the required fields it responds with a status code 400.
router.post("/", async (req, res) => {
router.post("/", async (req, res, next) => {
try {
} catch (err) {
next(err);
}
res.status(201).json({ message: `[POST] /api/projects/` });
});

// - [ ] `[PUT] /api/projects/:id`
// - Returns the updated project as the body of the response.
// - If there is no project with the given `id` it responds with a status code 404.
// - If the request body is missing any of the required fields it responds with a status code 400.
router.put("/:id", async (req, res) => {
router.put("/:id", async (req, res, next) => {
try {
} catch (err) {
next(err);
}
res.status(201).json({ message: `[PUT] /api/projects/${req.params.id}` });
});

// - [ ] `[DELETE] /api/projects/:id`
// - Returns no response body.
// - If there is no project with the given `id` it responds with a status code 404.
router.delete("/:id", async (req, res) => {
router.delete("/:id", async (req, res, next) => {
try {
} catch (err) {
next(err);
}
res.status(201).json({ message: `[DELETE] /api/projects/${req.params.id}` });
});

// - [ ] `[GET] /api/projects/:id/actions`
// - Returns an array of actions (could be empty) belonging to a project with the given `id`.
// - If there is no project with the given `id` it responds with a status code 404.
router.get("/:id/actions", async (req, res) => {
router.get("/:id/actions", async (req, res, next) => {
try {
} catch (err) {
next(err);
}
res
.status(200)
.json({ message: `[GET] /api/projects/${req.params.id}/actions` });
});

router.use((err, req, res, next) => {
res.status(err.status || 500).json({
custMessage: "Error Occured retrieving /api/actions path",
message: err.message,
});
});

module.exports = router;