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
15 changes: 15 additions & 0 deletions models/album.js
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -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");

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: String,
});

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"
}
}
45 changes: 40 additions & 5 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
" <!-- one album -->" +
" <div class='row album' data-album-id='" + "HARDCODED ALBUM ID" + "'>" +
Expand All @@ -64,15 +98,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 +123,5 @@ function renderAlbum(album) {
" <!-- end one album -->";

// render to the page with jQuery
$('#albums').append(albumHtml);
}
69 changes: 66 additions & 3 deletions seed.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
24 changes: 24 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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 *
**********/
Expand Down
101 changes: 58 additions & 43 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,68 +19,83 @@
</head>
<body>
<div class="jumbotron">

<div class="container">
<h1>Welcome to tunely</h1>
<p>Your music binder!</p>
</div>
</div>

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

<form class="form-horizontal" id="new-album">
<fieldset>

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

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

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

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

</div>
</div>

<!-- 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="artistName">Artist Name</label>
<div class="col-md-4">
<input id="textinput" name="artistName" type="text" placeholder="" class="form-control input-md">

<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="releaseDate">Release Date</label>
<div class="col-md-4">
<input id="releaseDate" name="releaseDate" type="text" placeholder="1992" 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 -->
<!-- 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 class='panel-footer'>
</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>
</div>
<!-- end one album -->
</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'>








Expand Down