'use strict';

const express = require('express');

const app = express();
const mongoose = require('mongoose');
const { json } = require('body-parser');

const PORT = process.env.PORT || 3000;
const MONGODB_URL = process.env.MONGODB_URL || 'mongodb://localhost:27017/logjam';


app.use(express.static('client'));
app.use(json());


// SET UP MODEL FOR WHAT IS TO BE SENT TO DB ON FORM SUBMIT
const Song = mongoose.model('song', {
  userId: String,
  title: String,
  lyric: String,
  audio: String
});

// GETS ALL THE SONGS BACK FROM THE DB
app.get('/api/getAll', (req, res, err) => {
  Song
    .find()
    .then(songs => res.json({ songs }))
    .catch(err)
}); 

// FIND ONE SONG BY ID AND RETURN THAT SONG
app.get('/api/getOne/:id', (req, res, err) => {
  let songId = req.params.id;
  Song
    .findById(songId)
    .then(songs => res.json({ songs }))
    .catch(err)
});
 

// POSTS NEW SONG TO DB
app.post('/api/newSong', (req, res, err) => {
  console.log("req.body", req.body);
  Song
    .create(req.body)
    .then(song => res.json(song))
    .catch(err)
});

// // DELETE SONG FROM DB
// deletes but still gets hung up
app.get('/api/deleteSong/:id', (req, res, err) => {
  let id = {
    _id: req.params.id
  };
  Song.remove(id, function(err, songs){
    if(err){
      console.log("it broke");                
    }
    else{                
      console.log("song removed");
    }
  })
  .then(() => res.send("song deleted in DB"))
})                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    


mongoose.Promise = Promise;

mongoose.connect(MONGODB_URL, () => { 
  app.listen(PORT, () => console.log(`Listening on PORT: ${PORT}`));
})

@@ -497,16 +497,17 @@
 ng%0A%7D);%0A%0A
+%0A
 // GETS 
@@ -662,16 +662,17 @@
 )%0A%7D); %0A%0A
+%0A
 // FIND 
@@ -1068,16 +1068,17 @@
 r)%0A%7D);%0A%0A
+%0A
 // // DE
@@ -1099,42 +1099,8 @@
  DB%0A
-// deletes but still gets hung up%0A
 app.
@@ -1411,16 +1411,17 @@
 in DB%22))
+;
 %0A%7D)     
@@ -2014,16 +2014,419 @@
      %0A%0A%0A
+// UPDATE SONG LYRICS%0Aapp.patch('/api/updateSong/:id', (req, res, err) =%3E %7B%0A  let id = %7B%0A    _id: req.params.id%0A  %7D;%0A  Song.findOneAndUpdate(id, %7B$set:%7B%0A    title: req.body.title,%0A    lyric: req.body.lyric,%0A    audio: req.body.audio%0A  %7D%7D)%0A  .then(console.log(%22req.body after update%22, req.body))%0A  .then((song) =%3E %7B%0A    console.log('updated song', song)%0A    res.json(song)%0A  %7D)%0A  .catch(err)%0A%7D);%0A%0A%0A%0A%0A%0A%0A%0A%0A
 mongoose

update function working