Code Monkey home page Code Monkey logo

entertainment-packages-api-nodejs's Introduction

NodeJS PostgreSQL API

Entertainment Packages API Exercise

Author: Nico Rithner

Summary

Create a REST API using Node.js and PostgreSQL. Seed the database from the data provided as JSON and expose REST APIs with some specific filtering requirements.

Table of Contents

Requirements

  • REST API using Node / Express API with PostgreSQL Database

  • README with clear instructions on how to run and use the application

  • Seed script that seeds the PostgreSQL Database with the provided data

  • CRUD APIs for each entity

  • GET Shows and filter by Network (ex: /shows?network_id=1)

  • GET Shows and filter by Package (ex: /shows?package_id=1)

  • GET Package by ID should also provide all Networks included in that Package (ex: /packages/:id)

Stack

  • NodeJS
  • ExpressJS
  • PostgreSQL
  • Sequelize

Schema

The project includes three main tables and a through-table.

Based on the data provided as json:

  • A 'package' has many networks.
  • A 'network' belongs to many packages.
  • A 'network' has mnay shows.
  • A 'show' belongs to a network.

To avoid nesting packages and network are associated through a join table named 'packageNetworks'. A 'packageNetworks' table has both a package and a network foreign key. A 'show' belongs to a newtork only so there is a reference to network in 'show' with a foreing key.

Schema Diagram

Database schema

Installation

  1. Make sure you have yarn installed
  2. Clone this repo git clone [email protected]:Coding-Gymnasium/node-express-api-2023.git
  3. Cd to project and run yarn install
  4. Set psql user:
        $ psql
        <default-user>=# CREATE USER postgres WITH PASSWORD "postgres";
        <default-user>=# GRANT ALL PRIVILEGES ON DATABASE node_express_api_dev TO postgres;
  5. Create database npx sequelize-cli db:create
  6. Migrate tables yarn migrate:up
  7. Seed tables. Note: because of the associations the tables need to be seed in order. Please use the following command: yarn seed-in-order
  8. Run project yarn start

Endpoints

Postman

Using Postman try the following endpoints

Run in Postman

Sample Endpoints

View All Packages
Image of api call using Postman
View Shows by Package
Image of api call using Postman
View Shows by Package
Image of api call using Postman
View Networks by Package
Image of api call using Postman

List of Endpoints


Packages

Create a Package

POST ➡️ http://localhost:8000/api/v1/packages/

{
  "name": "Gold"
}
Get All Packages

GET ➡️ http://localhost:8000/api/v1/packages/

Get Package by ID

GET ➡️ http://localhost:8000/api/v1/packages/1

  • Displays Networks associated with package
Update Package by ID

PUT ➡️ http://localhost:8000/api/v1/packages/1

Change price

{
  "price": 19.9
}
Delete Package by ID

DELETE ➡️ http://localhost:8000/api/v1/packages/1

Delete ALL Packages

DELETE ➡️ http://localhost:8000/api/v1/packages


Networks

Create a Network

POST ➡️ http://localhost:8000/api/v1/networks/

{
  "name": "ABC"
}
Get All Networks

GET ➡️ http://localhost:8000/api/v1/networks/

Get Network by ID

GET ➡️ http://localhost:8000/api/v1/networks/1

Update Network by ID

PUT ➡️ http://localhost:8000/api/v1/networks/1

Change name

{
  "name": "CBS"
}
Delete Network by ID

DELETE ➡️ http://localhost:8000/api/v1/networks/1

Delete ALL Networks

DELETE ➡️ http://localhost:8000/api/v1/networks


Shows

Create a Show

POST ➡️ http://localhost:8000/api/v1/shows/

{
  "title": "American Gods",
  "imdb_rating": 7.7
}
Get All Shows

GET ➡️ http://localhost:8000/api/v1/shows/

Get Associated Shows by Package Id

GET ➡️ http://localhost:8000/api/v1/shows?package_id=1

Get Associated Shows by Networks Id

GET ➡️ http://localhost:8000/api/v1/shows?network_id=2

Get Show by ID

GET ➡️ http://localhost:8000/api/v1/shows/1

Update Show by ID

PUT ➡️ http://localhost:8000/api/v1/shows/1

Change title

{
  "title": "Forge in Fire"
}

Change network

{
  "NetworkId": 2
}
Delete Show by ID

DELETE ➡️ http://localhost:8000/api/v1/shows/1

Delete ALL Shows

DELETE ➡️ http://localhost:8000/api/v1/shows


Connect Network to Package

Create a PackageNetwork

POST ➡️ http://localhost:8000/api/v1/packageNetworks/

{
  "NetworkId": 1,
  "PackageId": 2
}
Get All PackageNetworks

GET ➡️ http://localhost:8000/api/v1/packageNetworks/

Get PackageNetwork by ID

GET ➡️ http://localhost:8000/api/v1/packageNetworks/1

Update PackageNetwork by ID

PUT ➡️ http://localhost:8000/api/v1/packageNetworks/1

{
  "id": 4
}
Delete PackageNetwork by ID

DELETE ➡️ http://localhost:8000/api/v1/packageNetworks/1

Delete ALL PackageNetworks

DELETE ➡️ http://localhost:8000/api/v1/packageNetworks


Swagger UI

To use the swagger ui please navigate to:

http://localhost:8000/api-docs

Screenshot of app's swagger ui

entertainment-packages-api-nodejs's People

Contributors

nicorithner avatar

entertainment-packages-api-nodejs's Issues

Add Tests

Tests needed

Note:

I attempted to set up Jest and Supertest I had problems setting it up to hit the endpoints.
Need to spend more time on the documentation learning how to work it with sequelize

Current seeding causes a validation error

After seeding the database and a new user or package is created I get a validation error.

I think it has to do with the way I set up seeding Networks and Packages with ids.

When creating a new network or package with sequelize it provides an id without taking in consideration the ones seeded and because the id is not unique this causes and error.

I need to investigate farther and adjust the seeding method.

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.