Skip to content

Commit

Permalink
module type to require type
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmd7t committed Jun 5, 2023
1 parent 353e175 commit 9302724
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 92 deletions.
12 changes: 8 additions & 4 deletions config/dbconfig.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { Sequelize } from "sequelize";
import "dotenv/config";
const { Sequelize } = require("sequelize");
const dotenv = require("dotenv");

export const sequelize = new Sequelize(`mysql://${process.env.DB_USERNAME}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`, { dialect: "mysql" })
dotenv.config();

const sequelize = new Sequelize(`mysql://${process.env.DB_USERNAME}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`, { dialect: "mysql" })
// export const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USERNAME, process.env.DB_PASS, { dialect: "mysql", host: 'http://rentr.click/' })

export function init() {
function init() {
//checking if connection is done
sequelize.authenticate().then(() => {
console.log(`Database connected to rntr`)
}).catch((err) => {
console.log(err)
})
}

module.exports = { sequelize, init }
34 changes: 19 additions & 15 deletions controllers/auth.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import sendMail from '../services/emailService.js';
import generateOTP from '../utils/otp.js';
import UserModel from '../models/userModel.js';
import { hash, compare } from '../utils/password.js';
import { generate } from '../utils/token.js';
import responses from '../helpers/responses.js';
const sendMail = require('../services/emailService.js');
const generateOTP = require('../utils/otp.js');
const UserModel = require('../models/userModel.js');
const { hash, compare } = require('../utils/password.js');
const { generate } = require('../utils/token.js');
const responses = require('../helpers/responses.js');

/*---- REGISTER USER ----*/
export const register = async (req, res) => {
const register = async (req, res) => {
try {
const { name, email, password, phone, latitude, longitude } = req.body;
const passwordHash = hash(password);
Expand Down Expand Up @@ -53,7 +53,7 @@ export const register = async (req, res) => {
}

/*---- LOGIN USER ----*/
export const login = async (req, res) => {
const login = async (req, res) => {
try {
const { email, password } = req.body;
const user = await UserModel.findOne({
Expand All @@ -76,7 +76,7 @@ export const login = async (req, res) => {
}

/*---- SEND OTP ----*/
export const sendOTP = async (req, res) => {
const sendOTP = async (req, res) => {
try {
const { email } = req.body;
const otpGenerated = generateOTP();
Expand All @@ -99,7 +99,7 @@ export const sendOTP = async (req, res) => {
}

/*---- VERIFY EMAIL ----*/
export const verifyEmail = async (req, res) => {
const verifyEmail = async (req, res) => {
try {
const { email, otp } = req.body;
const user = await UserModel.findOne({
Expand All @@ -122,7 +122,7 @@ export const verifyEmail = async (req, res) => {
}

/*---- RESET PASSWORD ----*/
export const resetPassword = async (req, res) => {
const resetPassword = async (req, res) => {
try {
const { email, password } = req.body;
const passwordHash = hash(password);
Expand All @@ -141,7 +141,7 @@ export const resetPassword = async (req, res) => {
}

/*---- CHANGE PASSWORD ----*/
export const changePassword = async (req, res) => {
const changePassword = async (req, res) => {
try {
const { password } = req.body;
const passwordHash = hash(password);
Expand All @@ -160,7 +160,7 @@ export const changePassword = async (req, res) => {
}

/*---- CHANGE USER DATA ----*/
export const changeUserData = async (req, res) => {
const changeUserData = async (req, res) => {
try {
const { name, phone, latitude, longitude } = req.body;
console.log(req.userId);
Expand All @@ -186,7 +186,7 @@ export const changeUserData = async (req, res) => {
}

/*---- CHANGE PROFILE IMAGE ----*/
export const changeProfileImage = async (req, res) => {
const changeProfileImage = async (req, res) => {
try {

const image = 'http://127.0.0.1:3000/images/' + req.file.filename;
Expand All @@ -211,7 +211,7 @@ export const changeProfileImage = async (req, res) => {
}

/*---- GET USER DATA ----*/
export const getUserData = async (req, res) => {
const getUserData = async (req, res) => {
try {
const user = await UserModel.findOne({
where: { id: req.userId }
Expand All @@ -224,4 +224,8 @@ export const getUserData = async (req, res) => {
} catch (error) {
return responses.internalServerError(res);
}
}

module.exports = {
login, register, sendOTP, verifyEmail, changeUserData, getUserData, changePassword, resetPassword, changeProfileImage
}
20 changes: 12 additions & 8 deletions controllers/products.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import ProductModel from '../models/productModel.js';
import UserModel from '../models/userModel.js';
import responses from '../helpers/responses.js';
import { Sequelize } from 'sequelize';
const ProductModel = require('../models/productModel.js');
const UserModel = require('../models/userModel.js');
const responses = require('../helpers/responses.js');
const { Sequelize } = require('sequelize');



/*---- ADD PRODUCT ----*/
export const addProduct = async (req, res) => {
const addProduct = async (req, res) => {
try {
const { name, description, price, latitude, longitude, startBooking, endBooking } = req.body;
const image = 'http://127.0.0.1:3000/images/' + req.file.filename;
Expand All @@ -32,7 +32,7 @@ export const addProduct = async (req, res) => {
}

/*---- GET PRODUCT ----*/
export const getProduct = async (req, res) => {
const getProduct = async (req, res) => {
try {
const product = await ProductModel.findOne({ where: { id: req.params.id } });
if (product) {
Expand All @@ -47,7 +47,7 @@ export const getProduct = async (req, res) => {


/*---- GET ALL PRODUCTS ----*/
export const getAllProducts = async (req, res) => {
const getAllProducts = async (req, res) => {
try {
const user = await UserModel.findOne({ where: { id: req.userId } });
if (user) {
Expand Down Expand Up @@ -95,7 +95,7 @@ function deg2rad(deg) {


/*---- SEARCH PRODUCTS ----*/
export const searchProducts = async (req, res) => {
const searchProducts = async (req, res) => {
try {
const Op = Sequelize.Op;
const products = await ProductModel.findAll({ where: { name: { [Op.like]: `%${req.query.name}%` } } });
Expand All @@ -107,4 +107,8 @@ export const searchProducts = async (req, res) => {
} catch (error) {
return responses.internalServerError(res);
}
}

module.exports = {
addProduct, getProduct, getAllProducts, searchProducts
}
18 changes: 11 additions & 7 deletions controllers/reviews.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import responses from '../helpers/responses.js';
import ReviewModel from '../models/reviewModel.js';
import ProductReviewModel from '../models/productReviewModel.js';
const responses = require('../helpers/responses.js');
const ReviewModel = require('../models/reviewModel.js');
const ProductReviewModel = require('../models/productReviewModel.js');



/*---- ADD REVIEW ----*/
export const addReview = async (req, res) => {
const addReview = async (req, res) => {
try {
const { rating } = req.body;
const data = {
Expand Down Expand Up @@ -36,7 +36,7 @@ export const addReview = async (req, res) => {


/*---- ADD PRODUCT REVIEW ----*/
export const addProductReview = async (req, res) => {
const addProductReview = async (req, res) => {
try {
const { rating } = req.body;
const data = {
Expand Down Expand Up @@ -66,7 +66,7 @@ export const addProductReview = async (req, res) => {
}

/*---- GET REVIEWS ----*/
export const getReviews = async (req, res) => {
const getReviews = async (req, res) => {
try {
const reviews = await ReviewModel.findAll({ where: { to_id: req.params.id } });
if (reviews) {
Expand All @@ -81,7 +81,7 @@ export const getReviews = async (req, res) => {
}

/*---- GET PRODUCT REVIEWS ----*/
export const getProductReviews = async (req, res) => {
const getProductReviews = async (req, res) => {
try {
const reviews = await ProductReviewModel.findAll({ where: { to_id: req.params.id } });
if (reviews) {
Expand All @@ -93,4 +93,8 @@ export const getProductReviews = async (req, res) => {
} catch (error) {
return responses.internalServerError(res);
}
}

module.exports = {
addReview, getReviews, getProductReviews, addProductReview
}
2 changes: 1 addition & 1 deletion helpers/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ const responses = {
internalServerError,
};

export default responses;
module.exports = responses;
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const express = require("express");
import cors from "cors";
import helmet from "helmet";
import bodyParser from "body-parser";
import dotenv from "dotenv";
import morgan from "morgan";
import authRoutes from "./routes/auth.js";
import productsRoutes from "./routes/products.js";
import reviewsRoutes from "./routes/reviews.js";
import { init } from "./config/dbconfig.js";
import errorHandler from './middleware/errorHandler.js';
const cors = require("cors");
const helmet = require("helmet");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
const morgan = require("morgan");
const authRoutes = require("./routes/auth.js");
const productsRoutes = require("./routes/products.js");
const reviewsRoutes = require("./routes/reviews.js");
const { init } = require("./config/dbconfig.js");
const errorHandler = require('./middleware/errorHandler.js');

/*---- CONFIGURATIONS ----*/
dotenv.config();
Expand Down
8 changes: 5 additions & 3 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { decode } from '../utils/token.js';
const { decode } = require('../utils/token.js');

export const verifyToken = async (req, res, next) => {
const verifyToken = async (req, res, next) => {
try {
let token = req.headers.authorization;

Expand All @@ -16,4 +16,6 @@ export const verifyToken = async (req, res, next) => {
} catch (error) {
res.status(500).json({ error: error.message });
}
}
}

module.exports = { verifyToken }
4 changes: 2 additions & 2 deletions middleware/errorHandler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import responses from "../helpers/responses.js";
const responses = require("../helpers/responses.js");
function errorHandler(err, req, res, next) {
console.log(err);
responses.internalServerError(res);
}

export default errorHandler;
module.exports = errorHandler;
6 changes: 3 additions & 3 deletions models/productModel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataTypes } from "sequelize";
import { sequelize } from "../config/dbconfig.js";
const { DataTypes } = require("sequelize");
const { sequelize } = require("../config/dbconfig.js");
let ProductModel = sequelize.define("products", {
id: {
type: DataTypes.INTEGER,
Expand Down Expand Up @@ -46,4 +46,4 @@ let ProductModel = sequelize.define("products", {

}, { timestamps: true },);

export default ProductModel;
module.exports = ProductModel;
6 changes: 3 additions & 3 deletions models/productReviewModel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataTypes } from "sequelize";
import { sequelize } from "../config/dbconfig.js";
const { DataTypes } = require("sequelize");
const { sequelize } = require("../config/dbconfig.js");
let ProductReviewModel = sequelize.define("product_reviews", {
id: {
type: DataTypes.INTEGER,
Expand All @@ -21,4 +21,4 @@ let ProductReviewModel = sequelize.define("product_reviews", {
},
}, { timestamps: false },);

export default ProductReviewModel;
module.exports = ProductReviewModel;
6 changes: 3 additions & 3 deletions models/reviewModel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataTypes } from "sequelize";
import { sequelize } from "../config/dbconfig.js";
const { DataTypes } = require("sequelize");
const { sequelize } = require("../config/dbconfig.js");
let ReviewModel = sequelize.define("reviews", {
id: {
type: DataTypes.INTEGER,
Expand All @@ -21,4 +21,4 @@ let ReviewModel = sequelize.define("reviews", {
},
}, { timestamps: false },);

export default ReviewModel;
module.exports = ReviewModel;
6 changes: 3 additions & 3 deletions models/userModel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataTypes } from "sequelize";
import { sequelize } from "../config/dbconfig.js";
const { DataTypes } = require("sequelize");
const { sequelize } = require("../config/dbconfig.js");
let UserModel = sequelize.define("users", {
id: {
type: DataTypes.INTEGER,
Expand Down Expand Up @@ -45,4 +45,4 @@ let UserModel = sequelize.define("users", {

}, { timestamps: true },);

export default UserModel;
module.exports = UserModel;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "nodemon start",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
10 changes: 5 additions & 5 deletions routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from "express";
import { login, register, sendOTP, verifyEmail, changeUserData, getUserData, changePassword, resetPassword, changeProfileImage } from "../controllers/auth.js";
import { verifyToken } from '../middleware/auth.js';
import uploadImage from '../services/uploadImage.js';
const express = require("express");
const { login, register, sendOTP, verifyEmail, changeUserData, getUserData, changePassword, resetPassword, changeProfileImage } = require("../controllers/auth.js");
const { verifyToken } = require('../middleware/auth.js');
const uploadImage = require('../services/uploadImage.js');


const router = express.Router();
Expand All @@ -16,4 +16,4 @@ router.patch("/profile-image", [verifyToken, uploadImage], changeProfileImage);
router.post("/reset-password", resetPassword);
router.post("/change-password", verifyToken, changePassword);

export default router;
module.exports = router;
Loading

0 comments on commit 9302724

Please sign in to comment.