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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions Back-end/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=4000
15 changes: 15 additions & 0 deletions Back-end/Config/Configdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// dbConfig.js
export const dbConfig = {
HOST: "localhost",
USER: "root",
PASSWORD: "",
DB: "exam",
PORT:"3306",
dialect: "mysql",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
};
63 changes: 63 additions & 0 deletions Back-end/Controllers/ArticleController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// RoleController.js
import { db } from "../Models/index.js";

const Article = db.Articles;



// 1. Create new Article
const addArticle = async (req, res) => {
let info = {
image_url: req.body.image_url,
title: req.body.title,
category: req.body.category,
author: req.body.author,
body: req.body.body
};

try {
const article = await Article.create(info);
res.status(200).send(article);
} catch (error) {
console.error("Error creating Article:", error);
res.status(500).send("Internal Server Error");
}
};



// 2. get all articles
const getAllArticles = async (req, res) => {
let articles = await Article.findAll({});
res.status(200).send(articles);
}

// 3. get single article
const getOneArticle = async (req, res) => {
let id = req.params.id;
let article = await Article.findOne({ where: { id: id } });
res.status(200).send(article);
}

// 4. update article
const updateArticle = async (req, res) => {
let id = req.params.id;
const article = await Article.update(req.body, { where: { id: id } });
res.status(200).send(article);
}

// 5. delete article
const deleteArticle = async (req, res) => {
let id = req.params.id;
await Article.destroy({ where: { id: id } });
res.status(200).send('Article deleted');
}

export {
addArticle,
getAllArticles,
getOneArticle,
updateArticle,
deleteArticle

};
96 changes: 96 additions & 0 deletions Back-end/Controllers/UserController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// UserController.js
import { db } from "../Models/index.js";
import bcrypt from "bcrypt";
// import jwt from "jsonwebtoken";
const User = db.Users;
const Transaction =db.Transactions;
const addUser = async (req, res) => {
let info = {
username: req.body.username,
email: req.body.email,
password: req.body.password
};

try {
// Hash the password
const hashedPassword = await bcrypt.hash(info.password, 10);
info.password = hashedPassword;

const user = await User.create(info);

res.status(200).json({
status: "success",
data: user,
});
} catch (error) {
console.error("Error creating User:", error);
res.status(500).send(error.message);
}
};

// 2. get all Users
const getAllUser = async (req, res) => {
try {
// Fetch all users
let users = await User.findAll({
});

// Check if there are no users
if (users.length === 0) {
res.status(404).send({ message: "No users in the database" });
return;
}

res.status(200).send(users);
} catch (error) {
console.error("Error fetching users:", error);
res.status(500).send(error.message);
}
};

// 3. get single User
const getOneUser = async (req, res) => {
let id = req.params.id;
let user = await User.findOne({
where: { id: id }
});
res.status(200).send(user);
};

// 4. update User
const updateUser = async (req, res) => {
let id = req.params.id;
const user = await User.update(req.body, { where: { id: id } });
res.status(200).send(user);
};

// 5. delete User
const deleteUser = async (req, res) => {
let id = req.params.id;
await User.destroy({ where: { id: id } });
res.status(200).send("User deleted");
};

const login = async (req, res, next) => {
const username = req.body.username;
const password = req.body.password;

const user = await User.findOne({ where: { username: username } , include: [{ model: db.Roles, as: 'role' }]});


if (!user || !(await User.comparePassword(password, user.password))) {
const error = res
.status(401)
.json({ message: "please enter a correct username or password" });
return next(error);
}
const token = jwt.sign({ id: user.id, role:user.role.name, username:user.username}, process.env.SECRET_STRING, {
expiresIn: process.env.LOGIN_EXPIRES,
});
res.status(200).json({ message: "success", token ,user:user });
};




export { addUser, getAllUser, getOneUser, updateUser, deleteUser, login };
33 changes: 33 additions & 0 deletions Back-end/Models/ArticleModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// ArticleModel.js
export const createArticleModel = (sequelize, DataTypes) => {
const Article = sequelize.define("Articles", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
image_url: {
type: DataTypes.STRING,
allowNull: false
},
title: {
type: DataTypes.STRING,
allowNull: false
},
category: {
type: DataTypes.STRING,
allowNull: false
},
author: {
type: DataTypes.STRING,
allowNull: false
},
body: {
type: DataTypes.STRING,
allowNull: false
}
}, {
timestamps: true
});
return Article;
};
26 changes: 26 additions & 0 deletions Back-end/Models/UserModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const createUserModel = (sequelize, DataTypes) => {
const User = sequelize.define("Users", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
username: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
},


}, {
timestamps: true
});
return User;
};
42 changes: 42 additions & 0 deletions Back-end/Models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { dbConfig } from "../Config/Configdb.js";
import { Sequelize, DataTypes } from "sequelize";
import {createArticleModel} from "./ArticleModel.js"
import { createUserModel } from './UserModel.js';

const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
port: dbConfig.PORT,
dialect: dbConfig.dialect,
operatorAliases: false,

pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle,
},
});

sequelize
.authenticate()
.then(() => {
console.log("connected to the database");
})
.catch((error) => {
console.error("error connecting: " + error);
});

const db = {};

db.Sequelize = Sequelize;
db.sequelize = sequelize;

db.Articles = createArticleModel(sequelize, DataTypes);
db.Users = createUserModel(sequelize, DataTypes);


db.sequelize.sync({ force: false }).then(() => {
console.log("Database synchronization done!");
});

export { db };
44 changes: 0 additions & 44 deletions Back-end/README.md

This file was deleted.

18 changes: 18 additions & 0 deletions Back-end/Routes/ArticleRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
addArticle,
getAllArticles,
getOneArticle,
updateArticle,
deleteArticle

} from '../Controllers/ArticleController.js';
import { Router } from 'express';
const router = Router();

router.post('/', addArticle);
router.get('/', getAllArticles);
router.get('/:id', getOneArticle);
router.patch('/:id', updateArticle);
router.delete('/:id', deleteArticle);

export default router;
24 changes: 24 additions & 0 deletions Back-end/Routes/UserRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RoleRoute.js
import {
addUser,
getAllUser,
getOneUser,
updateUser,
deleteUser,
login
} from '../Controllers/UserController.js';
import { Router } from 'express';
// import {verifyadmin, verifyToken} from '../middelware/auth.js'

const router = Router();

router.post('/', addUser);
router.get('/',getAllUser);
router.get('/:id', getOneUser);
router.patch('/:id', updateUser);
router.delete('/:id', deleteUser);
router.post('/login', login);



export default router;
1 change: 1 addition & 0 deletions Back-end/node_modules/.bin/color-support

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Back-end/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Back-end/node_modules/.bin/mkdirp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Back-end/node_modules/.bin/node-pre-gyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Back-end/node_modules/.bin/nodemon

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Back-end/node_modules/.bin/nodetouch

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Back-end/node_modules/.bin/nopt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Back-end/node_modules/.bin/rimraf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Back-end/node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading