Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladrillo committed Mar 1, 2022
0 parents commit 52e37bf
Show file tree
Hide file tree
Showing 24 changed files with 10,617 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"env": {
"commonjs": true,
"es2021": true,
"node": true,
"jest": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": "latest"
},
"rules": {
}
}
36 changes: 36 additions & 0 deletions .github/lockdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# PLEASE DO NOT REMOVE THIS FILE

# Configuration for Repo Lockdown - https://github.com/dessant/repo-lockdown

# Skip issues and pull requests created before a given timestamp. Timestamp must
# follow ISO 8601 (`YYYY-MM-DD`). Set to `false` to disable
skipCreatedBefore: false

# Issues and pull requests with these labels will be ignored. Set to `[]` to disable
exemptLabels: []

# Comment to post before closing or locking. Set to `false` to disable
comment: false

# Label to add before closing or locking. Set to `false` to disable
label: false

# Close issues and pull requests
close: true

# Lock issues and pull requests
lock: true

# Limit to only `issues` or `pulls`
only: pulls
# Optionally, specify configuration settings just for `issues` or `pulls`
# issues:
# label: wontfix

# pulls:
# comment: >
# This repository does not accept pull requests, see the README for details.
# lock: false

# Repository to extend settings from
# _extends: repo
93 changes: 93 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Mac files
.DS_Store

# VSCode settings
.vscode/

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# databases
*.db3
153 changes: 153 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Sprint Challenge Instructions

**Read these instructions carefully. Understand exactly what is expected _before_ starting this Sprint Challenge.**

This challenge allows you to practice the concepts and techniques learned over the past sprint and apply them in a concrete project. This sprint explored **how to build web services based on the REST (REpresentational State Transfer) architectural style**. During this sprint, you studied **Node.js and Express, server side routing, how to write Express middleware and how to deploy an API to Heroku**.

In your challenge this week, you will demonstrate your mastery of these skills by designing and creating a web API to manage the following resources: `Projects` and `Actions`.

