aboutsummaryrefslogtreecommitdiff
path: root/server/controllers/bin.js
diff options
context:
space:
mode:
authorBlaster4385 <[email protected]>2024-02-19 20:42:57 +0530
committerBlaster4385 <[email protected]>2024-02-21 23:52:45 +0530
commit86ed77b9cc63d9fd8d61a0d3b56ef1307be7e007 (patch)
tree4c6dfcbb5737db8b0042f470ba06d3761f0d0d58 /server/controllers/bin.js
parent23de613393d2399babe3a7b4d2fb77d772fcfa87 (diff)
refactor: Rewrite server in go
Diffstat (limited to 'server/controllers/bin.js')
-rw-r--r--server/controllers/bin.js100
1 files changed, 0 insertions, 100 deletions
diff --git a/server/controllers/bin.js b/server/controllers/bin.js
deleted file mode 100644
index 882ecef..0000000
--- a/server/controllers/bin.js
+++ /dev/null
@@ -1,100 +0,0 @@
-import sqlite3 from "sqlite3";
-
-const dbPath = "./database/bins.db";
-
-function createBinTable() {
- const db = new sqlite3.Database(dbPath, (err) => {
- if (err) {
- console.error("Error connecting to database:", err.message);
- return;
- }
-
- const sql = `CREATE TABLE IF NOT EXISTS bin (
- id TEXT PRIMARY KEY,
- html_content TEXT
- )`;
-
- db.run(sql, (err) => {
- if (err) {
- console.error("Error creating table:", err.message);
- } else {
- console.log("`bin` table created successfully");
- }
-
- db.close((err) => {
- if (err) {
- console.error("Error closing database:", err.message);
- }
- });
- });
- });
-}
-
-const createBin = (req, res) => {
- console.log(`createBin: ${req.body}`);
- const { html_content } = req.body;
- const id = Math.random().toString(36).substring(7);
-
- const db = new sqlite3.Database(dbPath, (err) => {
- if (err) {
- console.error("Error connecting to database:", err.message);
- throw err; // Re-throw for async handling
- }
-
- const sql = `INSERT INTO bin (id, html_content) VALUES (?, ?)`;
-
- db.run(sql, [id, html_content], (err) => {
- if (err) {
- console.error("Error creating entry:", err.message);
- throw err; // Re-throw for async handling
- } else {
- res.status(201).json({ id });
- console.log("Entry created successfully:", id);
- }
-
- db.close((err) => {
- if (err) {
- console.error("Error closing database:", err.message);
- }
- });
- });
- });
-};
-
-const getBin = async (req, res) => {
- const { id } = req.params;
- if (!id) {
- throw new Error("Missing required parameter: id"); // Throw error for missing ID
- }
-
- const db = new sqlite3.Database(dbPath, (err) => {
- if (err) {
- console.error("Error connecting to database:", err.message);
- throw err; // Re-throw for async handling
- }
-
- const sql = `SELECT * FROM bin WHERE id = ?`;
-
- db.get(sql, [id], (err, row) => {
- if (err) {
- console.error("Error retrieving entry:", err.message);
- throw err; // Re-throw for async handling
- } else if (!row) {
- console.log("Entry not found:", id);
- return null; // Handle case where no entry exists
- }
-
- res.status(200).json(row);
- console.log("Entry retrieved:", id);
- db.close((err) => {
- if (err) {
- console.error("Error closing database:", err.message);
- }
- });
-
- return row;
- });
- });
-};
-
-export default { createBinTable, createBin, getBin };