From 28c11545eb846f4ab47217b267ca1c425a7389c5 Mon Sep 17 00:00:00 2001 From: ijritchey Date: Tue, 8 Nov 2022 20:43:30 -0800 Subject: [PATCH] add: update item route --- controllers/item.js | 24 ++++++++++++++++-------- controllers/job.js | 16 +++++----------- package-lock.json | 36 +++++++++++++++++++++++++++++++----- package.json | 1 + testUser.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 24 deletions(-) create mode 100644 testUser.js diff --git a/controllers/item.js b/controllers/item.js index 5707b2f..8a462ea 100644 --- a/controllers/item.js +++ b/controllers/item.js @@ -2,7 +2,7 @@ const Item = require('../models/item') const getItemsByTask = async (req, res) => { try { - const items = await Item.find({task: req.params.taskId}) + const items = await Item.find({ task: req.params.taskId }) return res.status(200).json(items) } catch (error) { console.log(error); @@ -13,12 +13,12 @@ const getItemsByTask = async (req, res) => { const createItemByTask = async (req, res) => { try { const item = await Item.create({ - task: req.params.taskId, - title: req.body.title, - description: req.body.description, - status: req.body.status, - dueDate: req.body.dueDate, - timeSpent: req.body.timeSpent, + task: req.params.taskId, + title: req.body.title, + description: req.body.description, + status: req.body.status, + dueDate: req.body.dueDate, + timeSpent: req.body.timeSpent, }) return res.status(200).json(item) } catch (error) { @@ -29,9 +29,17 @@ const createItemByTask = async (req, res) => { const updateItemByTask = async (req, res) => { try { + const item = await Item.findById(req.params.itemId); + const newItem = await Item.findByIdAndUpdate(req.params.itemId, { + title: req.body.title ? req.body.title : item.title, + description: req.body.description ? req.body.description : item.description, + status: req.body.status ? req.body.status : item.status, + dueDate: req.body.dueDate ? req.body.dueDate : item.dueDate, + timeSpent: req.body.timeSpent ? req.body.timeSpent : item.timeSpent, + }); + return res.json({ item: newItem }) } catch (error) { console.log(error); - return res.end() } }; diff --git a/controllers/job.js b/controllers/job.js index 6d0c9a6..13e616f 100644 --- a/controllers/job.js +++ b/controllers/job.js @@ -63,7 +63,7 @@ const jobById = async (req, res) => { ], }, }, - {$limit: 1} + { $limit: 1 } // { $unwind:{ // path: "$tasks", // preserveNullAndEmptyArrays: true @@ -159,16 +159,10 @@ const updateJob = async (req, res) => { { title: req.body.title ? req.body.title : getJob.title, url: req.body.url ? req.body.url : getJob.url, - description: req.body.description - ? req.body.description - : getJob.description, + description: req.body.description ? req.body.description : getJob.description, status: req.body.status ? req.body.status : getJob.status, - contactEmail: req.body.contactEmail - ? req.body.contactEmail - : getJob.contactEmail, - contactName: req.body.contactName - ? req.body.contactName - : getJob.contactName, + contactEmail: req.body.contactEmail ? req.body.contactEmail : getJob.contactEmail, + contactName: req.body.contactName ? req.body.contactName : getJob.contactName, }, { upsert: true, @@ -315,7 +309,7 @@ const createTaskByJobId = async (req, res) => { // }); const updateTaskById = async (req, res) => { try { - const task = await Task.findById(req.params.id); + const task = await Item.findById(req.params.id); let newTask = await Task.findByIdAndUpdate(req.params.id, { title: req.body.title ? req.body.title : task.title, description: req.body.description diff --git a/package-lock.json b/package-lock.json index c3775b9..39656a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "@ngneat/falso": "^6.1.0", "axios": "^1.1.3", "bcrypt": "^5.1.0", "body-parser": "^1.20.1", @@ -1056,6 +1057,15 @@ "node": ">=10" } }, + "node_modules/@ngneat/falso": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/@ngneat/falso/-/falso-6.1.0.tgz", + "integrity": "sha512-eka5OxW65B1RphpLJ04Pd4PEkrmTab/Ut50K0OceAdM+O+MZA7YF9xo51uZgkxbhg8bJ5zEh5vucDRMSofcqsw==", + "dependencies": { + "seedrandom": "3.0.5", + "uuid": "8.3.2" + } + }, "node_modules/@tootallnate/once": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz", @@ -3664,6 +3674,11 @@ "node": ">=6" } }, + "node_modules/seedrandom": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-3.0.5.tgz", + "integrity": "sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg==" + }, "node_modules/semver": { "version": "5.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", @@ -4106,7 +4121,6 @@ "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "optional": true, "bin": { "uuid": "dist/bin/uuid" } @@ -5081,6 +5095,15 @@ } } }, + "@ngneat/falso": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/@ngneat/falso/-/falso-6.1.0.tgz", + "integrity": "sha512-eka5OxW65B1RphpLJ04Pd4PEkrmTab/Ut50K0OceAdM+O+MZA7YF9xo51uZgkxbhg8bJ5zEh5vucDRMSofcqsw==", + "requires": { + "seedrandom": "3.0.5", + "uuid": "8.3.2" + } + }, "@tootallnate/once": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz", @@ -5419,8 +5442,7 @@ "cloudinary-core": { "version": "2.13.0", "resolved": "https://registry.npmjs.org/cloudinary-core/-/cloudinary-core-2.13.0.tgz", - "integrity": "sha512-Nt0Q5I2FtenmJghtC4YZ3MZZbGg1wLm84SsxcuVwZ83OyJqG9CNIGp86CiI6iDv3QobaqBUpOT7vg+HqY5HxEA==", - "requires": {} + "integrity": "sha512-Nt0Q5I2FtenmJghtC4YZ3MZZbGg1wLm84SsxcuVwZ83OyJqG9CNIGp86CiI6iDv3QobaqBUpOT7vg+HqY5HxEA==" }, "color-support": { "version": "1.1.3", @@ -7055,6 +7077,11 @@ "sparse-bitfield": "^3.0.3" } }, + "seedrandom": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-3.0.5.tgz", + "integrity": "sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg==" + }, "semver": { "version": "5.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", @@ -7404,8 +7431,7 @@ "uuid": { "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "optional": true + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" }, "validate-npm-package-license": { "version": "3.0.4", diff --git a/package.json b/package.json index a8d6621..ad6c775 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ }, "homepage": "https://github.com/jonrob08/devvy-server-side#readme", "dependencies": { + "@ngneat/falso": "^6.1.0", "axios": "^1.1.3", "bcrypt": "^5.1.0", "body-parser": "^1.20.1", diff --git a/testUser.js b/testUser.js new file mode 100644 index 0000000..d9cb2c2 --- /dev/null +++ b/testUser.js @@ -0,0 +1,44 @@ +require('dotenv').config() +const mongoose = require('mongoose'); +// import { randUser } from '@ngneat/falso'; +const { randUser } = require('@ngneat/falso'); + +// Database Set Up +const MONGO_CONNECTION_STRING = process.env.MONGO_CONNECTION_STRING; +mongoose.connect(MONGO_CONNECTION_STRING, { useNewUrlParser: true, useUnifiedTopology: true }); +const db = mongoose.connection; + +db.once('open', () => { + console.log(`Connected to MongoDB at HOST: ${db.host} and PORT: ${db.port}`); +}); + +db.on('error', (error) => { + console.log(`Database Error: ${error}`); +}) + + +const User = require('./models/user'); +const Job = require('./models/job'); +const Task = require('./models/task'); +const Item = require('./models/item') + +function createUsers() { + for (let i = 0; i < 16; i++) { + const userData = randUser(); + const newUser = new User({ + username: userData.email, + full_name: userData.firstName + " " + userData.lastName, + password: 'password', + username: userData.username, + day: "10", + month: "10", + year: "1990" + }) + newUser.save(); + } +} + +createUsers(); + + +