From bd9f8531ac67f49cba9e1ea3e85ce050ac176c79 Mon Sep 17 00:00:00 2001 From: varun Date: Wed, 8 Feb 2023 02:41:26 -0500 Subject: [PATCH] crud operations in tankers with customer id --- src/controllers/tankersController.js | 77 +++++++++++++++----------- src/routes/tankersRoute.js | 83 ++++++++++++++++++++++++---- 2 files changed, 117 insertions(+), 43 deletions(-) diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js index 4f12bcda..ef2af941 100644 --- a/src/controllers/tankersController.js +++ b/src/controllers/tankersController.js @@ -16,7 +16,7 @@ exports.addTankers = async (req, reply) => { //const username = req.params.username; console.log(req.params); - const username = loginObject.user.username; + var customerId = req.params.customerId; //console.log(loginObject.user.username) // const userInfo = await User.findOne({ username: username.toString() }); // const updateData = req.body; @@ -25,6 +25,7 @@ console.log(req.params); // console.log("This is the reply in the handler after the validations", reply); tankersData = { + customerId:customerId, tankerName: req.body.tankerName, phoneNumber: req.body.phoneNumber, typeofwater: req.body.typeofwater, @@ -33,9 +34,9 @@ console.log(req.params); var tanker_Name = req.body.tankerName - var i_tank = await Tanker.findOne({ tankerName: tanker_Name}) + var i_tank = await Tanker.findOne({ tankerName: tanker_Name,customerId:customerId}) if(i_tank){ - throw new Error('tankname already exists'); + throw new Error('tankername already exists'); } else { @@ -49,6 +50,7 @@ console.log(req.params); tankers.phoneNumber = usertobeInserted.phoneNumber; tankers.capacity = usertobeInserted.capacity; tankers.typeofwater = usertobeInserted.typeofwater; + tankers.customerId = usertobeInserted.customerId; } } const insertedTank = await tankers.save(); @@ -62,25 +64,20 @@ console.log(req.params); }; //update selected tanker -exports.updateTankersInfo = async (req, reply) => { - try { - //const username = loginObject.user.username; - const tankerName = req.params.tankerName; +exports.updateTankersInfo = async (req, reply) => { - var tank_name = req.body.tankerName - var i_tank = await Tanker.findOne({ tankerName: tank_name}) - const tanker = req.body; + try { + var customerId = req.params.customerId; + var tankerName = req.query.tankerName; + var tanker = req.body; const { ...updateData } = tanker; - const update = await Tanker.findOneAndUpdate({ tankerName: tankerName }, updateData, { new: true }); - - if(i_tank){ - throw new Error('tankname already exists'); - } - else - { - //return update; - reply.send({ status_code: 200, data: update }); - } + const update = await Tanker.findOneAndUpdate({ customerId:customerId,tankerName: tankerName, }, updateData, { new: true }); + //console.log(update.username) + //return update; + + reply.send({ status_code: 200, data: update }); + + } catch (err) { throw boom.boomify(err); @@ -90,8 +87,9 @@ exports.updateTankersInfo = async (req, reply) => { //delete selected tanker exports.deleteTankerInfo = async (req, reply) => { try { - const tankerName = req.params.tankerName; - const tanker = await Tanker.findOneAndDelete({ tankerName: tankerName }); + var customerId = req.params.customerId; + var tankerName = req.query.tankerName; + const tanker = await Tanker.findOneAndDelete({ customerId:customerId,tankerName: tankerName, }); reply.send({ status_code: 200, data: tanker}); // return tanker; } catch (err) { @@ -139,7 +137,6 @@ exports.tankerBooking = async (req, reply) => { tankersBookingData.address = usertobeInserted.address; tankersBookingData.dateOfOrder = usertobeInserted.dateOfOrder; - } } @@ -156,7 +153,7 @@ exports.tankerBooking = async (req, reply) => { exports.getTanker = async (req, reply) => { try { - await Tanker.find({tankerName: req.query.tankerName}) + await Tanker.find({customerId: req.query.customerId}) .exec() .then((docs) => { reply.send({ status_code: 200, data: docs, count: docs.length }); @@ -173,17 +170,11 @@ exports.getTanker = async (req, reply) => { exports.addBores = async (req, reply) => { try { - //const username = req.params.username; + var customerId = req.params.customerId; - console.log(customerId); + //console.log(customerId); //const username = loginObject.user.username; - //console.log(loginObject.user.username) - // const userInfo = await User.findOne({ username: username.toString() }); - // const updateData = req.body; - - - - // console.log("This is the reply in the handler after the validations", reply); + boresData = { customerId:customerId, boreName: req.body.boreName, @@ -253,3 +244,23 @@ exports.deleteBoresInfo = async (req, reply) => { }; +exports.updateBoresInfo = async (req, reply) => { + + try { + var customerId = req.params.customerId; + var boreName = req.query.boreName; + const bore = req.body; + const { ...updateData } = bore; + const update = await Bore.findOneAndUpdate({ customerId:customerId,boreName: boreName, }, updateData, { new: true }); + //console.log(update.username) + //return update; + + reply.send({ status_code: 200, data: update }); + + + } + catch (err) { + throw boom.boomify(err); + } +}; + diff --git a/src/routes/tankersRoute.js b/src/routes/tankersRoute.js index aa6d6629..7b5a0eff 100644 --- a/src/routes/tankersRoute.js +++ b/src/routes/tankersRoute.js @@ -8,11 +8,21 @@ module.exports = function (fastify, opts, next) { fastify.route({ method: "POST", - url: "/api/addTankers", + url: "/api/addTankers/:customerId", schema: { tags: ["Supplier"], description: "This is to cretae New Tanker", summary: "This is to Create New Tanker.", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, body: { type: "object", properties: { @@ -41,20 +51,23 @@ module.exports = function (fastify, opts, next) { //update tankers fastify.route({ method: "PUT", - url: "/api/updateTankers/:tankerName", + url: "/api/updateTankers/:customerId", schema: { tags: ["Supplier"], summary: "This is to update tanker", params: { - required: ["tankerName"], + required: ["customerId"], type: "object", properties: { - tankerName: { + customerId: { type: "string", - description: "tankerName", + description: "customerId", }, }, }, + querystring: { + tankerName: {type: 'string'} + }, body: { type: "object", // required: ['phone'], @@ -82,20 +95,23 @@ module.exports = function (fastify, opts, next) { fastify.route({ method: "PUT", - url: "/api/deleteTanker/:tankerName", + url: "/api/deleteTanker/:customerId", schema: { tags: ["Supplier"], summary: "This is to delete tanker", params: { - required: ["tankerName"], + required: ["customerId"], type: "object", properties: { - tankerName: { + customerId: { type: "string", - description: "tankerName", + description: "customerId", }, }, }, + querystring: { + tankerName: {type: 'string'} + }, security: [ { basicAuth: [], @@ -149,7 +165,7 @@ module.exports = function (fastify, opts, next) { description: "This is for Get Tanker Data", summary: "This is for to Get Tanker Data", querystring: { - tankerName: {type: 'string'} + customerId: {type: 'string'} }, security: [ { @@ -244,10 +260,57 @@ module.exports = function (fastify, opts, next) { }, ], }, + + //preHandler: fastify.auth([fastify.authenticate]), handler: tankersController.deleteBoresInfo, }); + fastify.route({ + method: "PUT", + url: "/api/updateBores/:customerId", + schema: { + tags: ["Supplier"], + summary: "This is for update bore", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + querystring: { + boreName: {type: 'string'} + }, + + body: { + type: "object", + // required: ['phone'], + properties: { + + boreName: { type: "string" }, + typeofwater: { type: "string" }, + description: { type: "string" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: [ + // fastify.auth([fastify.operatorAuthenticate]), + // validationHandler.validatePhoneFormat, + // ], + preHandler: fastify.auth([fastify.authenticate]), + handler: tankersController.updateBoresInfo, + }); + +