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
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#ignore dependencies
node_modules
36 changes: 36 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const express = require('express');

const mongoose = require('mongoose');


const app = express();

app.use(express.json());




//Connection to db
//Feel free to add your own link & name for db
mongoose.connect('mongodb:/localhost:27017/Storify',
{ useNewUrlParser: true,
useUnifiedTopology: true })
.then(() => console.log('Connection to MongoDB succeeded.'))
.catch(() => console.log('Connection to MongoDB failed.'));


const authRoutes = require('./routes/Auth');
const listRoutes = require('./routes/Lists');
const categoryRoutes = require('./routes/Categories');
const itemRoutes = require('./routes/Items');


app.use('/api/auth', authRoutes);
app.use('/api/lists', listRoutes);
app.use('/api/categories', categoryRoutes);
app.use('/api/items', itemRoutes);


module.exports = app;


86 changes: 86 additions & 0 deletions backend/controllers/Auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const User = require('../models/User');
const jwt = require('jsonwebtoken');
const bcrypt =require('bcryptjs')

exports.signup = (req, res, next) => {
bcrypt.hash(req.body.password, 10).then(
(hash) => {
const user = new User({
email: req.body.email,
password: hash,
userName: res.body.userName
});
user.save().then(
() => {
res.status(201).json({
message: 'User added successfully!'
});
}
).catch(
(error) => {
res.status(500).json({
error: error
});
}
);
}
);
};

exports.login = (req, res, next) => {
User.findOne({ email: req.body.email }).then(
(user) => {
if (!user) {
return res.status(401).json({
error: new Error('User not found!')
});
}
bcrypt.compare(req.body.password, user.password).then(
(valid) => {
if (!valid) {
return res.status(401).json({
error: new Error('Incorrect password!')
});
}
const token = jwt.sign(
{ userId: user._id },
'RANDOM_TOKEN_SECRET',
{ expiresIn: '24h' });
res.status(200).json({
userId: user._id,
token: token
});
}
).catch(
(error) => {
res.status(500).json({
error: error
});
}
);
}
).catch(
(error) => {
res.status(500).json({
error: error
});
}
);
};

exports.getUser= (req, res, next) => {
User.findOne({
_id: req.params.id
}).then(
(user) => {
res.status(200).json(user);
}
).catch(
(error) => {
res.status(404).json({
error: error
});
}
);
};

74 changes: 74 additions & 0 deletions backend/controllers/Categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const Category= require('../models/Category');
exports.getCategories= (req, res, next) => {
Category.find().then(
(categories) => {
res.status(200).json(categories);
}
).catch(
(error) => {
res.status(400).json({
error: error
});
}
);
};
exports.addCategories= (req, res, next) => {
const category = new Category({
name: req.body.name,
user_id: req.body.user_id,
created_at: req.body.created_at,
updated_at: req.body.updated_at
});
category.save().then(
() => {
res.status(201).json({
message: 'Category added successfully!'
});
}
).catch(
(error) => {
res.status(400).json({
error: error
});
}
);
};
exports.updateCategoriesById= (req, res, next) => {
const category = new Category({
_id: req.body._id,
name: req.body.name,
user_id: req.body.user_id,
created_at: req.body.created_at,
updated_at: req.body.updated_at
});
Category.updateOne({_id: req.params.id}, category).then(
() => {
res.status(201).json({
message: 'Category updated successfully!'
});
}
).catch(
(error) => {
res.status(400).json({
error: error
});
}
);
};


exports.deleteCategoriesById= (req, res, next) => {
Category.deleteOne({_id: req.params.id}).then(
() => {
res.status(200).json({
message: 'Deleted Category!'
});
}
).catch(
(error) => {
res.status(400).json({
error: error
});
}
);
};
79 changes: 79 additions & 0 deletions backend/controllers/Items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const Item = require('../models/Item');

exports.getItems= (req, res, next) => {
Item.find().then(
(items) => {
res.status(200).json(items);
}
).catch(
(error) => {
res.status(400).json({
error: error
});
}
);
};
exports.addItems= (req, res, next) => {
const item = new Item({
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
});
item.save().then(
() => {
res.status(201).json({
message: 'Item added successfully!'
});
}
).catch(
(error) => {
res.status(400).json({
error: error
});
}
);
};
exports.updateItemsById= (req, res, next) => {
const item = new Item({
_id: req.body._id,
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
});
Item.updateOne({_id: req.params.id}, item).then(
() => {
res.status(201).json({
message: 'Item updated successfully!'
});
}
).catch(
(error) => {
res.status(400).json({
error: error
});
}
);
};
exports.deleteItemsById= (req, res, next) => {
Item.deleteOne({_id: req.params.id}).then(
() => {
res.status(200).json({
message: 'Deleted Item!'
});
}
).catch(
(error) => {
res.status(400).json({
error: error
});
}
);
};
Loading