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 AlbumSchema = new Schema ({
artistName: String,
name: String,
releaseDate: String,
genres: [String]
});

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

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

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

name: String,
trackNumber: Number

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"
}
}
45 changes: 40 additions & 5 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,43 @@ sampleAlbums.push({

$(document).ready(function() {
console.log('app.js loaded!');
});

$.ajax({
method: 'GET',
url: '/api/albums',
success: function (data){
console.log(data);
data.forEach(function (x){
renderAlbum(x);
});
}
});



$("#album-form form").on("submit", function (event){
event.preventDefault();
var formData = $(this).serialize();
console.log($(this).serialize());


$.ajax({
method: 'POST',
url: '/api/albums',
data: formData,
success: function (data){
console.log('album after POST', data);
renderAlbum(data);
}
});
$(this).trigger('reset');
});





});



Expand All @@ -64,15 +99,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 @@ -88,5 +123,5 @@ function renderAlbum(album) {
" </div>" +
" <!-- end one album -->";

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

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' ]
});

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
52 changes: 23 additions & 29 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,21 @@ 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' ]
});




Expand All @@ -52,6 +30,7 @@ albums.push({
/*
* HTML Endpoints
*/
var db = require('./models');

app.get('/', function homepage (req, res) {
res.sendFile(__dirname + '/views/index.html');
Expand All @@ -73,6 +52,21 @@ app.get('/api', function api_index (req, res){
});
});

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

app.post('/api/albums', function (req,res){
console.log(req.body);
//res.json(data.genres);
db.Album.create(req.body, function (err, album) {
if (err) {console.log('error, err');}
console.log(album);
res.json(album);
});
});
/**********
* SERVER *
**********/
Expand Down
99 changes: 55 additions & 44 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,67 +24,78 @@ <h1>Welcome to tunely</h1>
<p>Your music binder!</p>
</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>
<!-- 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="">

<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>
</div>
</div>

<li class="list-group-item">
<h4 class='inline-header'>Artist Name:</h4>
<span class='artist-name'>Ladyhawke</span>
</li>
<!-- 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">

<li class="list-group-item">
<h4 class='inline-header'>Released date:</h4>
<span class='album-releaseDate'>2008, November 18</span>
</li>
</ul>
</div>
</div>
</div>

</div>
<!-- end of album internal row -->
<!-- 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 class='panel-footer'>
</div>
</div>
</div>

</div>
<!-- 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>
</div>
<!-- end one album -->


<!-- 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>

</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