diff --git a/client/.gitignore b/client/.gitignore new file mode 100644 index 0000000..6a1037f --- /dev/null +++ b/client/.gitignore @@ -0,0 +1,127 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and *not* Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* diff --git a/controllers/Category.js b/controllers/Category.js new file mode 100644 index 0000000..aab3437 --- /dev/null +++ b/controllers/Category.js @@ -0,0 +1,36 @@ +import express from 'express'; +import mongoose from 'mongoose'; + +import CategoryMessage from '../models/Category.js'; + +const router = express.Router(); + + +export const createCategory = async (req, res) => { + const { id,name,items } = req.body; + + const newCategoryMessage = new CategoryMessage({deps}) + + try { + await newCategoryMessage.save(); + + res.status(201).json(newCategoryMessage ); + } catch (error) { + res.status(409).json({ message: error.message }); + } +} + + +export const deleteCategory = async (req, res) => { + const { id } = req.params; + + if (!mongoose.Types.ObjectId.isValid(id)) return res.status(404).send(`No Category with id: ${id}`); + + await CategoryMessage.findByIdAndRemove(id); + + res.json({ message: "Category deleted successfully." }); +} + + + +export default router; diff --git a/models/Category.js b/models/Category.js new file mode 100644 index 0000000..03a7f30 --- /dev/null +++ b/models/Category.js @@ -0,0 +1,16 @@ +import mongoose from 'mongoose'; + +const categorySchema = mongoose.Schema({ + id: Number, + name:String, + items:[], + createdAt: { + type: Date, + default: new Date(), + }, + +}) + +var categoryMessage = mongoose.model('categoryMessage', categorySchema); + +export default categoryMessage; \ No newline at end of file diff --git a/package.json b/package.json index a0ac94a..fc29ac0 100644 --- a/package.json +++ b/package.json @@ -3,12 +3,16 @@ "version": "1.0.0", "description": "", "main": "index.js", + "type": "module", + "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "client-install": "npm install --prefix client", - "server": "nodemon index.js", + "server": "nodemon server.js", "client": "npm start --prefix client", - "dev": "concurrently \"npm run server\" \"npm run client\"" + "dev": "concurrently \"npm run server\" \"npm run client\"", + "build": "CI= react-scripts build", + }, "repository": { "type": "git", diff --git a/routes/category.js b/routes/category.js new file mode 100644 index 0000000..1f0063d --- /dev/null +++ b/routes/category.js @@ -0,0 +1,10 @@ +import express from 'express'; + +import { getCategory, deleteCategory } from '../controllers/Category.js'; + +const router = express.Router(); + +router.category('/', createCategory); +router.delete('/:id', deleteCategory); + +export default router; diff --git a/server.js b/server.js new file mode 100644 index 0000000..d03448a --- /dev/null +++ b/server.js @@ -0,0 +1,19 @@ +import express from 'express'; +import bodyParser from 'body-parser'; +import mongoose from 'mongoose'; +import cors from 'cors'; + + +const app = express(); + +app.use(bodyParser.json({ extended: true })) +app.use(bodyParser.urlencoded({ extended: true })) +app.use(cors()); + + +const CONNECTION_URL = 'mongodb+srv://user:userpassword@cluster0.hbc0x.mongodb.net/myFirstDatabase?retryWrites=true&w=majority+srv://mongodb+srv://user:userpassword@cluster0.hbc0x.mongodb.net/myFirstDatabase?retryWrites=true&w=majority:123123123@practice.jto9p.mongodb.net/test'; +const PORT = process.env.PORT|| 5000; + +mongoose.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => app.listen(PORT, () => console.log(`Server Running on Port: http://localhost:${PORT}`))) + .catch((error) => console.log(`${error} did not connect`));