Permalink
Browse files

added more stuff

  • Loading branch information...
PhillipCumber committed Nov 8, 2018
1 parent 0aad073 commit 73693b2a2bcb0f169e5de141cea95aa4ebb990ca
Showing with 131 additions and 3 deletions.
  1. +2 −1 .gitignore
  2. +53 −0 controllers/userController.js
  3. +11 −0 db/connections.js
  4. +24 −0 db/seeds.js
  5. +11 −0 models/Idea.js
  6. +15 −0 models/User.js
  7. +13 −0 routes/index.js
  8. +2 −2 server.js
@@ -1 +1,2 @@
node_modules/
node_modules/
.env
@@ -0,0 +1,53 @@
let User = require('../models/User')
let Idea = require('../models/Idea')

let userController = {

index: (req, res) => {

User.find({})
.then((user) => {

res.send(user)
})
},

show: (req, res) => {

User.findById(req.params.userId)
.then((user) => {

res.send(user)
})
},

update: (req, res) => {

User.findByIdAndUpdate(req.params.userId, req.body)
.then((update) => {
update.save()
res.send(update)
})
},

delete: (req, res) => {

User.findByIdAndDelete(req.params.userId)
.then(() => {

res.send(200)
})
},

create: (req, res) => {

User.create(req.body)
.then((user) => {

user.save()
res.send(user)
})
}
}

module.exports = userController
@@ -0,0 +1,11 @@
require('dotenv').config()
let mongoose = require('mongoose')

mongoose.connect(process.env.MONGODB_URI)

mongoose.connection.once('open', () => {

console.log('Mongoose is connected')
})

module.exports = mongoose
@@ -0,0 +1,24 @@
const User = require('../models/User')
const Idea = require('../models/Idea')
const mongoose = require('./connections')

const mars = new Idea({
title: 'Fly to Mars',
description: "Earth isn't Red enough. Let's move to a new planet"
})
const tesla = new Idea({
title: 'Build a Car',
description: "Gas is too expensive. I'm gonna build a car that doesn't need gas"
})
const elon = new User({
userName: 'elon_musk',
passWord: 'spaceiscool',
ideas: [mars, tesla]
})

User.remove({})
.then(() => Idea.remove({}))
.then(() => Idea.insertMany([mars, tesla]))
.then(() => elon.save())
.then(() => console.log('Successful Save'))
.then(() => mongoose.connection.close())
@@ -0,0 +1,11 @@
let mongoose = require('../db/connections')
let Schema = mongoose.Schema

let Idea = new Schema ({

title: String,
description: String,

})

module.exports = mongoose.model('Idea', Idea)
@@ -0,0 +1,15 @@
let mongoose = require('../db/connections')
let Schema = mongoose.Schema

let User = new Schema ({

userName: String,
passWord: String,
ideas : [{

type: Schema.Types.ObjectId,
ref: 'Idea'
}]
})

module.exports = mongoose.model('User', User)
@@ -0,0 +1,13 @@
let express = require('express')
let router = express.Router()
let userController = require('../controllers/userController')

router.get('/api/users', userController.index)
router.post('/api/users', userController.create)
router.get('/api/users/:userId', userController.show)
router.patch('/api/users/:userId', userController.update)
router.delete('/api/users/:userId', userController.delete)



module.exports = router
@@ -1,10 +1,10 @@
let express = require('express')
let app = express()
// let routes = require('./routes/index')
let routes = require('./routes/index')

app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// app.use('/', routes)
app.use('/', routes)
app.use(express.static(__dirname + '/client/build/'));
app.get('/', (req,res) => {
res.sendFile(__dirname + '/client/build/index.html')

0 comments on commit 73693b2

Please sign in to comment.