From da17d5e4448b9dca12bf1b8d0d4f97bc275ff1e0 Mon Sep 17 00:00:00 2001
From: shwtea <shwetagupta0711754@gmail.com>
Date: Tue, 3 Sep 2024 14:42:07 +0530
Subject: [PATCH] update api added

---
 api/apiBaseRouter.js       |  3 ++
 api/layout/business.js     |  9 ++--
 api/layout/controller.js   |  3 +-
 api/layout/routes.js       |  5 ++-
 api/layout/service.js      |  3 +-
 api/template/business.js   | 30 +++++++++++++
 api/template/controller.js | 32 ++++++++++++++
 api/template/routes.js     | 10 +++++
 api/template/service.js    | 63 +++++++++++++++++++++++++++
 swagger.yaml               | 88 +++++++++++++++++++++++++++++++++++++-
 10 files changed, 235 insertions(+), 11 deletions(-)
 create mode 100644 api/template/business.js
 create mode 100644 api/template/controller.js
 create mode 100644 api/template/routes.js
 create mode 100644 api/template/service.js

diff --git a/api/apiBaseRouter.js b/api/apiBaseRouter.js
index ebf3692..113178f 100644
--- a/api/apiBaseRouter.js
+++ b/api/apiBaseRouter.js
@@ -1,5 +1,6 @@
 const healthCheckRouter = require('./health-check/routes');
 const layoutRouter = require('./layout/routes');
+const templateRouter = require('./template/routes')
 const apiBaseRouter = require('express').Router();
 
 // Exposed endpoints
@@ -11,4 +12,6 @@ apiBaseRouter.use('/health-check', healthCheckRouter);
 
 apiBaseRouter.use('/widget', layoutRouter);
 
+apiBaseRouter.use('/template', templateRouter);
+
 module.exports = apiBaseRouter;
diff --git a/api/layout/business.js b/api/layout/business.js
index ece8010..9dc4c6c 100644
--- a/api/layout/business.js
+++ b/api/layout/business.js
@@ -1,13 +1,13 @@
 /* eslint-disable prefer-promise-reject-errors */
 /* eslint-disable no-throw-literal */
 // const logger = require('utils/logger');
