parent
798761d3ba
commit
a1ece909f2
@ -1,96 +0,0 @@
|
|||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,113 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in new issue