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
1 change: 1 addition & 0 deletions Server/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SECRET_kEY = ""
Empty file added Server/.envexample
Empty file.
2 changes: 2 additions & 0 deletions Server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
105 changes: 105 additions & 0 deletions Server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}

const express = require("express");
const cors = require("cors");
const PublicController = require("./controllers/PublicController");
const UserController = require("./controllers/UserController");
const Authorization = require("./middlewares/authorization");
const Authentication = require("./middlewares/authentication");
const MechanicController = require("./controllers/MechanicController");
const PostController = require("./controllers/PostController");
const errorHandler = require("./middlewares/errorHandler");
const OrderController = require("./controllers/OrderController");

const app = express();
const PORT = process.env.PORT || 3000;

app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());

app.get("/", PublicController.getAllMechanics);
app.get("/pubposts", PublicController.getAllPosts);
app.post("/register", UserController.register);
app.post("/login", UserController.login);

app.get(
"/home",
Authentication.AuthenticationUser,
PublicController.getAllMechanics
);
app.get(
"/postsUser",
Authentication.AuthenticationUser,
PublicController.getAllPosts
);
app.get(
"/mechanics/:id",
Authentication.AuthenticationUser,
PublicController.getMechanicById
);
app.post(
"/orders",
Authentication.AuthenticationUser,
OrderController.createOrder
);
app.get(
"/orders",
Authentication.AuthenticationUser,
OrderController.getAllOrders
);
app.delete(
"/orders/:id",
Authentication.AuthenticationUser,
OrderController.deleteOrder
);
app.put(
"/orders/:id",
Authentication.AuthenticationUser,
OrderController.updateOrder
);
app.post(
"/orders/complete/:id",
Authentication.AuthenticationUser,
OrderController.completeOrder
);
app.post(
"/orders/donepayment/:id",
Authentication.AuthenticationUser,
OrderController.donePayment
);

app.post("/mechanicsLogin", MechanicController.mechanicLogin);
app.get(
"/dashboard",
Authentication.Authentication,
Authorization.mechanicOnlyPostAccess,
PostController.getAllPosts
);
app.post(
"/posts",
Authentication.Authentication,
Authorization.mechanicOnlyPostAccess,
PostController.create
);
app.put(
"/posts/:id",
Authentication.Authentication,
Authorization.mechanicOnlyPostAccess,
Authorization.mechanicPostOwnership,
PostController.update
);
app.delete(
"/posts/:id",
Authentication.Authentication,
Authorization.mechanicOnlyPostAccess,
Authorization.mechanicPostOwnership,
PostController.delete
);

app.use(errorHandler);
app.listen(PORT, () => {
console.log(`Server is running on port http://localhost:${PORT}`);
});
19 changes: 19 additions & 0 deletions Server/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"development": {
"username": "postgres",
"password": "postgres",
"database": "E-Mechanics_DB",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"use_env_variable": "DATABASE_URL"
}
}
33 changes: 33 additions & 0 deletions Server/controllers/MechanicController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const {Mechanic} = require('../models');
const { comparePassword } = require("../helpers/bcrypt");
const { createToken } = require("../helpers/jwt");

module.exports = class MechanicController {
static async mechanicLogin(req, res, next){
const { email, password } = req.body;
if (!email) {
throw { name: "BadRequest", message: "Email is required" };
}
if (!password) {
throw { name: "BadRequest", message: "Password is required!" };
}

try {
const mechanic = await Mechanic.findOne({ where: { email } });
if (!mechanic) {
throw { name: "Unauthorized", message: "Email/password is required" };
}

const isValidPassword = comparePassword(password, mechanic.password);
if (!isValidPassword) {
throw { name: "Unauthorized", message: "Email/password is required" };
}

const access_token = createToken({ id: mechanic.id, email: mechanic.email });

res.status(200).json({ access_token });
} catch (err) {
next(err);
}
}
}
150 changes: 150 additions & 0 deletions Server/controllers/OrderController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
const { Op } = require('sequelize')
const { Order, Mechanic, User, Package} = require('../models'); // Adjust the path as necessary

module.exports = class OrderController {
static async getAllOrders(req, res, next) {
try {
const orders = await Order.findAll({
include: [
{
model: Mechanic,
attributes: ['id', 'fullName'],
},
{
model: User,
attributes: ['id', 'email'],
},
{
model: Package,
attributes: ['id', 'namePackage', 'price'],
}
],
});

res.status(200).json(orders);
} catch (error) {
console.error('Error in getAllOrders:', error); // Log the error for debugging
next(error);
}
}

static async createOrder(req, res, next) {
try {
const { description, status, date, MechanicId, PackageId } = req.body;
const UserId = req.user.id; // Assuming the user ID is stored in req.user

const newOrder = await Order.create({
description,
status,
date,
UserId,
MechanicId,
PackageId
});

res.status(201).json({
message: "Order created successfully",
data: newOrder
});
} catch (error) {
console.error('Error in createOrder:', error); // Log the error for debugging
next(error);
}
}

static async updateOrder(req, res, next) {
try {
const { id } = req.params;
const { description, status, date, MechanicId, PackageId } = req.body;

const order = await Order.findByPk(id);

if (!order) {
return res.status(404).json({ message: "Order not found" });
}

await order.update({
description,
status,
date,
MechanicId,
PackageId
});

res.status(200).json({
message: "Order updated successfully",
data: order
});
} catch (error) {
console.error('Error in updateOrder:', error); // Log the error for debugging
next(error);
}
}

static async completeOrder(req, res, next) {
try {
const { id } = req.params;

const order = await Order.findByPk(id);

if (!order) {
return res.status(404).json({ message: "Order not found" });
}

await order.update({ status: 'completed' });

res.status(200).json({
message: "Order completed successfully",
data: order
});
} catch (error) {
console.error('Error in completeOrder:', error); // Log the error for debugging
next(error);
}
}

static async donePayment(req, res, next) {
try {
const { id } = req.params;

const order = await Order.findByPk(id);

if (!order) {
return res.status(404).json({ message: "Order not found" });
}

await order.update({ status: 'donePayment' });

res.status(200).json({
message: "Payment done successfully",
data: order
});
} catch (error) {
console.error('Error in donePayment:', error); // Log the error for debugging
next(error);
}
}

static async deleteOrder(req, res, next) {
try {
const { id } = req.params;

const order = await Order.findByPk(id);

if (!order) {
return res.status(404).json({ message: "Order not found" });
}

await order.destroy();

res.status(200).json({
message: "Order deleted successfully",
data: order
});
} catch (error) {
console.error('Error in deleteOrder:', error); // Log the error for debugging
next(error);
}
}

}
Loading