-const getAllWidgetService = require('../layout/service');
+const widgetService = require('../layout/service');
 
 
 
 const getAllWidget = async (page) => {
   try {
-    const response = await getAllWidgetService.getAllWidget(page);
+    const response = await widgetService.getAllWidget(page);
     return { status: '200', response }
   } catch (error) {
     throw error
@@ -17,7 +17,7 @@ const getAllWidget = async (page) => {
 
 const createWidget = async (data) => {
   try {
-    const response = await getAllWidgetService.createWidget(data);
+    const response = await widgetService.createWidget(data);
     return { status: '200', response }
   } catch (error) {
     throw error
@@ -26,8 +26,7 @@ const createWidget = async (data) => {
 };
 
 
-
 module.exports = {
   getAllWidget,
-  createWidget
+  createWidget,
 };
diff --git a/api/layout/controller.js b/api/layout/controller.js
index b543c69..b53a331 100644
--- a/api/layout/controller.js
+++ b/api/layout/controller.js
@@ -24,7 +24,8 @@ const createWidget = async (req, res, next) => {
 };
 
 
+
 module.exports = {
   getAllWidget,
-  createWidget
+  createWidget,
 };
\ No newline at end of file
diff --git a/api/layout/routes.js b/api/layout/routes.js
index 157994b..6d1628a 100644
--- a/api/layout/routes.js
+++ b/api/layout/routes.js
@@ -1,8 +1,9 @@
 const layoutRouter = require('express').Router();
-const { getAllWidget,createWidget} = require('./controller');
+const { getAllWidget, createWidget } = require('./controller');
 
 layoutRouter.get('/allLayout/:page', getAllWidget);
-  
+
 layoutRouter.post('/createWidget', createWidget);
 
+
 module.exports = layoutRouter;
\ No newline at end of file
diff --git a/api/layout/service.js b/api/layout/service.js
index 6a3529f..705512b 100644
--- a/api/layout/service.js
+++ b/api/layout/service.js
@@ -28,7 +28,8 @@ const createWidget = async (data) => {
 
 };
 
+
 module.exports = {
   getAllWidget,
-  createWidget
+  createWidget,
 };
\ No newline at end of file
diff --git a/api/template/business.js b/api/template/business.js
new file mode 100644
index 0000000..b72e0ae
--- /dev/null
+++ b/api/template/business.js
@@ -0,0 +1,30 @@
+/* eslint-disable prefer-promise-reject-errors */
+/* eslint-disable no-throw-literal */
+// const logger = require('utils/logger');
+const templateService = require('../template/service');
+
+
+const updateAndMapWidget = async (page,widgetId,position, startTime, endTime) => {
+  try {
+    const response = await templateService.updateAndMapWidget(page,widgetId,position, startTime, endTime);
+    return { status: '200', response }
+  } catch (error) {
+    throw error
+  }
+
+};
+
+const createTemplate = async (name, desc, widgets, hashId) => {
+  try {
+    const response = await templateService.createTemplate(name, desc, widgets, hashId);
+    return { status: '200', response }
+  } catch (error) {
+    throw error
+  }
+
+};
+
+module.exports = {
+  updateAndMapWidget,
+  createTemplate
+};
diff --git a/api/template/controller.js b/api/template/controller.js
new file mode 100644
index 0000000..ffb6154
--- /dev/null
+++ b/api/template/controller.js
@@ -0,0 +1,32 @@
+// const logger = require('utils/logger');
+const templateBusiness = require('./business');
+// const Joi = require('joi');
+
+
+
+const updateAndMapWidget = async (req, res, next) => {
+  try {
+    console.log("comign here")
+    const { page, widgetId } = req.params;
+    const { position, startTime, endTime } = req.body;
+    const response = await templateBusiness.updateAndMapWidget(page, widgetId, position, startTime, endTime);
+    return res.status(response.status).json({ data: response });
+  } catch (error) {
+    throw error
+  }
+};
+
+const createTemplate = async (req, res, next) => {
+  try {
+    const { name, desc, widgets, hashId } = req.body;
+    const response = await templateBusiness.createTemplate(name, desc, widgets, hashId);
+    return res.status(response.status).json({ data: response });
+  } catch (error) {
+    throw error
+  }
+};
+
+module.exports = {
+  updateAndMapWidget,
+  createTemplate
+}
\ No newline at end of file
diff --git a/api/template/routes.js b/api/template/routes.js
new file mode 100644
index 0000000..63d69c3
--- /dev/null
+++ b/api/template/routes.js
@@ -0,0 +1,10 @@
+const templateRouter = require('express').Router();
+
+const {  updateAndMapWidget,createTemplate } = require('./controller');
+
+
+templateRouter.put('/:page/widgets/:widgetId', updateAndMapWidget);
+
+templateRouter.post('/createTemplate', createTemplate);
+
+module.exports = templateRouter;
\ No newline at end of file
diff --git a/api/template/service.js b/api/template/service.js
new file mode 100644
index 0000000..1e1554b
--- /dev/null
+++ b/api/template/service.js
@@ -0,0 +1,63 @@
+// const logger = require('utils/logger');
+
+const Widget = require("../models/widget");
+const Template = require("../models/template")
+
+
+const updateAndMapWidget = async (page,widgetId,position, startTime, endTime) => {
+  try {
+    const template = await Template.findOne({ name: page });
+    if (!template) {
+      return { status: 404,  error: 'template not found' };
+    }
+    console.log(page,widgetId,position, startTime, endTime)
+    const widgetIndex = template.widgets.findIndex(w => w.widgetId.toString() === widgetId);
+    
+    if (widgetIndex !== -1) {
+      template.widgets[widgetIndex] = {
+        ...template.widgets[widgetIndex], 
+        position,                         
+        startTime,                       
+        endTime                          
+      };
+    } else {
+      template.widgets.push({
+        widgetId: widgetId,  
+        position,            
+        startTime,           
+        endTime              
+      });
+    }
+
+    await template.save();
+    return { status: 200, widgetId: widgetId, page: page };
+  } catch (error) {
+    throw error
+  }
+
+};
+
+const createTemplate = async (name, desc, widgets, hashId) => {
+  try {
+    console.log("in service")
+    const newTemplate = new Template({
+      name,
+      desc,
+      widgets,
+      hashId
+    });
+
+    // Save the template to the database
+    await newTemplate.save();
+    return { status: 201,  message: 'Template created successfully', id: newTemplate._id };
+  } catch (error) {
+    throw error
+  }
+
+};
+
+
+module.exports = {
+  updateAndMapWidget,
+  createTemplate
+};
\ No newline at end of file
diff --git a/swagger.yaml b/swagger.yaml
index 0f8c0f6..b170eb5 100644
--- a/swagger.yaml
+++ b/swagger.yaml
@@ -9,8 +9,10 @@ info:
 #     description: Local server
 
 paths:
-  /templates/{page}:
+  /template/{page}:
     get:
+      tags:
+        - "Template"
       summary: Get template details of a particular page
       parameters:
         - in: path
@@ -62,6 +64,8 @@ paths:
 
   /widget/allLayout/{page}:
     get:
+      tags:
+        - "Widget"
       summary: Get widget details for a page
       parameters:
         - in: path
@@ -144,6 +148,8 @@ paths:
 
   /widget/createWidget:
     post:
+      tags:
+        - "Widget"
       summary: Create a new widget
       requestBody:
         required: true
@@ -226,8 +232,10 @@ paths:
                   error:
                     type: string
 
-  /api/templates/{page}/widgets/{widgetId}:
+  /template/{page}/widgets/{widgetId}:
     put:
+      tags:
+        - "Template"
       summary: Update and map a widget to a particular page
       parameters:
         - in: path
@@ -282,3 +290,79 @@ paths:
                 properties:
                   error:
                     type: string
+
+  /template/createTemplate:
+    post:
+      tags:
+        - "Template"
+      summary: Create a new template
+      description: Create a new template for a specific page, including widget mapping and scheduling.
+      requestBody:
+        description: The template to create
+        required: true
+        content:
+          application/json:
+            schema:
+              type: object
+              properties:
+                name:
+                  type: string
+                  description: The name of the template
+                  example: "Homepage Template"
+                desc:
+                  type: string
+                  description: A description of the template
+                  example: "Template for the homepage layout"
+                widgets:
+                  type: array
+                  description: A list of widgets mapped to the template
+                  items:
+                    type: object
+                    properties:
+                      widgetId:
+                        type: string
+                        description: The ID of the widget
+                        example: "64e9b0d4f3a1a85e5e4e9f76"
+                      position:
+                        type: integer
+                        description: The position of the widget in the template
+                        example: 0
+                      startTime:
+                        type: string
+                        format: date-time
+                        description: The start time for the widget to be active
+                        example: "2024-01-01T00:00:00Z"
+                      endTime:
+                        type: string
+                        format: date-time
+                        description: The end time for the widget to be active
+                        example: "2024-12-31T23:59:59Z"
+                hashId:
+                  type: string
+                  description: A unique identifier for the template
+                  example: "homepage-template-001"
+      responses:
+        '201':
+          description: Template created successfully
+          content:
+            application/json:
+              schema:
+                type: object
+                properties:
+                  message:
+                    type: string
+                    example: "Template created successfully"
+                  id:
+                    type: string
+                    description: The ID of the created template
+                    example: "64e9b0d4f3a1a85e5e4e9f78"
+        '400':
+          description: Bad request
+          content:
+            application/json:
+              schema:
+                type: object
+                properties:
+                  error:
+                    type: string
+                    example: "Validation error: 'name' is required."  
-- 
GitLab