From 667841f8a751019ef1e089c1b2227e26000501c6 Mon Sep 17 00:00:00 2001 From: trebloc Date: Wed, 9 Dec 2015 09:47:06 -0800 Subject: [PATCH] turning in homework --- models/album.js | 15 +++++++ models/index.js | 5 +++ models/song.js | 10 +++++ package.json | 4 +- public/js/app.js | 45 ++++++++++++++++++--- seed.js | 69 ++++++++++++++++++++++++++++++-- server.js | 24 +++++++++++ views/index.html | 101 +++++++++++++++++++++++++++-------------------- 8 files changed, 221 insertions(+), 52 deletions(-) create mode 100644 models/song.js diff --git a/models/album.js b/models/album.js index 0411a57..831abab 100644 --- a/models/album.js +++ b/models/album.js @@ -1,2 +1,17 @@ +// Database - Server + var mongoose = require("mongoose"); var Schema = mongoose.Schema; + +var song = require('./song.js'); + +var AlbumSchema = new Schema({ + artistName: String, + name: String, + song: [ song.schema ], + releaseDate: String, + genres: [ String ] +}); + +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..610e32e 100644 --- a/models/index.js +++ b/models/index.js @@ -1,2 +1,7 @@ +// What you need to populate-seed your Database on your back-end + var mongoose = require("mongoose"); mongoose.connect("mongodb://localhost/tunely"); + +module.exports.Album = require("./album.js"); + diff --git a/models/song.js b/models/song.js new file mode 100644 index 0000000..7a3447e --- /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: String, +}); + +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..34e2d03 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -38,17 +38,51 @@ sampleAlbums.push({ $(document).ready(function() { - console.log('app.js loaded!'); + // console.log('app.js loaded!'); + // console.log(sampleAlbums[0].artistName); + // console.log(sampleAlbums[0].name); + // console.log(sampleAlbums[0].releaseDate); + // console.log(sampleAlbums[0].genres); + // renderAlbum(sampleAlbums[0]); + // sampleAlbums.forEach(function(element) { + // renderAlbum(element); + // }); + // ajax to get albums from server-side + $.ajax({ + method: "GET", + url: "/api/albums", + success: function (albums) { + console.log(albums); + albums.forEach(function(element){ + renderAlbum(element); + }); + }, + error: function () { + console.log("uh oh...") + } + }); + +$('#singlebutton').on('click', function (event){ + event.preventDefault(); + var newAlbum = $("#new-album").serialize(); + console.log(newAlbum); + $('form').trigger('reset'); + $.post( '/api/albums', newAlbum , function( album ) { + // console.log(album); + renderAlbum(album); + }); + }); + }); - // this function takes a single album and renders it to the page function renderAlbum(album) { console.log('rendering album:', album); + var albumHtml = " " + "
" + @@ -64,15 +98,15 @@ function renderAlbum(album) { " " + "
" + @@ -89,4 +123,5 @@ function renderAlbum(album) { " "; // render to the page with jQuery + $('#albums').append(albumHtml); } diff --git a/seed.js b/seed.js index 1943f8f..baab57e 100644 --- a/seed.js +++ b/seed.js @@ -1,15 +1,78 @@ // This file allows us to seed our application with data // simply run: `node seed.js` from the root of this project folder. +// Seeding a Database var db = require("./models"); -var albumsList =[ - // put data here! +var albumsList = [ + { + 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' ] + } + ]; +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 +}); + + db.Album.remove({}, function(err, albums){ - db.Album.create(albumsList, function(err, albums){ + db.Album.create(albumsList, function(err, albums) { + album.song.push() + //take the array of sampleSongs + // iterate through sampleSongs with forEach + // and within the forEach callback function + // create the song it the iterator is currently on + sampleSongs.forEach(function(song){ + albums.song.push(db.Song.create(song, function(err, song){ + //store the song into album + })) + }); if (err) { return console.log('ERROR', err); } console.log("all albums:", albums); console.log("created", albums.length, "albums"); diff --git a/server.js b/server.js index 5da137b..8799f43 100644 --- a/server.js +++ b/server.js @@ -5,8 +5,12 @@ var express = require('express'); // generate a new express app and call it 'app' var app = express(); +var bodyParser = require('body-parser'); + // serve static files from public folder app.use(express.static(__dirname + '/public')); +app.use(bodyParser.urlencoded({ extended: true })); +var db = require('./models'); /************ * DATABASE * @@ -73,6 +77,26 @@ app.get('/api', function api_index (req, res){ }); }); +app.get('/api/albums', function index (req, res){ + db.Album.find({}, function(err, albums) { + console.log(albums); + res.json(albums); + }); +}); + +app.post('/api/albums', function (req, res) { + console.log(req.body); + + // splitting items in the array + 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) { return console.log('ERROR', err); } + console.log('album=',album); + res.json(album); + }); +}); + /********** * SERVER * **********/ diff --git a/views/index.html b/views/index.html index 5cb6501..2374921 100644 --- a/views/index.html +++ b/views/index.html @@ -19,68 +19,83 @@
+

Welcome to tunely

Your music binder!

- -
+
-
-

Albums

-
-
- -
+
+
+
+ + Add New Album + +
+ +
+ - -
- -
-
-
- +
+
- -
-
- album image -
+ +
+ +
+ -
-
    -
  • -

    Album Name:

    - Ladyhawke -
  • +
+
-
  • -

    Artist Name:

    - Ladyhawke -
  • + +
    + +
    + -
  • -

    Released date:

    - 2008, November 18 -
  • - -
    +
    +
    -
    - + +
    + +
    + +
    +
    - -
    + +
    + +
    +
    -
    - +
    +
    +
    +
    +
    +
    +
    +
    +

    Albums

    +
    +
    + +
    + + + + +