Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tabs table and API #41

Merged
merged 11 commits into from
Nov 23, 2024
63 changes: 63 additions & 0 deletions backend/src/controllers/tabsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const tabsService = require("../services/tabsService");

// Controller for fetching all tabs
const getAllTabs = async (req, res) => {
try {
const tabs = await tabsService.getAllTabs();
res.json(tabs);
} catch (error) {
res.status(500).json({ error: "Failed to fetch tabs" });
}
};

// Controller for fetching a tab by ID
const getTabById = async (req, res) => {
try {
const tab = await tabsService.getTabById(req.params.id);
res.json(tab);
} catch (error) {
res.status(500).json({ error: "Failed to fetch tab" });
}
};

// Controller for creating a new tab
const createTab = async (req, res) => {
console.log(req.token);
try {
const { name, user_id } = req.body;

await tabsService.createTab(name, user_id);
res.json({ message: "Tab created successfully" });
} catch (error) {
res.status(500).json({ error: "Failed to create tab" });
}
};

// Controller for updating a tab by ID
const updateTab = async (req, res) => {
try {
const { name } = req.body;
await tabsService.updateTab(req.params.id, name);
res.json({ message: "Tab updated successfully" });
} catch (error) {
res.status(500).json({ error: "Failed to update tab" });
}
};

// Controller for deleting a tab by ID
const deleteTab = async (req, res) => {
try {
await tabsService.deleteTab(req.params.id);
res.json({ message: "Tab deleted successfully" });
} catch (error) {
res.status(500).json({ error: "Failed to delete tab" });
}
};

module.exports = {
getAllTabs,
getTabById,
createTab,
updateTab,
deleteTab,
};
4 changes: 3 additions & 1 deletion backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ setupSwagger(app);
// Route setup
const authRoute = require("./routes/authRoute");
const warehouseRoutes = require("./routes/warehouseRoute");
const tabRoutes = require("./routes/tabsRoute");

app.use("/api/warehouses", warehouseRoutes);
app.use("/api/auth", authRoute);
app.use("/api/tabs", tabRoutes);

// Default route
app.get("/api", (req, res) =>
res.send("Try: /api/status, /api/warehouses, or /api/warehouses/:id")
res.send("Try: /api/status, /api/warehouses, /api/warehouses/:id, /api/tabs, or /api/tabs/:id")
);

// Status endpoint
Expand Down
153 changes: 153 additions & 0 deletions backend/src/routes/tabsRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
const express = require('express');
const router = express.Router();
const tabsController = require('../controllers/tabsController');

/**
* @swagger
* /api/tabs:
* get:
* summary: Get all tabs
* responses:
* 200:
* description: A list of tabs
* content:
* application/json:
* schema:
* type: array
* items:
* type: object
* properties:
* id:
* type: integer
* example: 1
* name:
* type: string
* example: "Tab 1"
* createdAt:
* type: timestamp
* format: date-time
* example: "2021-09-21T21:00:00.000Z"
* user_id:
* type: string
* example: "1"
*/
router.get('/', tabsController.getAllTabs);

/**
* @swagger
* /api/tabs/{id}:
* get:
* summary: Get a tab by ID
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* description: The tab ID
* responses:
* 200:
* description: A single tab
* content:
* application/json:
* schema:
* type: object
* properties:
* id:
* type: integer
* example: 1
* name:
* type: string
* example: "Tab 1"
* createdAt:
* type: timestamp
* format: date-time
* example: "2021-09-21T21:00:00.000Z"
* user_id:
* type: string
* example: "1"
* 404:
* description: Tab not found
*/
router.get('/:id', tabsController.getTabById);

/**
* @swagger
* /api/tabs:
* post:
* summary: Create a new tab
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* id:
* type: integer
* example: 1
* name:
* type: string
* example: "New Tab"
* user_id:
* type: string
* example: "user123"
* responses:
* 201:
* description: Tab created successfully
* 500:
* description: Failed to create tab
*/
router.post('/', tabsController.createTab);

/**
* @swagger
* /api/tabs/{id}:
* put:
* summary: Update an existing tab
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* description: The tab ID
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* name:
* type: string
* example: "Updated Tab Name"
* responses:
* 200:
* description: Tab updated successfully
* 500:
* description: Failed to update tab
*/
router.put('/:id', tabsController.updateTab);

/**
* @swagger
* /api/tabs/{id}:
* delete:
* summary: Delete a tab
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* description: The tab ID
* responses:
* 200:
* description: Tab deleted successfully
* 500:
* description: Failed to delete tab
*/
router.delete('/:id', tabsController.deleteTab);

module.exports = router;
90 changes: 90 additions & 0 deletions backend/src/services/tabsService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const { connection } = require("../database");

// Query all tabs
const getAllTabs = () => {
return new Promise((resolve, reject) => {
connection.query("SELECT * FROM `ai-journal`.`tabs`", (error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
});
});
};

// Query tab by ID
const getTabById = (id) => {
return new Promise((resolve, reject) => {
connection.query(
"SELECT * FROM `ai-journal`.`tabs` WHERE id = ?",
[id],
(error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
}
);
});
};

// Create a new tab
const createTab = (name, userId) => {
return new Promise((resolve, reject) => {
connection.query(
"INSERT INTO `ai-journal`.`tabs` (name, user_id) VALUES (?, ?)",
[name, userId],
(error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
}
);
});
};

// Update a tab by ID
const updateTab = (id, name) => {
return new Promise((resolve, reject) => {
connection.query(
"UPDATE `ai-journal`.`tabs` SET name = ? WHERE id = ?",
[name, id],
(error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
}
);
});
};

// Delete a tab by ID
const deleteTab = (id) => {
return new Promise((resolve, reject) => {
connection.query(
"DELETE FROM `ai-journal`.`tabs` WHERE id = ?",
[id],
(error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
}
);
});
};

module.exports = {
getAllTabs,
getTabById,
createTab,
updateTab,
deleteTab,
};
Loading