/play-api

Express back-end API that supports Play-JS front-end.

Primary LanguageJavaScript

Play - Back End API Endpoint

Live Link

Table of Contents

Description/Purpose

This project is a back-end service that drives a separate front-end application. It includes a database service that tracks songs that users have favorited through the front-end application. Users can also create playlists and add their favorite songs to a playlist. Song metadata comes from the MusixMatch API.

System Requirements

This application requires several technologies to be installed to your local machine. The following are required:

  1. NodeJS
  2. PostgreSQL

Setup

To install this application locally, take the following steps:

  1. Run git clone git@github.com:DanielMulitauopele/play-api.git in your terminal
  2. Navigate to the newly created project directory
  3. Run npm install in your terminal to install the required node modules
  4. In your terminal run psql to launch the PostgreSQL CLI
  5. Run CREATE DATABASE play; for the development database, and CREATE DATABASE play_test; for the test database.
  6. Run knex migrate:latest to run the necessary migrations to get the tables up and running.
  7. Run npm test to start the test suite.
  8. Run npm start to start the server to view the application endpoints at http://localhost:3000.

Dependencies

Production

Development

Contributors

This project was developed, tested, and published by Mike McKee and Daniel Mulitauopele.

API Endpoints

There are two main endpoints for interacting with this API.

Songs Endpoints

GET /api/v1/songs

Returns all songs that have been favorited and added to the database.

Request

GET /api/v1/songs

Response

[
    {
        "id": 1,
        "name": "We Are The Champions",
        "artist_name": "Queen",
        "genre": "Rock",
        "song_rating": 84
    },
    {
        "id": 2,
        "name": "Sandstorm",
        "artist_name": "Darude",
        "genre": "Electronic",
        "song_rating": 82
    }
]

GET /api/v1/songs/:id

Returns a song with the specific given :id.

Request

GET /api/v1/songs/1

Response

{
    "id": 1,
    "name": "We Are The Champions",
    "artist_name": "Queen",
    "genre": "Rock",
    "song_rating": 84
}

If no song is found with given ID, the following is returned with a status 404.

{
    "error": "Could not find song with id: 35"
}

POST /api/v1/songs

Adds a song to the database with given parameters.

Request

POST /api/v1/songs

Content-Type: application/json
Body:
{ "name":"We Will Rock You", "artist_name": "Queen", "genre":"Rock", "song_rating":"90" }

Response

{
    "songs": {
        "id": 22,
        "name": "We Will Rock You",
        "artist_name": "Queen",
        "genre": "Rock",
        "song_rating": 90
    }
}

All Fields Are Required! Song rating must be between 1 and 100.

If any fields are not given (artist name in this case), a 400 status code is returned with the following response.

{
    "error": "Expected format: { name: <String>, artist_name: <String>, genre: <String>, song_rating: <Integer> }. You're missing a "artist_name" property."
}

PATCH /api/v1/songs/:id

Update a specific song with the given ID. ID field is required. You can specify all fields or just one.

Request

PATCH /api/v1/songs/1

Content-Type: application/json
Body:
{ "name":"We Will Rock Youuuu" }

Response

{
    "songs": {
        "id": 17,
        "name": "We Will Rock Youuuu",
        "artist_name": "Queen",
        "genre": "Rock",
        "song_rating": 90
    }
}

If song with the given ID is not found, returns a 404 error with the following response.

{
    "error": "Song with ID 500 not found"
}

DELETE /api/v1/songs/:id

Deletes the specific song with the given ID. ID field is required.

*Request

DELETE /api/v1/songs/1

Response

204 No Content

If song with the given ID is not found, returns a 404 error with the following response.

{
    "error": "Song with ID 500 not found"
}

Playlists Endpoints

GET /api/v1/playlists

Retrieves all playlists with their songs if applicable.

Request

GET /api/v1/playlists

Response

[
    {
        "id": 1,
        "playlist_name": "Super Cool Fun Jams",
        "songs": [
            {
                "id": 1,
                "name": "We Will Rock You",
                "artist_name": "Queen",
                "genre": "Rock",
                "song_rating": 82
            },
            {
                "id": 3,
                "name": "Le Freak",
                "artist_name": "Chic",
                "genre": "Disco/Pop",
                "song_rating": 82
            }
        ]
    },
    {
        "id": 2,
        "playlist_name": "Summer Mix",
        "songs": [
            {
                "id": 5,
                "name": "Summertime",
                "artist_name": "Childish Gambino",
                "genre": "Rap",
                "song_rating": 77
            }
        ]
    }
]

POST /api/v1/playlists

Adds a new playlist to the database. Field "playlist_name" is the only requirement.

Request

POST /api/v1/playlists

Content-Type: application/json
Body:
{ "playlist_name":"Summer Mix" }

Response

{
    "playlist": {
        "id": 4,
        "playlist_name": "Summer Mix"
    }
}

If no field "playlist_name" is given, returns a status 400 error.

{
    "error": "Expected format: { playlist_name: <String> }."
}

GET /api/v1/playlists/:playlist_id/songs

Retrieves a specific playlist with given :playlist_id and its songs.

Request

GET /api/v1/playlists/1/songs

Response

{
    "id": 1,
    "playlist_name": "Super Cool Fun Jams",
    "songs":[
        {
            "id": 1,
            "name": "We Will Rock You",
            "artist_name": "Queen",
            "genre": "Rock",
            "song_rating": 82
        },
        {
            "id": 3,
            "name": "Le Freak",
            "artist_name": "Chic",
            "genre": "Disco/Pop",
            "song_rating": 82
        }
    ]
}

If given :playlist_id is not found, a 404 error is returned with the following response.

{
    "error": "Playlist with ID 500 does not exist"
}

POST /api/v1/playlists/:playlist_id/songs/:id

Adds a given song with :id to playlist with given :playlist_id.

Request

POST /api/v1/playlists/1/songs/1

Response

{
    "message": "Successfully added Who Let The Dogs Out? to Super Cool Fun Jams"
}

If song ID is not found or playlist ID is not found, a 404 status code is returned with the following responses.

{
    "error": "Song with ID 500 does not exist"
}
{
    "error": "Playlist with ID 500 does not exist"
}

DELETE /api/v1/playlists/:playlist_id/songs/:id

Deletes a given song with :id from playlist with given :playlist_id.

Request

DELETE /api/v1/playlists/1/songs/1

Response

{
    "message": "Successfully removed Who Let The Dogs Out? from Super Cool Fun Jams"
}

If song ID is not found or playlist ID is not found, a 404 status code is returned with the following responses.

{
    "error": "Song with ID 500 does not exist"
}
{
    "error": "Playlist with ID 500 does not exist"
}