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

Postv2 #4

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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;
5 changes: 4 additions & 1 deletion models/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/tunely");
mongoose.connect("mongodb://localhost/tunely_test");
var Album = require('./album');

module.exports.Album = Album;
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "An app for tracking your music collection",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"repository": {
"type": "git",
Expand All @@ -17,6 +18,8 @@
},
"homepage": "https://github.com/tgaff/tunely#readme",
"dependencies": {
"express": "^4.13.3"
"express": "^4.13.3",
"mongoose": "^4.2.9",
"body-parser": "^1.13.2"
}
}
1 change: 0 additions & 1 deletion public/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@
vertical-align: baseline;
margin-right: 12px;
}

31 changes: 25 additions & 6 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,28 @@ sampleAlbums.push({

$(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(e) {
e.preventDefault();
var formData = $(this).serialize();
console.log('formData', formData);
$.post('/api/albums', formData, function(album) {
console.log('album after POST', album);
renderAlbum(album); //render the server's response
});
$(this).trigger("reset");
});

});




Expand All @@ -64,15 +83,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-name'>" + album.releaseDate + "</span>" +
" </li>" +
" </ul>" +
" </div>" +
Expand All @@ -88,5 +107,5 @@ function renderAlbum(album) {
" </div>" +
" <!-- end one album -->";

// render to the page with jQuery
}
$('#albums').prepend(albumHtml);
}
32 changes: 27 additions & 5 deletions seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,39 @@

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");
process.exit();
});

});
51 changes: 18 additions & 33 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,17 @@
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'));


/************
* 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 +44,20 @@ 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){
db.Album.create(req.body, function(err, album) {
if (err) { console.log('error', err); }
console.log(album);
res.json(album);
});
});

/**********
* SERVER *
**********/
Expand Down
Binary file added tunely.bmpr
Binary file not shown.
71 changes: 20 additions & 51 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ <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">
<form>
Artist<br>
<input type="text" name="artist">
<br>
Album Name<br>
<input type="text" name="album-name">
<br>
Release Date<br>
<input type="text" name="release-date">
<br>
Genre<br>
<input type="text" name="genre">
<br>
<input class="btn btn-primary col-md-12" type="submit" value="Submit">
</form>
</div>
</div>

<section class="container">
<div class="row">
Expand All @@ -34,57 +54,6 @@ <h2>Albums</h2>
<!-- albums! -->
<div id='albums'>




<!-- one album -->
<div class="row album">

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


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

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

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

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

<div class='panel-footer'>
</div>

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




</div>
</section>

Expand Down