Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W4D2 HW #4

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
14 changes: 14 additions & 0 deletions models/album.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var Song = require('./song');

var AlbumSchema = new Schema ({
artistName: String,
name: String,
releaseDate: String,
genres: [ String ],
songs: [ Song.schema ]
});

var Album = mongoose.model('Album', AlbumSchema);

module.exports = Album;
5 changes: 5 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/tunely");

var Album = require('./album');
var Song = require('./song');

module.exports.Album = require("./album.js");
11 changes: 11 additions & 0 deletions models/song.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var SongSchema = new Schema ({
name: String,
trackNumber: Number,
});

var Song = mongoose.model('Song', SongSchema);

module.exports = Song;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
},
"homepage": "https://github.com/tgaff/tunely#readme",
"dependencies": {
"express": "^4.13.3"
"express": "^4.13.3",
"mongoose": "~4.2.10",
"body-parser": "~1.14.1"
}
}
119 changes: 85 additions & 34 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,87 @@
*/


/* hard-coded data! */
var sampleAlbums = [];
sampleAlbums.push({
artistName: 'Ladyhawke',
name: 'Ladyhawke',
releaseDate: '2008, November 18',
genres: [ 'new wave', 'indie rock', 'synth pop' ]
});
sampleAlbums.push({
artistName: 'The Knife',
name: 'Silent Shout',
releaseDate: '2006, February 17',
genres: [ 'synth pop', 'electronica', 'experimental' ]
});
sampleAlbums.push({
artistName: 'Juno Reactor',
name: 'Shango',
releaseDate: '2000, October 9',
genres: [ 'electronic', 'goa trance', 'tribal house' ]
});
sampleAlbums.push({
artistName: 'Philip Wesley',
name: 'Dark Night of the Soul',
releaseDate: '2008, September 12',
genres: [ 'piano' ]
});
/* end of hard-coded data */




$(document).ready(function() {
console.log('app.js loaded!');
$.get('/api/albums').success(function (albums) {
albums.forEach(function(album) {
renderAlbum(album);
});
});


$('#album-form form').on('submit', function (event) {
event.preventDefault();
var formData = $(this).serialize();
console.log('formData', formData);
$.post('/api/albums', formData, function (album) {
console.log('album after POST', album);
renderAlbum(album); //renders the server's response
});
$(this).trigger("reset");
});


$('#albums').on('click', '.add-song', function (event) {
var id= $(this).parents('.album').data('album-id');
console.log('id', id);
$('#songModal').data('album-id', id);
$('#songModal').modal();
});

$('#saveSong').on('click', handleNewSongSubmit);

});


// handles the modal fields and POSTing the form to the server
function handleNewSongSubmit (event) {
var albumId = $('#songModal').data('album-id');
var songName = $('#songName').val();
var trackNumber = $('#trackNumber').val();

var formData = {
name: songName,
trackNumber: trackNumber
};

var postUrl = '/api/albums/' + albumId + '/songs';
console.log('posting to ', postUrl, ' with data ', formData);

$.post(postUrl, formData)
.success(function(song) {
console.log('song', song);

// re-get full album and render on the page
$.get('/api/albums/' + albumId).success(function(album) {
//remove old entry
$('[data-album-id=' + albumId + ']').remove();
//render a replacement
renderAlbum(album);
});

// clear form
$('#songName').val('');
$('#trackNumber').val('');
$('#songModal').modal('hide');
});
}



function buildSongsHtml(songs) {
var songText = " $ndash; ";
songs.forEach(function(song) {
songText = songText + "(" + song.trackNumber + ")" + song.name + " – ";
});
var songsHtml =
" <li class = 'list=list-group-item'>" +
" <h4 class='inline-header'>Songs:</h4>" +
" <span>" + songText + "</span>" +
" </li>";
return songsHtml;
}



