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 2 #14

Open
wants to merge 7 commits 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
28 changes: 28 additions & 0 deletions Assignment-2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Assignment #2 & #3

Assignment: Create my Shoppingify app. Use any front-end libraries of your choice. Link it to your API that was done on assignment #1. Fulfill user stories below:

User story 1: When I select the items tab, I can see a list of items under different categories.
User story 2: I can add a new item with name, category, note, and image.
User story 3: When I add a new item, I can select one from the existing categories or add a new one if the category does not exist
User story 4: When I select an item, I can see its details and I can choose to add the current list or delete the item.
User story 5: I can add items to the current list
User story 6 (optional): I can increase the number of item in the list
User story 7: I can remove the item from the list
User story 8: I can save/update the list with a name (user can have only one active list at a time)
User story 9: I can toggle between editing state and completing state
User story 10: When I am at completing state, I can save my progress by selecting the item
User story 11: I can cancel the active list
User story 12: When I try to cancel a list, I can see a confirmation notification
User story 13: I can see my shopping history and I can see the details of it
User story 14 (optional): I can see some statistics: top items, top categories, and monthly comparison. (Tips: use libraries like recharts for the graph)
User story (optional): I can search for items

Here is the link to the Figma Design that you need to implement. Also here is the list of steps that you need to follow to achieve this assignment:

Step 1: Fork and Clone the repository on Github
Step 2: Copy resources, README.md to your repository
Step 3: Login to Figma to checkout font, color, spacing,..
Step 4: Complete all required user stories
Step 5: Update README.md
Step 6: Deploy your app and submit your solution
291 changes: 291 additions & 0 deletions Assignment-2/backend/app.js
Original file line number Diff line number Diff line change
@@ -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(5000,()=>{
console.log("ur own server is listening on port 5000");
})
Loading