Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment-1-KhalilLiouane #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
const express = require('express');
const app = express();
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');

app.use(express.json());

const mongoose = require('mongoose');
const { INSPECT_MAX_BYTES } = require('buffer');
mongoose.connect('mongodb://localhost:27017/api', {useNewUrlParser: true, useUnifiedTopology: true});
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.once('open', function() {
console.log('Connected to database');
});

const UserSchema = new mongoose.Schema({
email: String,
password: String,
userName: String
});
const User = mongoose.model('User',UserSchema);

const auth = (req, res, next) => {
try {
const {id} =jwt.verify(req.get('Authorization'), 'anysecret');
const usr = User.findById(id);
req.user = usr;
next();
} catch(error){
res.send('Unauthorized Access')
}
}

app.post('/signup', async (req, res)=> {
try {
req.body.password = await bcrypt.hash(req.body.password, 12);
await User.create(req.body);
res.send('user Created Succesfully');
} catch (error) {
res.send('Problem with server');
}

});
app.get('/getUser/:id', auth ,async (req, res)=> {
res.json(await User.findById(req.params.id));
});
app.post('/login', async (req, res)=> {
try {
const usr = await User.findOne({email: req.body.email});
const match = await bcrypt.compare(req.body.password, usr.password)
if(!match){
res.send('Wrong Password')
}else{
res.send(jwt.sign({id: usr._id}, 'anysecret'))
}
} catch (error) {
console.error(error);
res.send('Wrong Email')
}
})

const listSchema = new mongoose.Schema({
"name": {type: string, required: true},
"created_at": {type: Date, default: Date.now()},
"updated_at ": {type: Date, default: Date.now()},
"user": {type: Schema.Types.ObjectId, ref: 'User'}
})
const List = mongoose.model("List", listSchema);

//operations over list
app.get('/lists', auth ,async(req, res)=> {
res.json(await List.find());
});

app.post('/api/lists',auth, async (req, res) => {
try {
await List.create(req.body);
res.send('list Created Succesfully');
} catch (error) {
res.send('Problem with server');
}
});

app.put('/lists/:id', auth, async (req, res) => {
await List.updateOne(req.params.id);
}) ;
app.delete('/lists/:id', auth, async (req, res) => {
await User.findByIdAndDelete(req.params.id);
res.send('user Deleted')
});

const itemSchema = new mongoose.Schema({
"neme": string,
"image": string,
"note": string,
"list_id": {type : Schema.Types.ObjectId, ref: 'List'},
"category_id": {type : Schema.Types.ObjectId, ref: 'Category'}
});
const Item = mongoose.model("Item", itemSchema);

app.get('/lists/:id/items', auth, async (req,res) => {
res.json(await List.find({"list_id": req.params.id}));
});
app.post('/lists/:id/items', auth, async (req, res) => {
try{
await Item.create({...req.body, "list_id": req.params.id});
}catch (error) {
res.send('Problem with server');
}
});
app.put('/lists/:id/items', auth, async (req, res) => {
await Item.findByIdAndUpdate(req.params.id, req.body);
}) ;
app.delete('/lists/:id/items', async (req, res, next) => {
await Item.deleteMany(req.params.id);
res.send('Deleted All items')
});

//operations over items

app.get('/item/',auth, async (req, res) => {
res.json(await Item.findById(req.params.id));
});

app.post('/item', async (req, res) => {
try {
await Item.create(req.body);
res.send("item created.");
}catch (error) {
res.send('Problem with server');
}
});
app.put('/item/:id', async (req, res) => {
await Item.findOneAndUpdate({ id: req.body.id },req.body);
res.send("The item is updated.")
});
app.delete('/item/:id', async (req, res) => {
await Item.findOneAndDelete({ id: req.body.id });
res.send("The item is deleted.");
});

const categorySchema = new mongoose.Schema({
"Name": String,
"Created_at": String,
"Updated_at": String,
"user_id": {type : Schema.Types.ObjectId, ref: 'User'}
});
const Category = mongoose.model('Category', categorySchema);

//operations over categories

app.get('/categories/', async (req, res) => {
res.json(await Category.findById(req.params.id));
});
app.post('/categories', async (req, res) => {
try {
await Category.create(req.body);
res.send("The category is created.");
}catch (error) {
res.send('Problem with server');
}
});


app.put('/categories/:id', async (req, res) => {
await Category.findOneAndUpdate({ id: req.body.id },req.body);
res.send(" category updated.")
});

app.delete('/categories', async (req, res) => {
await Category.findOneAndDelete({ id: req.body.id });
res.send("The category is deleted.");
});


app.listen(3000, ()=>{
console.log('App Listening on Port 3000');
})
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "node app.js",
"start:dev": "nodemon"
// "test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/asma-filali/Assignment-1.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/asma-filali/Assignment-1/issues"
},
"homepage": "https://github.com/asma-filali/Assignment-1#readme",
"dependencies": {
"bcrypt": "^5.0.1",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.12.3"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}