Skip to content

Commit

Permalink
Server Side
Browse files Browse the repository at this point in the history
  • Loading branch information
justinlucky committed Jan 3, 2024
1 parent 3f21fb0 commit 0cfd0b9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 13 deletions.
37 changes: 37 additions & 0 deletions Controllers/UsersController.js
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
const User = require('../Models/Users/user.js');

exports.signup = async (req, res) => {
try {
const { username, email, mobile, password } = req.body;
const newUser = new User({ username, email, mobile, password });
const savedUser = await newUser.save();
res.json(savedUser);
} catch (error) {
console.error('Error during signup:', error);
res.status(500).json({ error: 'Internal server error' });
}
};

exports.login = async (req, res) => {
try{
const {username, email, mobile, password} = req.body;

if(!(username || email || mobile)){
return res.status(400).json({ error: 'Please provide username, email, or mobile for login' });
}
const user = await User.findOne({ $or: [{ username }, { email }, { mobile }] });

if (!user) {
return res.status(401).json({ error: 'Invalid credentials' });
}
const isPasswordMatch = await user.comparePassword(password);

if (isPasswordMatch) {
res.json({ message: 'Login successful', user });
} else {
res.status(401).json({ error: 'Invalid credentials' });
}
} catch (error) {
console.error('Error during login:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
46 changes: 46 additions & 0 deletions Models/Products/Product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const mongoose = require('mongoose');

const productSchema = new mongoose.Schema({
productID: String,
productName: String,
productImage: [
{
image:[String],
image_URL: [String],
image_ALT: [String],
},
{
image:[String],
image_URL: [String],
image_ALT: [String],
},
{
image:[String],
image_URL: [String],
image_ALT: [String],
},
{
image:[String],
image_URL: [String],
image_ALT: [String],
},
{
image:[String],
image_URL: [String],
image_ALT: [String],
},
],
productPrice: String,
productAvailability: Boolean,
productAvailableSizes: [String],
productDescription: String,
productMaker: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Maker',
required: true,
},
});

const Product = mongoose.model('Product', productSchema);

module.exports = Product;
10 changes: 10 additions & 0 deletions Models/Products/ProductMaker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mongoose = require('mongoose');

const productMaker = new mongoose.Schema({
makerName: String,
makerLocation: String,
});

const Maker = mongoose.model('Maker', productMaker);

module.exports = Maker;
16 changes: 3 additions & 13 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const mongoose = require('mongoose');
const cors = require('cors');
const port = process.env.PORT || 7000;
const config = require('./Config/config.js');
const User = require('./Models/Users/user.js');
const userController = require('./Controllers/UsersController.js')

const app = express();
app.use(express.json());
Expand Down Expand Up @@ -35,18 +35,8 @@ process.on('SIGINT', () => {
});
});

app.post('/signup', async (req, res) => {
try {
const { username, email, mobile, password } = req.body;
const newUser = new User({ username, email, mobile, password });
const savedUser = await newUser.save();
res.json(savedUser);
} catch (error) {
console.error('Error during signup:', error);
res.status(500).json({ error: 'Internal server error' });
}
});

app.post('/signup', userController.signup);
app.post('/login', userController.login);

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
Expand Down

0 comments on commit 0cfd0b9

Please sign in to comment.