Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions models/album.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var Song = require('./song');

var AlbumSchema = new Schema({
artistName: String,
name: String,
releaseDate: String,
genres: [ 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");
module.exports.Song = require('./song.js');
10 changes: 10 additions & 0 deletions models/song.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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"
"body-parser": "^1.14.1",
"express": "^4.13.3",
"mongoose": "^4.2.10"
}
}
32 changes: 22 additions & 10 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,28 @@ sampleAlbums.push({
});
/* end of hard-coded data */




$(document).ready(function() {
console.log('app.js loaded!');
// sampleAlbums.forEach(function(element){
// renderAlbum(element);
// });

$.get('/api/albums', function(albums){
albums.forEach(function(element){
renderAlbum(element);
});
});

$('#album-form form').submit(function(event){
event.preventDefault();
var formData = $(this).serialize();
$.post('/api/albums', formData, function(album){
renderAlbum(album);
});
$(this).trigger('reset');
});
});





// this function takes a single album and renders it to the page
function renderAlbum(album) {
console.log('rendering album:', album);
Expand All @@ -64,15 +75,15 @@ 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>" +
" </ul>" +
" </div>" +
Expand All @@ -89,4 +100,5 @@ function renderAlbum(album) {
" <!-- end one album -->";

// render to the page with jQuery
$('#albums').append(albumHtml);
}
26 changes: 25 additions & 1 deletion seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ var db = require("./models");

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

db.Album.remove({}, function(err, albums){
Expand All @@ -16,4 +40,4 @@ db.Album.remove({}, function(err, albums){
process.exit();
});

});
});
54 changes: 22 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,24 @@ app.get('/api', function api_index (req, res){
});
});

app.get('/api/albums', function albums_index (req, res){
db.Album.find(function(err, albums){
if(err) {console.log(err); }
res.send(albums);
});
});

app.post('/api/albums', function albums_create (req, res){
console.log('body', req.body);
var genres = req.body.genres.split(', ').map(function(item) { return item.trim(); } );
req.body.genres = genres;
db.Album.create(req.body, function(err, album){
if(err) { console.log(err); }
console.log(album);
res.json(album);
});
});

/**********
* SERVER *
**********/
Expand Down
99 changes: 53 additions & 46 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,66 +25,73 @@ <h1>Welcome to tunely</h1>
</div>
</div>

<section class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h2>Albums</h2>
</div>
</div>
<!-- albums! -->
<div id='albums'>




<!-- one album -->
<div class="row album">
<section id='album-form' class='container'>
<div class='row'>
<div class="col-md-10 col-md-offset-1">

<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-body">
<form class="form-horizontal">
<fieldset>

<!-- Form Name -->
<legend>Add New Album</legend>

<!-- begin album internal row -->
<div class='row'>
<div class="col-md-3 col-xs-12 thumbnail album-art">
<img src="http://placehold.it/800x800" alt="album image">
</div>

<div class="col-md-9 col-xs-12">
<ul class="list-group">
<li class="list-group-item">
<h4 class='inline-header'>Album Name:</h4>
<span class='album-name'>Ladyhawke</span>
</li>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Album Name</label>
<div class="col-md-4">
<input id="name" name="name" type="text" placeholder="" class="form-control input-md" required="">

<li class="list-group-item">
<h4 class='inline-header'>Artist Name:</h4>
<span class='artist-name'>Ladyhawke</span>
</li>
</div>
</div>

<li class="list-group-item">
<h4 class='inline-header'>Released date:</h4>
<span class='album-releaseDate'>2008, November 18</span>
</li>
</ul>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="artistName">Artist Name</label>
<div class="col-md-4">
<input id="textinput" name="artistName" type="text" placeholder="" class="form-control input-md">

</div>
<!-- end of album internal row -->
</div>
</div>

<div class='panel-footer'>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="releaseDate">Release Date</label>
<div class="col-md-4">
<input id="releaseDate" name="releaseDate" type="text" placeholder="1992" class="form-control input-md">

</div>
</div>
</div>
</div>
<!-- end one album -->

<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="genres">Genres (separated by commas)</label>
<div class="col-md-4">
<textarea class="form-control" id="genres" name="genres">rock, pop</textarea>
</div>
</div>


<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton">Save Album</label>
<div class="col-md-4">
<button id="singlebutton" name="singlebutton" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</section>

<section class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h2>Albums</h2>
</div>
</div>
<!-- albums! -->
<div id='albums'>
</div>
</section>

Expand Down