Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
4 changes: 4 additions & 0 deletions config/config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

NODE_ENV = development
PORT = 8080
MONGO_URI =mongodb+srv://syrine:caravate14@assign.c3klc.mongodb.net/assignmentgi?retryWrites=true&w=majority
39 changes: 39 additions & 0 deletions config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Our connection with database



const mongoose = require('mongoose');



const connectDB = async () => {

try {

const conct = await mongoose.connect(process.env.MONGO_URI, {

useNewUrlParser: true,

useCreateIndex: true,

useUnifiedTopology: true

});



console.log(`MongoDB Connected: ${conct.connection.host}`);

} catch (err) {

console.log(`Error: ${err.message}`);

process.exit(1);

}

}



module.exports = connectDB
16 changes: 16 additions & 0 deletions middlewares/verifyToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Middleware for auth

const jwt=require("jsonwebtoken");
const secret=process.env.SECRET || 'secret';

const verifyToken = (req, res, next) => {
try {
let decodedToken=jwt.verify(req.get('Authorization'), secret);
req.decodedToken=decodedToken;
next();
} catch (error) {
res.status(401).send({status:401,message:"Unauthorized"});
}
}

module.exports=verifyToken;
22 changes: 22 additions & 0 deletions models/category.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose=require('mongoose');

const categorySchema=mongoose.Schema({
name:String,
user_id:{
type:mongoose.Schema.Types.ObjectId,
ref:'users'
},
createdAt:{
type:Date,
default:Date.now()

},
updatedAt:{
type:Date,
default:Date.now()
}


})

module.exports=mongoose.model('category',categorySchema)
36 changes: 36 additions & 0 deletions models/item.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const mongoose=require('mongoose');

const itemSchema=mongoose.Schema({

name:String,
user_id:{
type:mongoose.Schema.Types.ObjectId,
ref:'users'

},
category_id:{
type:mongoose.Schema.Types.ObjectId,
ref:'categorys'
},
note:String


,
image:String,

createdAt:{
type:Date,
default:Date.now()

},
updatedAt:{
type:Date,
default:Date.now()
}
});
itemSchema.post('remove',itemId=>{
let cleanUp=require("../middlewares/cleanUp");
cleanUp(itemId._id);
//still doesnt work
})
module.exports=mongoose.model('item',itemSchema)
23 changes: 23 additions & 0 deletions models/list.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const mongoose=require('mongoose');

const listSchema=mongoose.Schema({
name:{
type:String,
required:true
},
user_id:{
type:mongoose.Schema.Types.ObjectId,
ref:'users'
},
createdAt:{
type:Date,
default:Date.now()
},
updatedAt:{
type:Date,
default:Date.now()
},
items:[{type:mongoose.Schema.Types.ObjectId,ref:'items'}]

})
module.exports=mongoose.model('list',listSchema);
20 changes: 20 additions & 0 deletions models/user.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose = require("mongoose");

const userSchema=new mongoose.Schema({
email: {
type: String,
required: true,
unique: true,
trim: true,
lowercase: true,
},

password: {
type: String,
required: true,
trim: true,
},
userName: { type: String, required: true, trim: true },
});

module.exports=mongoose.model('user',userSchema);
Loading