164 lines
5.8 KiB
JavaScript
164 lines
5.8 KiB
JavaScript
/**
|
|
* @param {string} key
|
|
* @param {string} model
|
|
* @param {string} class
|
|
* @returns {string}
|
|
*/
|
|
|
|
app.use("/changeVehicleClass.lvorex", express.json())
|
|
app.post("/changeVehicleClass.lvorex", async (req, res) => {
|
|
const postBody = req.body
|
|
const { keyFound, userKey } = await controlKey(req, postBody.key)
|
|
|
|
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
|
|
const PermissionCheck = await checkPermission(userKey.rank, "Vehicles", 4)
|
|
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
|
|
const classes = [
|
|
"E",
|
|
"D",
|
|
"C",
|
|
"B",
|
|
"A",
|
|
"A+",
|
|
"S",
|
|
"S+"
|
|
]
|
|
if (!classes.some(c => c === postBody.class)) return res.json({ code: 404, message: "Class not found." })
|
|
|
|
let result = await query(`
|
|
update \`madmin_vehicles\`
|
|
set \`class\` = '${postBody.class}'
|
|
where \`model\` = '${postBody.model}'
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
|
|
res.json({ code: 200, message: "Changed." })
|
|
})
|
|
|
|
/**
|
|
* @param {string} key
|
|
* @param {string} model
|
|
* @param {string} import
|
|
* @returns {string}
|
|
*/
|
|
|
|
app.use("/changeVehicleImportStatus.lvorex", express.json())
|
|
app.post("/changeVehicleImportStatus.lvorex", async (req, res) => {
|
|
const postBody = req.body
|
|
const { keyFound, userKey } = await controlKey(req, postBody.key)
|
|
|
|
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
|
|
const PermissionCheck = await checkPermission(userKey.rank, "Vehicles", 4)
|
|
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
|
|
const newImportStatus = Number(postBody.import)
|
|
|
|
let result = await query(`
|
|
update \`madmin_vehicles\`
|
|
set \`import\` = ${newImportStatus},
|
|
\`category\` = '${newImportStatus === 1 ? "Import" : "Vanilla"}'
|
|
where \`model\` = '${postBody.model}'
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
|
|
res.json({ code: 200, message: "Changed." })
|
|
})
|
|
|
|
/**
|
|
* @param {string} key
|
|
* @param {File} file
|
|
* @param {string} model
|
|
* @returns {string}
|
|
*/
|
|
|
|
app.use("/changeVehicleImage.lvorex", express.json())
|
|
app.post("/changeVehicleImage.lvorex", UploadVehicleImage.single("file"), async (req, res) => {
|
|
const postBody = req.body
|
|
const { keyFound, userKey } = await controlKey(req, postBody.key)
|
|
|
|
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
|
|
const PermissionCheck = await checkPermission(userKey.rank, "Vehicles", 3)
|
|
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
|
|
|
|
const file = req.file
|
|
let result = await query(`
|
|
update \`madmin_vehicles\`
|
|
set \`image\` = '../img/VehicleImages/${file.filename}'
|
|
where \`model\` = '${postBody.model}'
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
|
|
res.json({ code: 200, message: "Done." })
|
|
})
|
|
|
|
/**
|
|
* @param {string} key
|
|
* @param {string} model
|
|
* @param {string} link
|
|
* @returns {string}
|
|
*/
|
|
|
|
app.use("/changeVehicleImageByLink.lvorex", express.json())
|
|
app.post("/changeVehicleImageByLink.lvorex", async (req, res) => {
|
|
const postBody = req.body
|
|
const { keyFound, userKey } = await controlKey(req, postBody.key)
|
|
|
|
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
|
|
const PermissionCheck = await checkPermission(userKey.rank, "Vehicles", 3)
|
|
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
|
|
|
|
let result = await query(`
|
|
update \`madmin_vehicles\`
|
|
set \`image\` = '${postBody.link}'
|
|
where \`model\` = '${postBody.model}'
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
|
|
res.json({ code: 200, message: "Changed." })
|
|
})
|
|
|
|
/**
|
|
* @param {string} key
|
|
* @param {string} model
|
|
* @returns {string}
|
|
*/
|
|
|
|
app.use("/clearVehicleImage.lvorex", express.json())
|
|
app.post("/clearVehicleImage.lvorex", async (req, res) => {
|
|
const postBody = req.body
|
|
const { keyFound, userKey } = await controlKey(req, postBody.key)
|
|
|
|
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
|
|
const PermissionCheck = await checkPermission(userKey.rank, "Vehicles", 3)
|
|
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
|
|
|
|
let result = await query(`
|
|
select * from \`madmin_vehicles\`
|
|
where \`model\` = '${postBody.model}'
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
result = result [0]
|
|
if (result.image === "img/CarImage.png") return res.json({ code: 404, message: "Image already clear." })
|
|
if (result.image.includes("http")) {
|
|
result = await query(`
|
|
update \`madmin_vehicles\`
|
|
set \`image\` = 'img/CarImage.png'
|
|
where \`model\` = '${postBody.model}'
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
res.json({ code: 200, message: "Cleared." })
|
|
return
|
|
}
|
|
const fileName = result.image.split("VehicleImages/")[1]
|
|
|
|
result = await query(`
|
|
update \`madmin_vehicles\`
|
|
set \`image\` = 'img/CarImage.png'
|
|
where \`model\` = '${postBody.model}'
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
|
|
const filePath = path.join(rootDir, `Web/Panel/Vehicles/img/VehicleImages/${fileName}`)
|
|
await fs.unlink(filePath)
|
|
|
|
res.json({ code: 200, message: "Cleared." })
|
|
}) |