1
0

Video 89: Resource Creation Endpoints: Part I

This commit is contained in:
JayWll 2020-02-23 14:53:38 -07:00
parent a340b108f1
commit 97ae98972b
6 changed files with 2980 additions and 93 deletions

View File

@ -1,30 +0,0 @@
// CRUD create read update delete
const { MongoClient, ObjectID } = require('mongodb')
const connectionURL = 'mongodb://127.0.0.1:27017'
const databaseName = 'task-manager'
MongoClient.connect(connectionURL, { useNewUrlParser: true }, (error, client) => {
if (error) {
return console.log('Unable to connect to database!')
}
const db = client.db(databaseName)
// db.collection('users').deleteMany({
// age: 38
// }).then((result) => {
// console.log(result)
// }).catch((error) => {
// console.log(error)
// })
db.collection('tasks').deleteOne({
description: 'Install MongoDB'
}).then((result) => {
console.log(result)
}).catch((error) => {
console.log(error)
})
})

File diff suppressed because it is too large Load Diff

View File

@ -4,14 +4,19 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "node src/index.js",
"dev": "nodemon src/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.4",
"mongodb": "^3.1.10",
"mongoose": "^5.3.16",
"validator": "^10.9.0"
},
"devDependencies": {
"nodemon": "^1.18.9"
}
}

View File

@ -1,62 +1,10 @@
const mongoose = require('mongoose')
const validator = require('validator')
mongoose.connect('mongodb://localhost:27017/task-manager-api', {
useNewUrlParser: true,
useCreateIndex: true
})
const User = mongoose.model('User', {
name: {
type: String,
required: true,
trim: true
},
email: {
type: String,
required: true,
trim: true,
lowercase: true,
validate(value) {
if (!validator.isEmail(value)) {
throw new Error('Email is invalid')
}
}
},
password: {
type: String,
required: true,
minlength: 7,
trim: true,
validate(value) {
if (value.toLowerCase().includes('password')) {
throw new Error('Password cannot contain "password"')
}
}
},
age: {
type: Number,
default: 0,
validate(value) {
if (value < 0) {
throw new Error('Age must be a positive number')
}
}
}
})
// const me = new User({
// name: ' Jason ',
// email: 'J@SON-WILLIAMS.CA ',
// password: 'testpassw0rd'
// })
//
// me.save().then(() => {
// console.log(me)
// }).catch((error) => {
// console.log('Error!', error)
// })
const Task = mongoose.model('Task', {
description: {
type: String,
@ -68,13 +16,3 @@ const Task = mongoose.model('Task', {
default: false
}
})
const task = new Task({
description: ' Eat lunch '
})
task.save().then(() => {
console.log(task);
}).catch((error) => {
console.log('Error!', error)
})

22
task-manager/src/index.js Normal file
View File

@ -0,0 +1,22 @@
const express = require('express')
require('./db/mongoose')
const User = require('./models/user')
const app = express()
const port = process.env.PORT || 4200
app.use(express.json())
app.post('/users', (req, res) => {
const user = new User(req.body)
user.save().then(() => {
res.send(user)
}).catch((e) => {
res.status(400).send(e)
})
})
app.listen(port, () => {
console.log('Server is up on port ' + port)
})

View File

@ -0,0 +1,43 @@
const mongoose = require('mongoose')
const validator = require('validator')
const User = mongoose.model('User', {
name: {
type: String,
required: true,
trim: true
},
email: {
type: String,
required: true,
trim: true,
lowercase: true,
validate(value) {
if (!validator.isEmail(value)) {
throw new Error('Email is invalid')
}
}
},
password: {
type: String,
required: true,
minlength: 7,
trim: true,
validate(value) {
if (value.toLowerCase().includes('password')) {
throw new Error('Password cannot contain "password"')
}
}
},
age: {
type: Number,
default: 0,
validate(value) {
if (value < 0) {
throw new Error('Age must be a positive number')
}
}
}
})
module.exports = User