Permalink
Browse files

full crud on users

  • Loading branch information...
spencermerryman committed Nov 8, 2018
1 parent 31a0b4d commit fd7bff9a64c75c9fb60c090f8d4b3efd45912ab8
Showing with 122 additions and 3 deletions.
  1. +2 −1 .gitignore
  2. +38 −0 controllers/userController.js
  3. +16 −0 db/connections.js
  4. +26 −0 db/seeds.js
  5. +10 −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,38 @@
const User = require('../models/User')
const Idea = require('../models/Idea')

const userController = {
index: (req, res) => {
User.find({})
.then((users) => {
res.send(users)
})
},
show: (req, res) => {
User.findById(req.params.userId)
.then((user) => {
res.send(user)
})
},
update: (req, res) => {
User.findByIdAndUpdate(req.params.userId, req.body)
.then((updatedUser) => {
updatedUser.save()
res.send(updatedUser)
})
},
delete: (req, res) => {
User.findByIdAndDelete(req.params.userId)
.then(() => {
res.send(200)
})
},
create: (req, res) => {
User.create(req.body)
.then((user) => {
res.send(user)
})
}
}

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

mongoose.connect(process.env.MONGODB_URI)

mongoose.connection.once('open', () => {
console.log(`Mongoose has connected to MongoDB`)
})

mongoose.connection.on('error', (error) => {
console.error(`MongoDB connection error!!! ${error}`)
process.exit(-1)
})

module.exports = mongoose

@@ -0,0 +1,26 @@
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,10 @@
const mongoose = require('../db/connections')
const Schema = mongoose.Schema

const Idea = new Schema({
title: String,
description: String
})

module.exports = mongoose.model('Idea', Idea)

@@ -0,0 +1,15 @@
const mongoose = require('../db/connections')
const Schema = mongoose.Schema

const User = new Schema({
username: String,
password: String,
ideas: [
{
type: Schema.Types.ObjectId,
ref: 'Idea'
}
]
})

module.exports = mongoose.model('User', User)
@@ -0,0 +1,13 @@
const express = require('express')
const router = express.Router()
const 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,6 +1,6 @@
const express = require('express')
const app = express()
// const routes = require('./routes/index')
const routes = require('./routes/index')

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

// app.use('/', routes)
app.use('/', routes)

const PORT = process.env.PORT || 3001
app.listen(PORT, () => {

0 comments on commit fd7bff9

Please sign in to comment.