Expand All @@ -51,7 +96,7 @@ function renderAlbum(album) {

var albumHtml =
" <!-- one album -->" +
" <div class='row album' data-album-id='" + "HARDCODED ALBUM ID" + "'>" +
" <div class='row album' data-album-id='" + album._id + "'>" +
" <div class='col-md-10 col-md-offset-1'>" +
" <div class='panel panel-default'>" +
" <div class='panel-body'>" +
Expand All @@ -64,16 +109,20 @@ function renderAlbum(album) {
" <ul class='list-group'>" +
" <li class='list-group-item'>" +
" <h4 class='inline-header'>Album Name:</h4>" +
" <span class='album-name'>" + "HARDCODED ALBUM NAME" + "</span>" +
" <span class='album-name'>" + album.name + "</span>" +
" </li>" +
" <li class='list-group-item'>" +
" <h4 class='inline-header'>Artist Name:</h4>" +
" <span class='artist-name'>" + "HARDCODED ARTIST NAME" + "</span>" +
" <span class='artist-name'>" + album.artistName + "</span>" +
" </li>" +
" <li class='list-group-item'>" +
" <h4 class='inline-header'>Released date:</h4>" +
" <span class='album-releaseDate'>" + "HARDCODED RELEASE DATE" + "</span>" +
" <span class='album-releaseDate'>" + album.releaseDate + "</span>" +
" </li>" +

buildSongsHtml(album.songs) +


" </ul>" +
" </div>" +
" </div>" +
Expand All @@ -82,11 +131,13 @@ function renderAlbum(album) {
" </div>" + // end of panel-body

" <div class='panel-footer'>" +
" <button class='btn btn-primary add-song'>Add Song</button>" +
" </div>" +

" </div>" +
" </div>" +
" <!-- end one album -->";

// render to the page with jQuery
$('#albums').prepend(albumHtml);
}
61 changes: 57 additions & 4 deletions seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,66 @@

var db = require("./models");

var albumsList =[
// put data here!
];
var albumList =[];
albumList.push({
artistName: 'Nine Inch Nails',
name: 'The Downward Spiral',
releaseDate: '1994, March 8',
genres: [ 'industrial', 'industrial metal' ]
});
albumList.push({
artistName: 'Metallica',
name: 'Metallica',
releaseDate: '1991, August 12',
genres: [ 'heavy metal' ]
});
albumList.push({
artistName: 'The Prodigy',
name: 'Music for the Jilted Generation',
releaseDate: '1994, July 4',
genres: [ 'electronica', 'breakbeat hardcore', 'rave', 'jungle' ]
});
albumList.push({
artistName: 'Johnny Cash',
name: 'Unchained',
releaseDate: '1996, November 5',
genres: [ 'country', 'rock' ]
});

var sampleSongs = [];

sampleSongs.push({ name: 'Swamped',
trackNumber: 1
});
sampleSongs.push({ name: "Heaven's a Lie",
trackNumber: 2
});
sampleSongs.push({ name: 'Daylight Dancer',
trackNumber: 3
});
sampleSongs.push({ name: 'Humane',
trackNumber: 4
});
sampleSongs.push({ name: 'Self Deception',
trackNumber: 5
});
sampleSongs.push({ name: 'Aeon',
trackNumber: 6
});
sampleSongs.push({ name: 'Tight Rope',
trackNumber: 7
});


// populate each album's song list
albumList.forEach(function(album) {
album.songs = sampleSongs;
});


db.Album.remove({}, function(err, albums){

db.Album.create(albumsList, function(err, albums){
db.Album.create(albumList, function(err, albums){
if (err) { return console.log('ERROR', err); }
console.log("all albums:", albums);
console.log("created", albums.length, "albums");
Expand Down
80 changes: 48 additions & 32 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,18 @@
var express = require('express');
// generate a new express app and call it 'app'
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');

// serve static files from public folder
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: true}));

/************
* DATABASE *
************/

/* hard-coded data */
var albums = [];
albums.push({
_id: 132,
artistName: 'Nine Inch Nails',
name: 'The Downward Spiral',
releaseDate: '1994, March 8',
genres: [ 'industrial', 'industrial metal' ]
});
albums.push({
_id: 133,
artistName: 'Metallica',
name: 'Metallica',
releaseDate: '1991, August 12',
genres: [ 'heavy metal' ]
});
albums.push({
_id: 134,
artistName: 'The Prodigy',
name: 'Music for the Jilted Generation',
releaseDate: '1994, July 4',
genres: [ 'electronica', 'breakbeat hardcore', 'rave', 'jungle' ]
});
albums.push({
_id: 135,
artistName: 'Johnny Cash',
name: 'Unchained',
releaseDate: '1996, November 5',
genres: [ 'country', 'rock' ]
});


var db = require('./models');

/**********
* ROUTES *
Expand Down Expand Up @@ -73,6 +45,50 @@ app.get('/api', function api_index (req, res){
});
});

app.get('/api/albums', function albumsIndex (req, res) {
db.Album.find({}, function (err, albums) {
res.json(albums);
});
});

app.post('/api/albums', function albumCreate (req, res) {
console.log('body', req.body);

db.Album.create(req.body, function (err, album) {
if (err) { console.log('error', err); }
console.log(album);
res.json(album);
});

});


app.get('/api/albums/:id', function albumShow (req, res) {
console.log('requested album id=', req.params.id);
db.Album.findOne({_id: req.params.id}, function(err, album) {
res.json(album);
});
});


app.post('/api/albums/:album_id/songs', function songsCreate(req, res) {
console.log('body', req.body);
db.Album.findOne({_id: req.params.albumId}, function (err, album) {
if (err) { console.log('error', err); }

var song = new db.Song(req.body);
album.songs.push(song);
album.save(function(err, savedAlbum) {
if (err) { console.log('error', err); }
console.log('album with new song saved:', savedAlbum);
res.json(song);
});
});

});



/**********
* SERVER *
**********/
Expand Down
Loading