From 3608ae1aef6386f6e41f7ae67b70cc1e2d34692f Mon Sep 17 00:00:00 2001 From: Mariem166 <74572429+Mariem166@users.noreply.github.com> Date: Sat, 8 May 2021 00:49:08 +0100 Subject: [PATCH 1/2] Add files via upload --- app.js | 291 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 00000000..bab60387 --- /dev/null +++ b/app.js @@ -0,0 +1,291 @@ +const express = require("express"); +const app = express(); +const bcrypt = require('bcrypt'); +const jwt = require("jsonwebtoken"); +const dotenv = require("dotenv"); +dotenv.config(); + +const mongoose = require('mongoose'); + +mongoose.connect('mongodb://localhost:27017/assignment-1', {useNewUrlParser: true, useUnifiedTopology: true}); +mongoose.connection.on('error', console.error.bind(console, 'error:')); +mongoose.connection.once('open', function() { + console.log('Connected to database'); +}); + + +mongoose.set('useFindAndModify', false); +const userSchema = new mongoose.Schema({ + email: String, + password: String, + username: String, + +}); +const userModel = mongoose.model('user', userSchema); + + +const listsSchema = new mongoose.Schema({ + "name" : "string", + "user_id": {type:mongoose.Schema.Types.ObjectId,ref:'users'} , + "created_at": "string", + "updated_at": "string" +}); +const lists = mongoose.model('lists', listsSchema); +const categorySchema = new mongoose.Schema({ + + name: String, + "user_id":{type:mongoose.Schema.Types.ObjectId,ref:'users'}, + "created_at":"string", + "updated_at":"string" + +}) +const category = mongoose.model('category',categorySchema); + +const itemsSchema = new mongoose.Schema ({ + "name": "string", + "category_id": {type:mongoose.Schema.Types.ObjectId,ref:'categories'}, + "user_id": {type:mongoose.Schema.Types.ObjectId,ref:'users'}, + "note": "string", + "image": "string", + "created_at": "string", + "updated_at": "string", + "list":[{type:mongoose.Schema.Types.ObjectId,ref:'lists'}] + + +}); +const items = mongoose.model('items',itemsSchema); + +app.use(express.json()); +app.post('/login', async (req, res) => { + try { + const user = await userModel.findOne({email: req.body.email}); + if(!user){ + res.send('Email Does Not Exist') + } + const match = await bcrypt.compare(req.body.password, user.password); + if(!match){ + res.send('Incorrect Password') + } + res.json({token: jwt.sign({id: user._id}, 'anysecret')}) + } catch (error) { + res.send(error) + } +}); + + + +app.post('/signup', async (req, res) => { + try { + req.body.password = await bcrypt.hash(req.body.password, 12); + const user = await userModel.create(req.body); + res.json({ + message: 'user Added Successfully', + user + }) + } catch (error) { + res.send(' Validation Error') + } +}); + + + +const auth = (req, res, next) => { + const token = req.get('Authorization'); + console.log(token); + if(token && jwt.verify(token, 'thisisasecret')){ + next(); + }else { + res.send('Unauthorized Access') + } + }; + + + +app.get('/getUser',async(req,res)=>{ + try{ + const user= await userModel.find(); + res.json(user); + }catch(error){ + res.send(err) + } +}) + + + +// listss + +app.post('/createList',async(req,res)=>{ +try{ +await lists.create(req.body); +res.send(' created succesfully') + +}catch(error){ + res.send('problem ') +} +}) + +app.get('/getLists',async(req,res)=>{ + res.json(await lists.find()); +}); +app.get('/getLists/:id', async (req, res) =>{ + try { + const use = await lists.findById(req.params.id); + res.json(use); + } catch (error) { + res.send(error); + } +}); + +app.get('/getLists/:id/items', async (req, res) =>{ + try { + const use = await lists.findById(req.params.id); + const x = await items.find({ list:req.params.id }) + res.json({x,use}); + + + + } catch (error) { + res.send(error); + } +}); + +app.post('/getLists/:id/items', async (req, res) =>{ + try{ + const use = await items.findById(req.body.id); + use.list.push(req.params.id); + await use.save(); + res.json(use); + + }catch(error){ + res.send('error') + } + +}) + + + + + + + + +app.delete('/deletelist/:id', async (req, res) =>{ + try { + await lists.findByIdAndRemove(req.params.id); + res.send(' Deleted'); + } catch (error) { + res.send(error); + } +}); + + + + // itemss + + +app.post('/createItem',async(req,res)=>{ + try{ + items.create(req.body); + res.send(" created succesfuuly ") + }catch(error){ +res.send("problem ") + } +}) + +app.get('/getItems',async(req,res)=>{ + res.json(await items.find()); +}); + + +app.put('/updateitem/:id',async(req,res,next)=>{ + await items.findByIdAndUpdate({_id:req.params.id}, + { + $set:{ + name:req.body.name, + category_id:req.body.category_id, + user_id:req.body.user_id, + note:req.body.note, + image:req.body.image, + created_at:req.body.created_at, + updated_at:req.body.updated_at + } + + }).then (result =>{ + res.status(200).json({updated_product:result}) + }).catch(err=>{ res.status(500).json({error:err}) + + }) + + + + +}) + + + +app.delete('/deleteitem/:id',async(req,res)=>{ +try{ + await items.findByIdAndRemove(req.params.id); + res.send(" deleted successfuly") +}catch(error){ + res.send("erorr") +} + +}) + +// categoriess :: + +app.post('/createCategory',async(req,res)=>{ + try{ + await category.create(req.body); + res.send(" created successfuly") + + + + }catch(error){ + res.send('problem ') + } + + +}); +app.get('/getCategories',async(req,res)=>{ + res.json( await category.find()); +}); + +app.put('/updateCategory/:id',async(req,res,next)=>{ + await category.findByIdAndUpdate({_id:req.params.id},{ + + $set:{ + name:req.body.name, + user_id:req.body.user_id, + created_at:req.body.created_at, + updated_at:req.body.updated_at + } + }).then(result=>{ +res.status(200).json({updated_product:result}) + + }).catch(err=>{res.status(500).json({error:err})}) + + +}) + +app.delete('/deleteCategory/:id',async(req,res)=>{ + try{ + await category.findByIdAndRemove(req.params.id); + res.send("deleted successufuly") + + }catch(error){ + res.send('system problem') + } + + +}); + + + + + + +app.listen(3000,()=>{ + console.log("ur own server is listening on port 3000"); +}) \ No newline at end of file From 14edb5ea4079ba85be09d6d044c709fc41ad9e4c Mon Sep 17 00:00:00 2001 From: Mariem166 <74572429+Mariem166@users.noreply.github.com> Date: Sat, 8 May 2021 00:49:49 +0100 Subject: [PATCH 2/2] Add files via upload --- package.json | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 00000000..ef9a9c44 --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + + "name": "assignment-1", + "version": "1.0.0", + "description": "In this Assignment You need to code your own server using ExpressJS.", + "main": "index.js", + "scripts": { + "start": "nodemon index.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/MedTech-CS425/Assignment-1.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/MedTech-CS425/Assignment-1/issues" + }, + "homepage": "https://github.com/MedTech-CS425/Assignment-1#readme", + "dependencies": { + "bcrypt": "^5.0.1", + "dotenv": "^8.2.0", + "express": "^4.17.1", + + "jsonwebtoken": "^8.5.1", + "mongoose": "^5.12.7", + + "nodemon": "^2.0.7" + } + } \ No newline at end of file