Video 89: Resource Creation Endpoints: Part I
This commit is contained in:
@@ -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
22
task-manager/src/index.js
Normal 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)
|
||||
})
|
||||
43
task-manager/src/models/user.js
Normal file
43
task-manager/src/models/user.js
Normal 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
|
||||
Reference in New Issue
Block a user