diff --git a/src/controllers/tankersController b/src/controllers/tankersController new file mode 100644 index 00000000..1c6c2a15 --- /dev/null +++ b/src/controllers/tankersController @@ -0,0 +1,96 @@ + +const Tanker = require("../models/tankers"); +const User = require("../models/User"); +const boom = require("boom"); +const fastify = require("fastify")({ + logger: true, +}); + + + +exports.addTankers = async (req, reply) => { + try { + + //const username = req.params.username; + +console.log(req.params); + 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); + tankersData = { + tankerName: req.body.tankerName, + phoneNumber: req.body.phoneNumber, + typeofwater: req.body.typeofwater, + capacity: req.body.capacity, + }; + var tanker_Name = req.body.tankerName + var i_tank = await Tanker.findOne({ tankerName: tanker_Name}) + if(i_tank){ + throw new Error('tankname already exists'); + } + else { + + var tankers = new Tanker(tankersData); + + checkFormEncoding = isUserFormUrlEncoded(req); + if (checkFormEncoding.isUserFormUrlEncoded) { + usertobeInserted = checkFormEncoding.tank; + console.log("thsi true url string"); + tankers.tankerName = usertobeInserted.tankerName; + tankers.phoneNumber = usertobeInserted.phoneNumber; + tankers.capacity = usertobeInserted.capacity; + tankers.typeofwater = usertobeInserted.typeofwater; + } + } + const insertedTank = await tankers.save(); + + return insertedTank; + + + } catch (err) { + throw boom.boomify(err); + } +}; + +//update selected tanker +exports.updateTankersInfo = async (req, reply) => { + try { + //const username = loginObject.user.username; + const tankerName = req.params.tankerName; + + var tank_name = req.body.tankerName + var i_tank = await Tanker.findOne({ tankerName: tank_name}) + const 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 }); + } + } + catch (err) { + throw boom.boomify(err); + } +}; + +//delete selected tank +exports.deleteTankerInfo = async (req, reply) => { + try { + const tankerName = req.params.tankerName; + const tanker = await Tanker.findOneAndDelete({ tankerName: tankerName }); + reply.send({ status_code: 200, data: tanker}); + // return tanker; + } catch (err) { + throw boom.boomify(err); + } +}; diff --git a/src/index.js b/src/index.js index a493db80..e6648716 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ const userController = require("./controllers/userController"); const tanksController = require("./controllers/tanksController"); +const tankersController = require("./controllers/tankersController"); const createConnectionController = require("./controllers/createConnectionController"); const cors = require("cors"); const swagger = require("./config/swagger"); @@ -288,6 +289,8 @@ fastify.register(require("./routes/usersRoute")); fastify.register(require("./routes/tanksRoute")); fastify.register(require("./routes/createConnectionsRoute")); +fastify.register(require("./routes/tankersRoute")); + // Testing route allows for retrieving a user by phone so one can see what is the phone verification code sent for a given user's phone // Also allows deletion of a user with a given phone number diff --git a/src/models/tankers.js b/src/models/tankers.js new file mode 100644 index 00000000..1b38e253 --- /dev/null +++ b/src/models/tankers.js @@ -0,0 +1,19 @@ +const mongoose = require("mongoose"); + + + +const Schema = mongoose.Schema; +const ObjectId = Schema.Types.ObjectId; + +// Store a random password reset code +const code = Math.floor(100000 + Math.random() * 900000); +const RoleSchema = new Schema({ name: String }); +const tankersSchema = new mongoose.Schema({ + + tankerName: { type: String, default: null }, + phoneNumber: { type: String, default: null }, + typeofwater: { type: String, default: null }, + capacity: { type: String, default: null }, +}); + +module.exports = mongoose.model("Tanker", tankersSchema); \ No newline at end of file diff --git a/src/routes/tankersRoute b/src/routes/tankersRoute new file mode 100644 index 00000000..16a931f5 --- /dev/null +++ b/src/routes/tankersRoute @@ -0,0 +1,113 @@ +const fastify = require("fastify"); +const tankersController = require("../controllers/tankersController"); + +module.exports = function (fastify, opts, next) { + + fastify.route({ + method: "POST", + url: "/api/addTankers", + schema: { + tags: ["Tanker"], + description: "This is for cretae New Tanker", + summary: "This is for Create New Tanker.", + body: { + type: "object", + properties: { + tankerName: { type: "string" }, + phoneNumber: { type: "string"}, + typeofwater: { type: "string" }, + capacity: { type: "string" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tankersController.addTankers, + // onResponse: (request, reply) => { + // validationHandler.sendPhoneVerificationCode(request, reply); + // }, + //onResponse: validationHandler.sendPhoneVerificationCode, + }); + + +//update tankers + fastify.route({ + method: "PUT", + url: "/api/updateTankers/:tankerName", + schema: { + tags: ["Tanker"], + summary: "This is for update tanker", + params: { + required: ["tankerName"], + type: "object", + properties: { + tankerName: { + type: "string", + description: "tankerName", + }, + }, + }, + body: { + type: "object", + // required: ['phone'], + properties: { + tankerName: { type: "string", default: null }, + phoneNumber: { type: "string", default: null }, + capacity: { type: "number" }, + typeofwater: { type: "string", default: null }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: [ + // fastify.auth([fastify.operatorAuthenticate]), + // validationHandler.validatePhoneFormat, + // ], + preHandler: fastify.auth([fastify.authenticate]), + handler: tankersController.updateTankersInfo, + }); + + + fastify.route({ + method: "PUT", + url: "/api/deleteTanker/:tankerName", + schema: { + tags: ["Tanker"], + summary: "This is for delete tanker", + params: { + required: ["tankerName"], + type: "object", + properties: { + tankerName: { + type: "string", + description: "tankerName", + }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tankersController.deleteTankerInfo, + }); + + + + + + next(); +} + +