From f550158865d22591a8b818afc351ebaac74c8830 Mon Sep 17 00:00:00 2001 From: JayWll Date: Sun, 23 Feb 2020 15:09:38 -0700 Subject: [PATCH] Video 90: Resource Creation Endpoints: Part II --- task-manager/src/db/mongoose.js | 12 ------------ task-manager/src/index.js | 13 ++++++++++++- task-manager/src/models/task.js | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 task-manager/src/models/task.js diff --git a/task-manager/src/db/mongoose.js b/task-manager/src/db/mongoose.js index 880ab83..e391751 100644 --- a/task-manager/src/db/mongoose.js +++ b/task-manager/src/db/mongoose.js @@ -4,15 +4,3 @@ mongoose.connect('mongodb://localhost:27017/task-manager-api', { useNewUrlParser: true, useCreateIndex: true }) - -const Task = mongoose.model('Task', { - description: { - type: String, - required: true, - trim: true - }, - completed: { - type: Boolean, - default: false - } -}) diff --git a/task-manager/src/index.js b/task-manager/src/index.js index e41fdd6..4028a1c 100644 --- a/task-manager/src/index.js +++ b/task-manager/src/index.js @@ -1,6 +1,7 @@ const express = require('express') require('./db/mongoose') const User = require('./models/user') +const Task = require('./models/task') const app = express() const port = process.env.PORT || 4200 @@ -11,7 +12,17 @@ app.post('/users', (req, res) => { const user = new User(req.body) user.save().then(() => { - res.send(user) + res.status(201).send(user) + }).catch((e) => { + res.status(400).send(e) + }) +}) + +app.post('/tasks', (req, res) => { + const task = new Task(req.body) + + task.save().then(() => { + res.status(201).send(task) }).catch((e) => { res.status(400).send(e) }) diff --git a/task-manager/src/models/task.js b/task-manager/src/models/task.js new file mode 100644 index 0000000..e1b56a1 --- /dev/null +++ b/task-manager/src/models/task.js @@ -0,0 +1,15 @@ +const mongoose = require('mongoose') + +const Task = mongoose.model('Task', { + description: { + type: String, + required: true, + trim: true + }, + completed: { + type: Boolean, + default: false + } +}) + +module.exports = Task