Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
harish845 authored Sep 10, 2024
0 parents commit 77c1f15
Show file tree
Hide file tree
Showing 99 changed files with 25,820 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Sight-Sense_Project

Members </br>
IT21289316 - Harish.B </br>
IT21272240 - Perera K.P.R.T </br>
IT21258480 - Dissanayake D.M.P.D </br>
IT21269134 - Kumbukgolla K.G.I.H.C </br>
33 changes: 33 additions & 0 deletions backend/controllers/Admin_controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const User = require("../models/User_model");

//Read user data
const readUser = async (req, res) => {
User
.find()
.then((AllUserData) => {
res.json(AllUserData);
})
.catch((err) => {
console.log(err);
});

};

//Delete one user
const deleteUser = async (req, res) => {
const id = req.params.id;
User
.findByIdAndDelete(id)
.then(() => {
res.json("User deleted");
})
.catch((err) => {
console.log(err);
});
};

module.exports = {
deleteUser,
readUser
};

31 changes: 31 additions & 0 deletions backend/controllers/AdvancedTest_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// AdvancedTest_controller.js
const axios = require("axios");

// Azure Custom Vision API Endpoint and Prediction Key
const predictionEndpoint = process.env.SIGHT_PREDICTION_ENDPOINT;
const predictionKey = process.env.SIGHT_PREDICTION_KEY;

// Controller function to upload and analyze the image
const uploadImage = async (req, res) => {
try {
// Send the image to the Azure Custom Vision API
const response = await axios.post(predictionEndpoint, req.file.buffer, {
headers: {
"Prediction-Key": predictionKey,
"Content-Type": "application/octet-stream",
},
});

// Send the API response to the frontend
res.json(response.data);
} catch (error) {
console.error("Error:", error);
res
.status(500)
.json({ error: "An error occurred while processing the image." });
}
};

module.exports = {
uploadImage,
};
31 changes: 31 additions & 0 deletions backend/controllers/ImageQuality_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// AdvancedTest_controller.js
const axios = require("axios");

// Azure Custom Vision API Endpoint and Prediction Key
const predictionEndpoint = process.env.QUALITY_PREDICTION_ENDPOINT;
const predictionKey = process.env.QUALITY_PREDICTION_KEY;

// Controller function to upload and analyze the image
const uploadImage = async (req, res) => {
try {
// Send the image to the Azure Custom Vision API
const response = await axios.post(predictionEndpoint, req.file.buffer, {
headers: {
"Prediction-Key": predictionKey,
"Content-Type": "application/octet-stream",
},
});

// Send the API response to the frontend
res.json(response.data);
} catch (error) {
console.error("Error:", error);
res
.status(500)
.json({ error: "An error occurred while processing the image." });
}
};

module.exports = {
uploadImage,
};
139 changes: 139 additions & 0 deletions backend/controllers/User_controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
const User = require("../models/User_model");
const test = require("../models/GeneralTest_Modal");
const jwt = require("jsonwebtoken");

// Generate JWT
const createToken = (_id) => {
return jwt.sign({_id}, process.env.SECRET, {expiresIn: '1d'})
}

//Register user
const createUser = async (req, res) => {
const {
firstname,
lastname,
contact,
addLine1,
addLine2,
addLine3,
gender,
email,
password,
} = req.body;

try {
const user = await User.register(
firstname,
lastname,
contact,
addLine1,
addLine2,
addLine3,
gender,
email,
password
);

//create a token
const token = createToken(user._id);

res.status(200).json({ email, token }); // sending JWT back to the browser
} catch (err) {
res.status(400).json({ err: err.message });
}
}

//Login user
const loginUser = async (req, res) => {
const {email, password} = req.body

try {
const user = await User.login(email, password);

//create a token
const token = createToken(user._id);

res.status(200).json({ user, token }); // sending JWT back to the browser
} catch (err) {
res.status(400).json({ err: err.message });
}
}

//Update user
const updateUser = async (req, res) => {
const { userId } = req.params;
const {
firstname,
lastname,
contact,
addLine1,
addLine2,
addLine3,
gender,
email,
} = req.body;

try {
const updatedUser = await User.findByIdAndUpdate(
userId,
{
firstname,
lastname,
contact,
addLine1,
addLine2,
addLine3,
gender,
email,
},
{ new: true }
);

if (!updatedUser) {
return res.status(404).json({ error: "User not found" });
}

res.status(200).json(updatedUser);
} catch (err) {
res.status(400).json({ error: err.message });
}
};

//Delete user
const deleteUser = async (req, res) => {
const { userId } = req.params;

try {
const deletedUser = await User.findByIdAndRemove(userId);

if (!deletedUser) {
return res.status(404).json({ error: "User not found" });
}

res.status(200).json({ message: "User deleted successfully" });
} catch (err) {
res.status(400).json({ error: err.message });
}
};

//Read test data
const readTest = async (req, res) => {
const token = req.headers.authorization.split(" ")[1];
try {
const user = jwt.verify(token, process.env.SECRET);
const testData = await test.find();

res.status(200).json(testData);
} catch (err) {
console.log(err);
res.status(500).json({ error: "Internal server error" });
}
};

module.exports = {
createUser,
loginUser,
updateUser,
deleteUser,
readTest
};
30 changes: 30 additions & 0 deletions backend/middleware/requireAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const jwt = require('jsonwebtoken')
const User = require('../models/User_model')

const requireAuth = async (req,res,next) => {

//verify authentication
const {authorization} = req.headers

if(!authorization) {
return res.status(401).json({error: "Authorization token required"})
}

//split the string into two parts by a space
const token = authorization.split(' ')[1] //token is in the position 1

try {
//verify token
const {_id} = jwt.verify(token, process.env.SECRET)

req.user = await User.findOne({_id}).select('_id, firstname') //only selecting id
next()

} catch (err) {
console.log(err)
res.status(401).json({error: "Requrest is not authorized"})

}
}

module.exports = requireAuth
19 changes: 19 additions & 0 deletions backend/middleware/roleAuthMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// roleAuthMiddleware.js
const roleAuthMiddleware = (roles) => {
return (req, res, next) => {
// Check if the role is provided as a query parameter
const roleQueryParam = req.query.role;

// Check if the user has the required role
if (roles.includes(roleQueryParam)) {
// User has the required role, so continue to the next middleware or route
next();
} else {
// User doesn't have the required role, return a 403 Forbidden response
res.status(403).json({ message: 'Forbidden' });
}
};
};

module.exports = roleAuthMiddleware;

24 changes: 24 additions & 0 deletions backend/models/ClinicsModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ClinicsSchema = new Schema({
clinicName: {
type: String,
required: true, //validating title field
},
clinicLocation: {
type: String,
required: true, //validating content field
},
clinicContact: {
type: String,
required: true, //validating content field
},
clinicWebsite: {
type: String,
required: false, //validating content field
},
});

const ClinicModel = mongoose.model("Clinic", ClinicsSchema);
module.exports = ClinicModel;
25 changes: 25 additions & 0 deletions backend/models/GeneralTest_Modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const mongoose = require("mongoose");

const Schema = mongoose.Schema;

const GeneralTestSchema = new Schema({
test_name: {
type: String,
required: false,
},
user_id: {
type: String,
required: false,
},
test_date: {
type: Date,
required: false,
},
test_score: {
type: String,
required: false,
},
});

const GeneralTest = mongoose.model("GeneralTest", GeneralTestSchema);
module.exports = GeneralTest;
Loading

0 comments on commit 77c1f15

Please sign in to comment.