62 lines
1.9 KiB
JavaScript
62 lines
1.9 KiB
JavaScript
// controllers/tabsController.js
|
|
import tabsService from '../service/tabsService.js';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
async function getTabs(ctx) {
|
|
const tabs = await tabsService.getTabs();
|
|
ctx.status=200;
|
|
ctx.body = { tabs };
|
|
}
|
|
async function findTabById(ctx) {
|
|
const { tabKey } = ctx.params;
|
|
const tab = await tabsService.findTabById(tabKey);
|
|
ctx.status=200;
|
|
console.log(tab);
|
|
ctx.body = { data: tab };
|
|
}
|
|
|
|
async function addTab(ctx) {
|
|
console.log(`Received POST request on /api/tab`);
|
|
const tab = ctx.request.body;
|
|
if (!tab.title) {
|
|
ctx.status = 400;
|
|
ctx.body = { message: 'Title is required!' };
|
|
return;
|
|
}
|
|
const existingTab = await tabsService.findTabByTitle(tab.title);
|
|
if (existingTab) {
|
|
ctx.status = 400;
|
|
ctx.body = { message: `A tab with title '${tab.title}' already exists!` };
|
|
return;
|
|
}
|
|
const id = uuidv4();
|
|
tab.id = id
|
|
const newTab = { ...tab };
|
|
await tabsService.addTab(newTab);
|
|
ctx.status = 201;
|
|
ctx.body = { data : id ,message: 'Tab added successfully!' };
|
|
}
|
|
|
|
async function updateTab(ctx) {
|
|
const updatedTab = ctx.request.body;
|
|
if(updatedTab.title){
|
|
const existingTab = await tabsService.findTabByTitle(updatedTab.title);
|
|
if (existingTab && existingTab.id !=updatedTab.id ) {
|
|
ctx.status = 400;
|
|
ctx.body = { message: `A tab with title '${updatedTab.title}' already exists!` };
|
|
return;
|
|
}
|
|
}
|
|
await tabsService.updateTab(updatedTab.id, updatedTab);
|
|
ctx.status = 200;
|
|
ctx.body = { data : updatedTab.id,message: 'Tab updated successfully!' };
|
|
}
|
|
|
|
async function deleteTab(ctx) {
|
|
const { tabKey } = ctx.params;
|
|
await tabsService.deleteTab(tabKey);
|
|
ctx.status = 200;
|
|
ctx.body = { message: 'Tab deleted successfully!' };
|
|
}
|
|
export default { addTab, getTabs,updateTab,deleteTab,findTabById };
|