This is an individual assessment. All work must be your own. All projects will be submitted to Codegrade for automated review. You will also be given feedback by code reviewers the Monday after challenge submissions. For more information on the review process [click here](https://www.notion.so/bloomtech/How-to-View-Feedback-in-CodeGrade-c5147cee220c4044a25de28bcb6bb54a).

You are not allowed to collaborate during the sprint challenge.

## Introduction

In meeting the minimum viable product (MVP) specifications listed below, your project should provide an API that has Create, Read, Update and Delete (CRUD) functionality for both `projects` and `actions`.

## Instructions

### Task 1: Project Set Up

- [ ] Run `npm install` to install your dependencies.
- [ ] Run tests locally executing `npm test`.
- [ ] Reset the database to its original state executing `npm run resetdb`.

### Task 2: Project Requirements (MVP)

Your finished project must include all of the following requirements:

#### NPM Scripts

A _"test"_ script already exists you can use to run tests against your code.
A _"resetdb"_ script exists that allows you to reset the database to its original state.

- [ ] Write an _npm script_ named _"start"_ that uses `node` to run the API server.
- [ ] Write an _npm script_ named _"server"_ that uses `nodemon` to run the API server.
- [ ] Install _nodemon_ as a development dependency that would not be used in production.

#### Environment Variables

- [ ] Bring the port number from the `process.env` variable, falling back to `9000` if `process.env.PORT` is undefined **!!!**

#### Endpoints

Inside `api/projects/projects-router.js` build the following endpoints:

- [ ] `[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.
- [ ] `[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.
- [ ] `[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.
- [ ] `[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.
- [ ] `[DELETE] /api/projects/:id`
- Returns no response body.
- If there is no project with the given `id` it responds with a status code 404.
- [ ] `[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.

Inside `api/actions/actions-router.js` build endpoints for performing CRUD operations on _actions_:

- [ ] `[GET] /api/actions`
- Returns an array of actions (or an empty array) as the body of the response.
- [ ] `[GET] /api/actions/:id`
- Returns an action with the given `id` as the body of the response.
- If there is no action with the given `id` it responds with a status code 404.
- [ ] `[POST] /api/actions`
- Returns the newly created action as the body of the response.
- If the request body is missing any of the required fields it responds with a status code 400.
- When adding an action make sure the `project_id` provided belongs to an existing `project`.
- [ ] `[PUT] /api/actions/:id`
- Returns the updated action as the body of the response.
- If there is no action 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.
- [ ] `[DELETE] /api/actions/:id`
- Returns no response body.
- If there is no action with the given `id` it responds with a status code 404.

#### Middleware functions

- [ ] Write at least two middleware functions for this API, and consume them in the proper places of your code.

### Database Schemas

The description of the structure and extra information about each _resource_ stored in the included database (`./data/database.db3`) is listed below.

#### 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 |

### Database Persistence Helpers

The project includes models you can use to manage the persistence of _project_ and _action_ data. These files are `api/projects/projects-model.js` and `api/actions/actions-model.js`. Both files publish the following api, which you can use to store, modify and retrieve each resource:

**All these helper methods return a promise. Remember to use .then().catch() or async/await.**

- `get()`: resolves to an array of all the resources contained in the database. If you pass an `id` to this method it will return the resource with that id if one is found.
- `insert()`: calling insert passing it a resource object will add it to the database and return the newly created resource.
- `update()`: accepts two arguments, the first is the `id` of the resource to update, and the second is an object with the `changes` to apply. It returns the updated resource. If a resource with the provided `id` is not found, the method returns `null`.
- `remove()`: the remove method accepts an `id` as its first parameter and, upon successfully deleting the resource from the database, returns the number of records deleted.

The `projects-model.js` includes an extra method called `getProjectActions()` that takes a _project id_ as its only argument and returns a list of all the _actions_ for the _project_.

We have provided test data for all the resources.

**Important Notes:**

- Do not make changes to your `package.json` except to add **additional** dependencies and scripts. Do not update existing packages.
- Your app must be able to run in Node v.12. Do not use newer features of Node (e.g.: optional chaining and nullish coalescing NOT supported).
- Use an HTTP client like `HTTPie`, `Postman` or `Insomnia` to manually test the API's endpoints.
- Use Express Routers to organize your endpoints.
- Even though you are only required to write two middleware functions, it is advised that you leverage middlewares as much as possible.
- You are welcome to create additional files, but **do not move or rename existing files** or folders.
- In your solution, it is essential that you follow best practices and produce clean and professional results.
- Schedule time to review, refine, and assess your work and perform basic professional polishing including spell-checking and grammar-checking on your work.

## Submission format

- [ ] Submit via Codegrade by pushing commits to your `main` branch.
- [ ] Check Codegrade before the deadline to compare its results against your local tests.
- [ ] Check Codegrade on the days following the Sprint Challenge for reviewer feedback.
- [ ] New commits will be evaluated by Codegrade if pushed _before_ the sprint challenge deadline.

## Interview Questions

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.
1. The basic principles of the REST architectural style.
1. Understand and explain the use of Express Routers.
1. Describe tooling used to manually test the correctness of an API.
1 change: 1 addition & 0 deletions api/actions/actions-middlware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// add middlewares here related to actions
48 changes: 48 additions & 0 deletions api/actions/actions-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// DO NOT MAKE CHANGES TO THIS FILE
const db = require('../../data/dbConfig.js');
const mappers = require('../../data/helpers/mappers');

module.exports = {
get,
insert,
update,
remove,
};

function get(id) {
let query = db('actions');

if (id) {
return query
.where('id', id)
.first()
.then((action) => {
if (action) {
return mappers.actionToBody(action);
} else {
return null;
}
});
} else {
return query.then((actions) => {
return actions.map((action) => mappers.actionToBody(action));
});
}
}

function insert(action) {
return db('actions')
.insert(action)
.then(([id]) => get(id));
}

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

function remove(id) {
return db('actions').where('id', id).del();
}
1 change: 1 addition & 0 deletions api/actions/actions-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Write your "actions" router here!
1 change: 1 addition & 0 deletions api/projects/projects-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// add middlewares here related to projects
Loading

0 comments on commit 52e37bf

Please sign in to comment.