feat: enhance token handling in removeSelection and saveRow functions
This commit is contained in:
@@ -8,6 +8,8 @@ import {
|
||||
createEntry,
|
||||
removeEntries,
|
||||
saveRow,
|
||||
resetData,
|
||||
getVitals,
|
||||
} from "./services/database.js";
|
||||
import { generateToken, authenticate } from "./services/tokenService.js";
|
||||
env.config();
|
||||
@@ -37,8 +39,10 @@ app.post("/lose", async (req, res) => {
|
||||
|
||||
app.get("/table-data", authenticate, async (req, res) => {
|
||||
const result = await getTableData();
|
||||
if (result.success) {
|
||||
if (result.success && result.data) {
|
||||
res.status(200).json(result.data);
|
||||
} else if (result.success && !result.data) {
|
||||
res.status(204).json({ success: true });
|
||||
} else {
|
||||
res.status(500).json({ success: false });
|
||||
}
|
||||
@@ -87,6 +91,15 @@ app.post("/login", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.delete("/reset-data", authenticate, async (req, res) => {
|
||||
const result = await resetData();
|
||||
if (result.success) {
|
||||
res.status(200).json({ success: true });
|
||||
} else {
|
||||
res.status(400).json({ success: false });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server is running on port: ${port}`);
|
||||
});
|
||||
|
@@ -47,8 +47,11 @@ export async function getTableData() {
|
||||
|
||||
if (result.length > 0) {
|
||||
return { success: true, data: result };
|
||||
} else if (result.entries.length === 0) {
|
||||
return { success: true };
|
||||
} else {
|
||||
return { success: false };
|
||||
}
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
export async function createEntry(data) {
|
||||
@@ -114,3 +117,12 @@ export async function saveRow(payload) {
|
||||
return { success: false };
|
||||
}
|
||||
}
|
||||
|
||||
export async function resetData() {
|
||||
const [result] = await pool.query("DELETE FROM lose");
|
||||
if (result.affectedRows > 0) {
|
||||
return { success: true };
|
||||
} else {
|
||||
return { success: false };
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user