GithubHelp home page GithubHelp logo

thelogan2019 / building-an-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bloominstituteoftechnology/web-sprint-challenge-build-a-web-api

0.0 0.0 0.0 154 KB

Acted as a Backend Developer to create endpoints and configure middleware to impact an existing database.

JavaScript 100.00%
api expressjs postman

building-an-api's Introduction

Sprint Challenge Instructions

Tools

  • Node >= 16.x
  • NPM >= 8.x (update NPM executing npm i -g npm)
  • Unix-like shell (Gitbash/bash/zsh)

Project Set Up

  • Fork, clone, and npm install.
  • Initialize the database of the project executing npm run resetdb.
  • Run tests locally executing npm test.

Project Instructions

Introduction

You will build an API that has Create, Read, Update and Delete (CRUD) functionality for two resources called projects and actions.

Task 1: Build 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.

  • [ g] Write an npm script named "start" that uses node to run the API server.
  • [ g] Write an npm script named "server" that uses nodemon to run the API server.
  • [ g] Install nodemon as a development dependency that would not be used in production.

Task 2: Consume Environment Variables

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

Task 3: Build 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.

Task 4: Build Middleware functions

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

Information on 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

Information on 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.
  • 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.

building-an-api's People

Contributors

ladrillo avatar thelogan2019 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.