Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend support #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions EGA-99-server/app/config/auth.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
secret: "bezkoder-secret-key"
};
13 changes: 13 additions & 0 deletions EGA-99-server/app/config/db.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
HOST: "localhost",
USER: "root",
PASSWORD: "CCNAsahan12#",
DB: "dbeva99",
dialect: "mysql",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
};
87 changes: 87 additions & 0 deletions EGA-99-server/app/controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const db = require("../models/index.js");
const config = require("../config/auth.config");
const User = db.users;
const Role = db.roles;

const Op = db.Sequelize.Op;

var jwt = require("jsonwebtoken");
var bcrypt = require("bcryptjs");

exports.signup = (req, res) => {
// Save User to Database
User.create({
username: req.body.username,
email: req.body.email,
password: bcrypt.hashSync(req.body.password, 8)
})
.then(user => {
if (req.body.roles) {
Role.findAll({
where: {
name: {
[Op.or]: req.body.roles
}
}
}).then(roles => {
user.setRoles(roles).then(() => {
res.send({ message: "User was registered successfully!" });
});
});
} else {
// user role = 1
user.setRoles([1]).then(() => {
res.send({ message: "User was registered successfully!" });
});
}
})
.catch(err => {
res.status(500).send({ message: err.message });
});
};

exports.signin = (req, res) => {
User.findOne({
where: {
username: req.body.username
}
})
.then(user => {
if (!user) {
return res.status(404).send({ message: "User Not found." });
}

var passwordIsValid = bcrypt.compareSync(
req.body.password,
user.password
);

if (!passwordIsValid) {
return res.status(401).send({
accessToken: null,
message: "Invalid Password!"
});
}

var token = jwt.sign({ id: user.id }, config.secret, {
expiresIn: 86400 // 24 hours
});

var authorities = [];
user.getRoles().then(roles => {
for (let i = 0; i < roles.length; i++) {
authorities.push("ROLE_" + roles[i].name.toUpperCase());
}
res.status(200).send({
id: user.id,
username: user.username,
email: user.email,
roles: authorities,
accessToken: token
});
});
})
.catch(err => {
res.status(500).send({ message: err.message });
});
};
147 changes: 147 additions & 0 deletions EGA-99-server/app/controllers/topic.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
const db = require("../models");
const Topic = db.topics;
const Op = db.Sequelize.Op;

// Create and Save a new Topic
exports.create = (req, res) => {
// Validate request
if (!req.body.title) {
res.status(400).send({
message: "Content can not be empty!"
});
return;
}

// Create a Topic
const topic = {
title: req.body.title,
description: req.body.description,
subscription: req.body.subscription,
created: req.body.created ? req.body.created : false
};

// Save Topic in the database
Topic.create(topic)
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Tutorial."
});
});
};

// Retrieve all Topics from the database.
exports.findAll = (req, res) => {
const title = req.query.title;
var condition = title ? { title: { [Op.like]: `%${title}%` } } : null;

Topic.findAll({ where: condition })
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving topics."
});
});
};

// Find a single Topic with an id
exports.findOne = (req, res) => {
const id = req.params.id;

Topic.findByPk(id)
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message: "Error retrieving Topic with id=" + id
});
});
};

// Update a Topic by the id in the request
exports.update = (req, res) => {
const id = req.params.id;

Topic.update(req.body, {
where: { id: id }
})
.then(num => {
if (num == 1) {
res.send({
message: "Topic was updated successfully."
});
} else {
res.send({
message: `Cannot update Topic with id=${id}. Maybe Topic Source was not found or req.body is empty!`
});
}
})
.catch(err => {
res.status(500).send({
message: "Error updating Topic with id=" + id
});
});
};

// Delete a Topic with the specified id in the request
exports.delete = (req, res) => {
const id = req.params.id;

Topic.destroy({
where: { id: id }
})
.then(num => {
if (num == 1) {
res.send({
message: "Topic was deleted successfully!"
});
} else {
res.send({
message: `Cannot delete Topic with id=${id}. Maybe Topic was not found!`
});
}
})
.catch(err => {
res.status(500).send({
message: "Could not delete Topic with id=" + id
});
});
};

// Delete all Topic from the database.
exports.deleteAll = (req, res) => {
Topic.destroy({
where: {},
truncate: false
})
.then(nums => {
res.send({ message: `${nums} Topics were deleted successfully!` });
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while removing all topics."
});
});
};

// Find all published Topics
exports.findAllCreated = (req, res) => {
Topic.findAll({ where: { created: true } })
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving topics."
});
});
};
Loading