Commit c0f13678 authored by Sandeep Fernando's avatar Sandeep Fernando

project structure change

parent 89345edd
This diff is collapsed.
......@@ -12,7 +12,7 @@
"api",
"garbage"
],
"author": "all",
"author": "GDC",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.7"
......@@ -21,11 +21,11 @@
"bcrypt": "^5.0.1",
"compression": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express-fileupload": "^1.2.1",
"helmet": "^4.6.0",
"mongoose": "^5.13.0",
"multer": "^1.4.2",
"socket.io": "^4.1.2"
"multer": "^1.4.2"
}
}
const port = 5000;
const mongoUri = "mongodb://localhost:27017/garbage-detection";
const port = 5500;
const mongoUri = "mongodb+srv://sndp:HK9oyTWrjUShWaTY@clustergdc.4mvlv.mongodb.net/gdcDB?retryWrites=true&w=majority";
module.exports = {
port,
......
const multer = require("multer");
const path = require("path");
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, "../public/uploads"));
},
filename: function (req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname));
}
})
const upload = multer({ storage: storage });
module.exports = {
upload
}
\ No newline at end of file
const bcrypt = require("bcrypt");
const { UserModel } = require("../models/user.model");
const login = async (req, res) => {
try {
const user = await UserModel.findOne({ email: req.body.email });
if (user && bcrypt.compareSync(req.body.password, user.password)) {
res.status(200).json({
status: "AUTHORIZED",
data: user
})
} else {
res.status(401).json({
status: "UNAUTHORIZED"
})
}
} catch (err) {
res.status(401).json({
status: "UNAUTHORIZED"
})
}
}
module.exports = {
login
}
require("express");
const { ChatModel } = require("../models/chat.model");
const createChat = async (req, res) => {
let formData = {
chat_name: req.body.chat_name,
chat_image: req.body.chat_image,
chat_members: [req.body.userId],
created_by: req.body.userId,
}
try {
let createdChat = await ChatModel.create(formData);
res.status(200).json({ data: createdChat });
} catch (error) {
res.status(500).json({ errors: error });
}
}
const getChatById = async (req, res) => {
try {
let chat = await ChatModel.findOne({ _id: req.params.id });
if (chat) {
res.status(200).json({ data: chat });
} else {
res.status(404).json({ data: "Chat not found" });
}
} catch (error) {
res.status(500).json({ errors: error })
}
}
const getChatsByUserId = async (req, res) => {
try {
let chats = await ChatModel.find();
if (chats) {
res.status(200).json({ data: chats });
} else {
res.status(404).json({ data: "Chats not found" });
}
} catch (error) {
res.status(500).json({ errors: error })
}
}
module.exports = {
createChat,
getChatById,
getChatsByUserId
}
\ No newline at end of file
const express = require("express");
const { ReportModel } = require("../models/report.model");
const createReport = async (req, res) => {
let formData = {
recordId: req.body.recordId,
result: req.body.result,
from: req.body.recordId.split('_')[0],
created_at: req.body.created_at
}
try {
let createdReport = await ReportModel.create(formData);
res.status(200).json({ data: createdReport });
} catch (error) {
res.status(500).json({ errors: error });
}
}
const getReports = async (req, res) => {
try {
let reports = await ReportModel.find({}, null,
{ sort: { created_at: 1 }, limit: null },
);
res.status(200).json({ data: reports });
} catch (error) {
res.status(500).json({ errors: error })
}
}
const getReportById = async (req, res) => {
try {
let report = await ReportModel.findOne({ _id: req.params.id });
if (report) {
res.status(200).json({ data: report });
} else {
res.status(404).json({ data: "Report not found" });
}
} catch (error) {
res.status(500).json({ errors: error })
}
}
const updateReport = async (req, res) => {
let formData = {
result: req.body.result,
updated_at: new Date()
}
try {
let report = await ReportModel.findOne({ _id: req.params.id });
if (report) {
let updatedReport = Object.assign(report, formData);
updatedReport = await updatedReport.save();
res.status(200).json({ data: updatedReport });
} else {
res.status(404).json({ error: "Report not found" });
}
} catch (error) {
res.status(500).json({ errors: error });
}
}
const deleteReport = async (req, res) => {
try {
let report = await ReportModel.findOne({ _id: req.params.id });
if (report) {
await ReportModel.deleteOne({ _id: req.params.id });
res.status(202).json({ data: {} });
} else {
res.status(404).json({ error: "Report not found" });
}
} catch (error) {
res.status(500).json({ errors: error });
}
}
const getReportsByUser = async (req, res) => {
try {
let reports = await ReportModel.find({ from: req.params.id })
if (reports) {
res.status(202).json({ data: reports });
} else {
res.status(404).json({ error: "Reports not found" });
}
} catch (error) {
res.status(500).json({ errors: error });
}
}
module.exports = {
createReport,
getReports,
getReportById,
updateReport,
deleteReport,
getReportsByUser
}
\ No newline at end of file
// const bcrypt = require("bcrypt");
// const { UserModel } = require("../models/user.model");
const { RoleModel } = require("../models/role.model");
const createRole = async (req, res) => {
const formData = {
role_name: req.body.role_name,
rank: req.body.rank
}
try {
const createdRole = await RoleModel.create(formData);
res.status(201).json({ data: createdRole });
} catch (error) {
res.status(500).json({ errors: error });
}
}
const getAllRoles = async (req, res) => {
try {
const roles = await RoleModel.find();
res.status(200).json({ data: roles });
} catch (error) {
res.status(500).json({ errors: error });
}
}
const getRoleById = async (req, res) => {
try {
const role = await RoleModel.findOne({ _id: req.params.id })
.select("_id role_name rank status created_at updated_at");
if (role) {
res.status(200).json({ data: role });
} else {
res.status(404).json({ error: "Role not found " });
}
} catch (error) {
res.status(500).json({ errors: error });
}
}
const updateRole = async (req, res) => {
const formData = {
role_name: req.body.role_name,
rank: req.body.rank,
status: req.body.status,
updated_at: new Date()
}
if (!req.body.role_name) {
delete formData.role_name;
}
if (!req.body.rank) {
delete formData.rank;
}
if (!req.body.status) {
delete formData.status;
}
try {
const role = await RoleModel.findOne({ _id: req.params.id });
if (role) {
let updatedRole = Object.assign(role, formData);
updatedRole = await updatedRole.save();
res.status(200).json({ data: updatedRole });
} else {
res.status(404).json({ error: "Role not found " });
}
} catch (error) {
res.status(500).json({ errors: error });
}
}
const deleteRole = async (req, res) => {
try {
const role = await RoleModel.findOne({ _id: req.params.id });
if (role) {
await RoleModel.deleteOne({ _id: req.params.id });
res.status(204).json({ data: {} });
} else {
res.status(404).json({ error: "Role not found " });
}
} catch (error) {
res.status(500).json({ errors: error });
}
}
module.exports = {
createRole,
getAllRoles,
getRoleById,
updateRole,
deleteRole
}
\ No newline at end of file
const express = require("express");
const fs = require('fs');
const path = require("path");
// If file was missing send default
const normname = "default.png";
const normpath = path.join(__dirname, "../public/static", normname);
const fileUpload = (req, res) => {
res.status(200).json({
data: {
filename: req.file.filename
}
});
}
const fileDownload = async (req, res) => {
try {
const filename = req.params.filename;
const filepath = path.join(__dirname, "../public/uploads", filename);
if (fs.existsSync(filepath)) {
res.status(200).sendFile(filepath, filename);
} else {
res.status(200).sendFile(normpath, normname);
}
} catch (error) {
res.status(500).json({ errors: error });
}
}
module.exports = {
fileUpload,
fileDownload
}
\ No newline at end of file
// Utils
const bcrypt = require("bcrypt");
// Models
const { UserModel } = require("../models/user.model");
const createUser = async (req, res) => {
const formData = {
full_name: req.body.full_name,
email: req.body.email,
password: bcrypt.hashSync(req.body.password, 10),
}
let user = req.body;
if (user.password !== user.repeat) {
res.status(401).json({ errors: "passwords do not match" });
} else {
const formData = {
email: user.email,
password: bcrypt.hashSync(user.password, 10),
}
try {
const createdUser = await UserModel.create(formData);
try {
const createdUser = await UserModel.create(formData);
res.status(201).json({ data: createdUser });
} catch (error) {
res.status(500).json({ errors: error });
res.status(201).json({ data: createdUser });
} catch (error) {
res.status(500).json({ errors: error });
}
}
}
......@@ -48,6 +54,7 @@ const updateUser = async (req, res) => {
const formData = {
full_name: req.body.full_name,
email: req.body.email,
role: req.body.role,
updated_at: new Date()
}
......@@ -59,6 +66,10 @@ const updateUser = async (req, res) => {
delete formData.email;
}
if (!req.body.role) {
delete formData.role;
}
try {
const user = await UserModel.findOne({ _id: req.params.id });
......
......@@ -2,7 +2,6 @@ const express = require("express");
const compression = require("compression");
const cors = require("cors");
const helmet = require("helmet");
const fileupload = require("express-fileupload");
const mongoose = require("mongoose");
const http = require("http");
const path = require("path");
......@@ -12,8 +11,12 @@ const { port, mongoUri } = require("./configs/configs");
// Importing routes
const userRoutes = require("./routes/user.routes");
const authRoutes = require("./routes/auth.routes");
const roleRoutes = require("./routes/role.routes");
const chatRoutes = require("./routes/chat.routes");
const reportRoutes = require("./routes/report.routes");
const locationRoutes = require("./routes/location.routes");
const fileUploadRoutes = require("./routes/file-upload.routes");
const uploadRoutes = require("./routes/upload.routes");
// Initialize
const app = express();
......@@ -25,21 +28,25 @@ app.use(express.urlencoded({ extended: true }));
app.use(compression());
app.use(cors());
app.use(helmet());
app.use(fileupload());
// Public route
app.use("/public", express.static(path.join(__dirname, "public")));
// Configuring routes
app.use("/api/users", userRoutes.router);
app.use("/api/auth", authRoutes.router);
app.use("/api/chats", chatRoutes.router);
app.use("/api/reports", reportRoutes.router);
app.use("/api/locations", locationRoutes.router);
app.use("/api/uploads", fileUploadRoutes.router);
app.use("/api/roles", roleRoutes.router);
app.use("/api/uploads", uploadRoutes.router);
// Web route
app.get("*", (req, res) => {
res.status(200).send("Garbage Detection System");
})
res.status(200).send("Garbage Detection Client");
});
// Connecting to Database
......@@ -55,4 +62,4 @@ mongoose.connect(mongoUri, { useNewUrlParser: true, useUnifiedTopology: true, us
// Starting the Server
server.listen(port, () => {
console.log(`Server has started on ${port}`);
})
\ No newline at end of file
});
\ No newline at end of file
const mongoose = require("mongoose");
const ChatSchema = mongoose.Schema({
chat_name: {
type: String,
default: "default",
},
chat_image: {
type: String,
default: "null",
},
chat_members: [{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
}],
chat_type: {
type: Number,
default: 0,
},
chat_status: {
type: Boolean,
default: true
},
created_by: {
type: String,
default: "null",
},
chat_lastMessage: {
type: mongoose.Schema.Types.ObjectId,
ref: "Message",
},
created_on: {
type: Date,
default: Date.now
},
updated_at: {
type: Date
}
})
module.exports = {
"ChatModel": mongoose.model("Chat", ChatSchema)
}
const mongoose = require("mongoose");
const ReportSchema = mongoose.Schema({
recordId: {
type: String,
default: null
},
from: {
type: String,
default: null
},
result: {
type: String,
default: null
},
created_at: {
type: Date,
default: Date.now
},
updated_at: {
type: Date
}
})
module.exports = {
"ReportModel": mongoose.model("Report", ReportSchema)
}
const mongoose = require("mongoose");
const RoleSchema = mongoose.Schema({
role_name: {
type: String,
required: "Role Name is required",
unique: "Role already exists"
},
rank: {
type: Number,
required: "Rank is required"
},
status: {
type: Boolean,
default: true,
},
created_at: {
type: Date,
default: Date.now
},
updated_at: {
type: Date,
}
});
module.exports = {
"RoleModel": mongoose.model("Role", RoleSchema)
}
......@@ -3,7 +3,7 @@ const mongoose = require("mongoose");
const UserSchema = mongoose.Schema({
full_name: {
type: String,
required: "Full Name is required"
default: "",