diff --git a/FinalReport.pdf b/FinalReport.pdf new file mode 100644 index 0000000..74e869f Binary files /dev/null and b/FinalReport.pdf differ diff --git a/app.js b/app.js new file mode 100644 index 0000000..d6a345a --- /dev/null +++ b/app.js @@ -0,0 +1,9 @@ +const fs = require("fs"); // require the fs module to manipulate with file system +const express = require("express"); // requiring the express module +const app = express(); +const morgan = require("morgan") +app.use(morgan("dev")); // using a morgan middleware to get the requested url at the client side to print at the console +app.use(express.json()); // using express.json() middleware to further read the body the code to handle post ,patch,put requests +const userRouters = require("./userRoutes"); // Importing the packages rounters that routes the data acoording to the http methods +app.use("/", userRouters); // whenver the home page is requested the control flow goes to the userRoute.js which routes according to the url +module.exports = app; // exporting to be used by the server.js file which is the starting file diff --git a/server.js b/server.js new file mode 100644 index 0000000..56e6ad9 --- /dev/null +++ b/server.js @@ -0,0 +1,8 @@ +/*This is starting file of the nodejs application */ +const app = require("./app"); // +const port = 3000; + +/*Starting server at port 3000 using the listen function*/ +app.listen(port, () => { + console.log("The server is started and listens at port 3000"); +}); \ No newline at end of file diff --git a/userControllers.js b/userControllers.js new file mode 100644 index 0000000..d6f0c2a --- /dev/null +++ b/userControllers.js @@ -0,0 +1,109 @@ +const fs = require("fs"); // requiring the module to manipulate with file system +const userJSON = JSON.parse(fs.readFileSync(`${__dirname}/data/users.json`, { encoding: "utf-8" })); // reading the json which is sample data created by me which consist of user info +const findObject = (getId) => { + for (let i = 0; i < userJSON.length ; i++) { + if (userJSON[i] != null && userJSON[i].id === getId) { + return userJSON[i]; + } + } + return undefined +} + +/*The url is get all the users names thus we applied a get request method send the entire json file with respective status*/ +exports.getAllUsers = (requestUsers, responseUsers) => { + responseUsers.status(200).json({ + status: "OK", + data: { + userJSON + } + }); +} +/*This url the client sends the data as json object and we write into the json file a post request*/ +exports.postUsers = (requestUsers, responUsers) => { + const newObject = requestUsers.body; + userJSON.push(newObject); + fs.writeFile(`${__dirname}/data/users.json`, JSON.stringify(userJSON), (err) => { + responUsers.status(201).json({ + status: "Written successfully", + data: { + newObject + } + }); + }) +} + +/*This url an id is mentioned additionally which should inturn fetch the particular user with the respective id */ +exports.getSpecificUser = (requestUser, responseUser) => { + const getId = Number(requestUser.params.id); + const getData = findObject(getId); + console.log(getData) + if (!getData) { + responseUser.status(404).json({ + status: "Invalid Id", + message: "Data not found" + }) + } + else { + responseUser.status(200).json({ + status: "OK", + data: { + getData + } + }); + } + +} + +/*A patch request is update a key for particular json object in the json file with respective to user ID*/ +exports.updateUser = (requestUsers, responseUsers) => { + const getId = Number(requestUsers.params.id); + const getObject = findObject(getId); + if (!getObject) { + responseUsers.status(404).json({ + status: "Not found", + message : "Invalid data" + }); + } + else { + const getId = Number(requestUsers.params.id); + const getCollege = requestUsers.body.college + console.log(getCollege); + userJSON[getId].college = getCollege; + fs.writeFile(`${__dirname}/data/users.json`, JSON.stringify(userJSON), (err) => { + responseUsers.status(201).json({ + status: "Updated successfully", + data: { + getObject + } + }); + }) + } +} + +/*A delete request is used to delete a json object with according to the user Id specified in the url params*/ +exports.deleteUser = (requestUser, responseUser) => { + const getId = Number(requestUser.params.id); + const getObject = findObject(getId); + console.log(getObject) + if (!getObject) { + responseUser.status(404).json({ + status: "Not found", + message : "Invalid data" + }); + } + else { + const getObject = userJSON.find(data => { + if(data != null) + data.id === getId + }) + delete userJSON[getId]; + fs.writeFile(`${__dirname}/data/users.json`, JSON.stringify(userJSON), (err) => { + responseUser.status(204).json({ + status: "Deleted successfully", + data: { + getObject + } + }); + }) + } +} \ No newline at end of file diff --git a/userRoutes.js b/userRoutes.js new file mode 100644 index 0000000..47e612a --- /dev/null +++ b/userRoutes.js @@ -0,0 +1,14 @@ +const express = require("express"); +const userApp = express.Router(); // mounting a rounter to handle the http request accordingly +const userController = require("./userControllers"); //The application logic to handle the http request stored in userController.js file following the MVC model + +userApp + .route("/api/users") + .get(userController.getAllUsers) + .post(userController.postUsers); +userApp + .route("/api/users/:id") + .get(userController.getSpecificUser) + .patch(userController.updateUser) + .delete(userController.deleteUser); +module.exports = userApp; diff --git a/users.json b/users.json new file mode 100644 index 0000000..dc285c5 --- /dev/null +++ b/users.json @@ -0,0 +1 @@ +[null,{"id":1,"firstName":"ghi","lastName":"jkl","cgpa":9.5,"college":"Guidy College of Engineering","SSLC":95,"HSC":94},{"id":2,"firstName":"mno","lastName":"pqr","cgpa":8.8,"college":"IIIT ,Delhi","SSLC":89,"HSC":98},{"id":3,"firstName":"stu","lastName":"vw","cgpa":8.2,"college":"Indian Institute of Technology","SSLC":100,"HSC":96},{"id":4,"firstName":"yz","lastName":"abc","cgpa":9.2,"college":"National Institute of Technology","SSLC":90,"HSC":92},{"id":0,"firstName":"abcdef","lastName":"1234","cgpa":9,"college":"Indian Institute of Information Technology","SSLC":99,"HSC":99},{"id":0,"firstName":"abcdef","lastName":"1234","cgpa":9,"college":"Indian Institute of Information Technology","SSLC":99,"HSC":99}] \ No newline at end of file