Skip to content

Commit

Permalink
Merge pull request #4 from adnan-mujagic/adnan-mujagic-dev
Browse files Browse the repository at this point in the history
Cleaning up
  • Loading branch information
amarell authored Apr 22, 2022
2 parents 6852c0f + ed62090 commit ebd7a99
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 26 deletions.
7 changes: 7 additions & 0 deletions controllers/productController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Product = require("../models/productModel");

module.exports.createProduct = async (req, res) => {};

module.exports.updateProduct = async (req, res) => {};

module.exports.deleteProduct = async (req, res) => {};
22 changes: 11 additions & 11 deletions controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
const e = require("express");
const { entityFactory } = require("../entityFactory");
const User = require("../models/userModel");
const { registerValidation, loginValidation } = require("./../validation");
const jwt = require("./../utilities/jwt");
const { updateEntity } = require("../utilities/updateEntity");

module.exports.register = async (req, res) => {
const errors = registerValidation(req.body);
if (errors.length > 0) {
return res.status(400).json({
errors,
message: errors.join(". "),
});
}

const emailExists = await User.findOne({ email: req.body.email });
if (emailExists) {
return res.status(400).json({
error: "This email is already in use.",
message: "This email is already in use",
});
}

const user = new User();
entityFactory(user, req.body);
updateEntity(user, req.body);

await user.save((err, savedUser) => {
if (err) {
return res.status(400).json({
status: "Something went wrong",
message: "Something went wrong",
});
} else {
return res.status(200).json({
data: savedUser,
message: "User registered successfully",
token: jwt.sign(savedUser),
});
}
});
Expand All @@ -40,28 +40,28 @@ module.exports.login = async (req, res) => {

if (errors.length > 0) {
return res.status(400).json({
errors,
message: errors.join(". "),
});
}

const user = await User.findOne({ email: req.body.email });

if (!user) {
return res.status(400).json({
error: "The user with this email address does not exist",
message: "The user with this email address does not exist",
});
}

if (user.password !== req.body.password) {
return res.status(400).json({
error: "Email or password is incorrect",
message: "Email or password is incorrect",
});
}

let token = jwt.sign(user);

return res.status(200).json({
success: true,
message: "User logged in successfully",
token,
});
};
7 changes: 0 additions & 7 deletions entityFactory.js

This file was deleted.

2 changes: 2 additions & 0 deletions server.js → index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require("dotenv").config();

// Import routes here
let authRoutes = require("./routes/auth");
let productRoutes = require("./routes/prouductRoutes");

const app = express();

Expand All @@ -27,6 +28,7 @@ app.use(express.json());
// Here we will add all the routes!
// app.use("/api", userRoute)
app.use("/", authRoutes);
app.use("/products", productRoutes);

const mongo = mongoose.connect(process.env.DB_PATH, config.DB_OPTIONS);

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Online tech shop backend",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"repository": {
"type": "git",
Expand Down
11 changes: 11 additions & 0 deletions routes/prouductRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const {
createProduct,
deleteProduct,
updateProduct,
} = require("../controllers/productController");

const router = require("express").Router();

router.route("/").post(createProduct).put(updateProduct).delete(deleteProduct);

module.exports = router;
7 changes: 7 additions & 0 deletions utilities/updateEntity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports.updateEntity = (entity, updates, forbiddenFields = []) => {
Object.keys(updates).forEach((key) => {
if (!forbiddenFields.includes(key)) {
entity[key] = updates[key];
}
});
};
2 changes: 1 addition & 1 deletion utilities/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const validateMissingFields = (required_fields, data) => {
msg = msg + missing_field + ", ";
});
msg = msg.substring(0, msg.length - 2);
msg = msg + " is/are required. ";
msg = msg + " is/are required";
return msg;
}
return 0;
Expand Down
11 changes: 5 additions & 6 deletions validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ const registerValidation = (data) => {
let missing_fields = validateMissingFields(required_fields, data);

if (missing_fields) {
errors.push({ error: missing_fields });
errors.push(missing_fields);
}

if (data.username && data.username.length <= 4) {
errors.push({ error: "The username has to be at least 4 chars long" });
errors.push("The username has to be at least 4 characters long");
}

if (data.password && !passwordRegex.test(data.password)) {
errors.push({
error:
"The password must have at least 8 chars, including at least 1 special character and at least 1 digit",
});
errors.push(
"The password must have at least 8 characters, including at least 1 special character and at least 1 digit"
);
}

return errors;
Expand Down

0 comments on commit ebd7a99

Please sign in to comment.