Git Product home page Git Product logo

song-tag's Introduction

song-tag

A mini project to practice relationships in Mongo.

##Objectives You'll create a simple set of schema revolving around Artists, their Songs, and a set of Tags that are associated with songs.

##Step 1: Create Schema

In server.js file, make sure you include Express and Mongoose as dependencies. Also, in server.js, initiliaze the connection to Mongo.

Here are the Mongoose models we'll create in our server.js:

####Artist

  • name (string)
  • bio (string)
  • genres (array of unique strings)

####Song

  • name (string)
  • album (string)
  • genre (string)
  • releasedOn (date)
  • isExplicit (boolean)

####Tag

  • name (string, unique)

Create each of the models listed above. Put them each in their own file in a /models directory. Make sure you use module.exports so that we can require these models in our API.

Now, let's connect these models. Both will use references, not embedded relationships.

  • Use a one-to-many relationship for Artist -> Song
  • Use a many-to-many relationship for Song -> Tag

This should necessitate adding fields to our models to represent these relationships:

####Artist

  • songs

####Song

  • artist
  • tags

####Tag

  • songs

##Step 2: Create API Back in server.js, let's create the routes for our little API.

####POST /artists Save a new artist

####GET /artists Returns a list of artists with a songs attribute

Create an artist named Nelly with the following attributes:

{
  "name": "Nelly",
  "bio": "Cornell Iral Haynes, Jr., better known by his stage name, Nelly, is an American rapper, singer, songwriter, entrepreneur, investor and occasional actor from St. Louis, Missouri",
  "genres": ["hip hop","pop","R&B"]
}

####GET /artists/:id Returns the specified artist

####POST /artists/:id/songs Adds a new song for the given artist

Create a song for Nelly with the following attributes:

{
  "name": "Ride wit me", 
  "album": "Country Grammar", 
  "genre": "pop", 
  "releasedOn": "4-1-2001", 
  "isExplicit": false
}

When you're finished, a GET to /artists/:id for Nelly should return something like this:

[
  {
    "_id": "...",
    "name": "Nelly",
    "bio": "Cornell Iral Haynes, Jr., better known by his stage name, Nelly, is an American rapper, singer, songwriter, entrepreneur, investor and occasional actor from St. Louis, Missouri",
    "songs": [
      {
        "name": "Ride wit me", 
        "album": "Country Grammar", 
        "genre": "pop", 
        "releasedOn": "4-1-2001", 
        "isExplicit": false
      }
    ],
    "genres": ["hip hop","pop","R&B"]
  }
]

####GET /song/:id Returns song with associated tags (remember populate)

####POST /song/:id/tags Creates a tag (if it doesn't already exist) and associate it with the given song. Hint: To do this, you might want to utilize the findOneAndUpdate with the upsert option set to true.

Now test your app by POSTing some tags to "Ride wit me."

Copyright

© DevMountain LLC, 2016. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.

song-tag's People

Contributors

cahlan avatar r-walsh avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.