diff --git a/models/album.js b/models/album.js
index 0411a57..62d5d60 100644
--- a/models/album.js
+++ b/models/album.js
@@ -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;
\ No newline at end of file
diff --git a/models/index.js b/models/index.js
index 6c10401..6199299 100644
--- a/models/index.js
+++ b/models/index.js
@@ -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');
\ No newline at end of file
diff --git a/models/song.js b/models/song.js
new file mode 100644
index 0000000..3a44311
--- /dev/null
+++ b/models/song.js
@@ -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;
\ No newline at end of file
diff --git a/package.json b/package.json
index 0d3e4f8..7fded2d 100644
--- a/package.json
+++ b/package.json
@@ -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"
}
}
diff --git a/public/js/app.js b/public/js/app.js
index 164eb55..832f253 100644
--- a/public/js/app.js
+++ b/public/js/app.js
@@ -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);
@@ -64,15 +75,15 @@ function renderAlbum(album) {
"
" +
" - " +
" " +
- " " + "HARDCODED ALBUM NAME" + "" +
+ " " + album.name + "" +
"
" +
" - " +
" " +
- " " + "HARDCODED ARTIST NAME" + "" +
+ " " + album.artistName + "" +
"
" +
" - " +
" " +
- " " + "HARDCODED RELEASE DATE" + "" +
+ " " + album.releaseDate + "" +
"
" +
"
" +
" " +
@@ -89,4 +100,5 @@ function renderAlbum(album) {
" ";
// render to the page with jQuery
+ $('#albums').append(albumHtml);
}
diff --git a/seed.js b/seed.js
index 1943f8f..fe1096d 100644
--- a/seed.js
+++ b/seed.js
@@ -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){
@@ -16,4 +40,4 @@ db.Album.remove({}, function(err, albums){
process.exit();
});
-});
+});
\ No newline at end of file
diff --git a/server.js b/server.js
index 5da137b..16a16b8 100644
--- a/server.js
+++ b/server.js
@@ -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 *
@@ -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 *
**********/
diff --git a/views/index.html b/views/index.html
index 5cb6501..feac6f3 100644
--- a/views/index.html
+++ b/views/index.html
@@ -25,66 +25,73 @@ Welcome to tunely
-