From d84aec7b688bf7befef9bfdb0240253ade365ad7 Mon Sep 17 00:00:00 2001
From: shwtea <shwetagupta0711754@gmail.com>
Date: Tue, 3 Sep 2024 12:10:29 +0530
Subject: [PATCH] basic api added

---
 api/layout/business.js   |  18 +++-
 api/layout/controller.js |  27 +++---
 api/layout/routes.js     |   8 +-
 api/layout/service.js    |  26 +++--
 api/models/widget.js     |   3 +-
 server.js                |   2 -
 swagger.yaml             | 198 ++++++++++++++++++---------------------
 7 files changed, 141 insertions(+), 141 deletions(-)

diff --git a/api/layout/business.js b/api/layout/business.js
index 73ab557..ece8010 100644
--- a/api/layout/business.js
+++ b/api/layout/business.js
@@ -5,14 +5,21 @@ const getAllWidgetService = require('../layout/service');
 
 
 
-const getAllWidget = async () => {
+const getAllWidget = async (page) => {
   try {
-    // logger.info(`Entering | getAllWidget BusinessLogic |`);
-    const response = await getAllWidgetService.getAllWidget();
-    // logger.info(`Exiting | generateAadhaarOtp BusinessLogic |`);
+    const response = await getAllWidgetService.getAllWidget(page);
+    return { status: '200', response }
+  } catch (error) {
+    throw error
+  }
+
+};
+
+const createWidget = async (data) => {
+  try {
+    const response = await getAllWidgetService.createWidget(data);
     return { status: '200', response }
   } catch (error) {
-    // logger.error(` Error | getAllWidget BusinessLogic | ${error}`)
     throw error
   }
 
@@ -22,4 +29,5 @@ const getAllWidget = async () => {
 
 module.exports = {
   getAllWidget,
+  createWidget
 };
diff --git a/api/layout/controller.js b/api/layout/controller.js
index e06ad30..b543c69 100644
--- a/api/layout/controller.js
+++ b/api/layout/controller.js
@@ -5,25 +5,26 @@ const layoutBusiness = require('./business');
 const getAllWidget = async (req, res, next) => {
   try {
     console.log("comign here")
-    // logger.info(`Entering | getAllWidget Controller | ${req.body}`);
-    // const schema = Joi.object({
-    //   aadhaarNumber: Joi.string().required(),
-    // });
-    // const { error } = schema.validate({  });
-    // if (error) {
-    //   logger.error(` Error | getAllWidget Controller | ${JSON.stringify(error)}`)
-    //   return next(new ExpressError('BAD_REQUEST'));
-    // }
-    const response = await layoutBusiness.getAllWidget();
-    // logger.info(`Exiting | getAllWidget Controller |`);
+    const { page } = req.params;
+    const response = await layoutBusiness.getAllWidget(page);
     return res.status(response.status).json({ data: response });
   } catch (error) {
-    // logger.error(` Error | getAllWidget Controller | ${error}`)
-    next(new ExpressError(error));
+    throw error
+  }
+};
+const createWidget = async (req, res, next) => {
+  try {
+    console.log("comign createWidget")
+
+    const response = await layoutBusiness.createWidget(req.body);
+    return res.status(response.status).json({ data: response });
+  } catch (error) {
+    throw error
   }
 };
 
 
 module.exports = {
   getAllWidget,
+  createWidget
 };
\ No newline at end of file
diff --git a/api/layout/routes.js b/api/layout/routes.js
index eaa7087..157994b 100644
--- a/api/layout/routes.js
+++ b/api/layout/routes.js
@@ -1,10 +1,8 @@
 const layoutRouter = require('express').Router();
-const { getAllWidget} = require('./controller');
+const { getAllWidget,createWidget} = require('./controller');
 
-layoutRouter.get('/allLayout', getAllWidget);
+layoutRouter.get('/allLayout/:page', getAllWidget);
   
-// layoutRouter.post('/aadhaarOtpVerify', aadhaarOtpVerify);
-
-// layoutRouter.post('/aadhaarPanSimilarity', aadhaarPanSimilarity);
+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 b5c9e23..6a3529f 100644
--- a/api/layout/service.js
+++ b/api/layout/service.js
@@ -1,15 +1,28 @@
 // const logger = require('utils/logger');
 
+const Widget = require("../models/widget");
+const Template = require("../models/template")
 
+const getAllWidget = async (page) => {
+  try {
+    const template = await Template.findOne({ name: page}).populate('widgets.widgetId');
+    if (!template) {
+      return res.status(404).json({ error: 'Template not found' });
+    }
+    res.status(200).json(template);
+  } catch (error) {
+    throw error
+  }
+
+};
 
-const getAllWidget = async () => {
+const createWidget = async (data) => {
   try {
-    // logger.info(`Entering | getAllWidget Service |`);
-    const response = "123";
-    // logger.info(`Exiting | getAllWidget Service |`);
-    return { status: '200', response }
+    console.log("in service")
+    const widget = new Widget(data);
+    await widget.save();
+    return { status: 201, id: widget._id, message: 'Widget created successfully' };
   } catch (error) {
-    // logger.error(` Error | getAllWidget Service | ${error}`)
     throw error
   }
 
@@ -17,4 +30,5 @@ const getAllWidget = async () => {
 
 module.exports = {
   getAllWidget,
+  createWidget
 };
\ No newline at end of file
diff --git a/api/models/widget.js b/api/models/widget.js
index f95ddc3..106c3d8 100644
--- a/api/models/widget.js
+++ b/api/models/widget.js
@@ -31,8 +31,7 @@ const ListItemSchema = new Schema({
 // Define main Widget schema
 const WidgetSchema = new Schema({
     title: { type: String, required: true },
-    layout: { type: String, required: true },
-    priority: { type: Number, required: true },
+    widget: { type: String, required: true },
     enable: { type: Boolean, default: true },
     buttonData: [ButtonDataSchema],
     sectionHeadingText: { type: String, required: true },
diff --git a/server.js b/server.js
index c4ce85d..524f6cb 100644
--- a/server.js
+++ b/server.js
@@ -9,8 +9,6 @@ const app = express();
 app.use(bodyParser.json());
 const apiBaseRouter = require('./api/apiBaseRouter');
 
-const baseURL = '/api-service';
-
 // Connect to MongoDB
 connectDB();
 
diff --git a/swagger.yaml b/swagger.yaml
index a2b5234..f9fb1ee 100644
--- a/swagger.yaml
+++ b/swagger.yaml
@@ -1,91 +1,27 @@
 openapi: 3.0.0
 info:
-  title: Layout Management API
+  title: Widgetized Homepage API
   version: 1.0.0
-  description: API for managing layouts including creating, retrieving, and deleting layouts.
-  
+  description: API for managing templates and widgets for a widgetized homepage and sub-pages.
+
+# servers:
+#   - url: http://localhost:3000
+#     description: Local server
+
 paths:
-  /api/layout/{layout}:
+  /widget/allLayout/{page}:
     get:
-      summary: Get a specific layout
+      summary: Get widget details for a page
       parameters:
         - in: path
-          name: layout
+          name: page
           required: true
           schema:
             type: string
-          description: The layout identifier
+          description: The page for which widget details are requested.
       responses:
-        200:
-          description: Successfully retrieved layout
-          content:
-            application/json:
-              schema:
-                type: object
-                properties:
-                  title:
-                    type: string
-                  layout:
-                    type: string
-                  priority:
-                    type: integer
-                  enable:
-                    type: boolean
-                  buttonData:
-                    type: array
-                    items:
-                      type: object
-                      properties:
-                        url:
-                          type: string
-                        title:
-                          type: string
-                  sectionHeadingText:
-                    type: string
-                  list:
-                    type: array
-                    items:
-                      type: object
-                      properties:
-                        bannerData:
-                          type: object
-                          properties:
-                            imageName:
-                              type: string
-                            headingText:
-                              type: string
-                            subHeadingText:
-                              type: string
-                            landingUrl:
-                              type: string
-                            rowStartTime:
-                              type: string
-                            rowEndTime:
-                              type: string
-                            timerPosition:
-                              type: string
-                            timerColor:
-                              type: string
-                        dimension:
-                          type: object
-                          properties:
-                            length:
-                              type: integer
-                            breadth:
-                              type: integer
-                        display_order:
-                          type: integer
-                        banner_title:
-                          type: string
-        404:
-          description: Layout not found
-
-  /widget/allLayout:
-    get:
-      summary: Get all layouts
-      responses:
-        200:
-          description: Successfully retrieved all layouts
+        '200':
+          description: Widgets retrieved successfully
           content:
             application/json:
               schema:
@@ -93,12 +29,12 @@ paths:
                 items:
                   type: object
                   properties:
+                    id:
+                      type: string
                     title:
                       type: string
                     layout:
                       type: string
-                    priority:
-                      type: integer
                     enable:
                       type: boolean
                     buttonData:
@@ -112,6 +48,8 @@ paths:
                             type: string
                     sectionHeadingText:
                       type: string
+                    apiUrl:
+                      type: string
                     list:
                       type: array
                       items:
@@ -128,14 +66,12 @@ paths:
                                 type: string
                               landingUrl:
                                 type: string
-                              rowStartTime:
-                                type: string
-                              rowEndTime:
-                                type: string
                               timerPosition:
                                 type: string
                               timerColor:
                                 type: string
+                          display_order:
+                            type: integer
                           dimension:
                             type: object
                             properties:
@@ -143,14 +79,21 @@ paths:
                                 type: integer
                               breadth:
                                 type: integer
-                          display_order:
-                            type: integer
                           banner_title:
                             type: string
+        '404':
+          description: Widgets not found
+          content:
+            application/json:
+              schema:
+                type: object
+                properties:
+                  error:
+                    type: string
 
-  /api/newLayout:
+  /widget/createWidget:
     post:
-      summary: Create a new layout
+      summary: Create a new widget
       requestBody:
         required: true
         content:
@@ -160,7 +103,7 @@ paths:
               properties:
                 title:
                   type: string
-                layout:
+                widget:
                   type: string
                 priority:
                   type: integer
@@ -177,6 +120,8 @@ paths:
                         type: string
                 sectionHeadingText:
                   type: string
+                apiUrl:
+                  type: string
                 list:
                   type: array
                   items:
@@ -193,14 +138,12 @@ paths:
                             type: string
                           landingUrl:
                             type: string
-                          rowStartTime:
-                            type: string
-                          rowEndTime:
-                            type: string
                           timerPosition:
                             type: string
                           timerColor:
                             type: string
+                      display_order:
+                        type: integer
                       dimension:
                         type: object
                         properties:
@@ -208,38 +151,64 @@ paths:
                             type: integer
                           breadth:
                             type: integer
-                      display_order:
-                        type: integer
                       banner_title:
                         type: string
       responses:
-        201:
-          description: New layout created successfully
+        '201':
+          description: Widget created successfully
           content:
             application/json:
               schema:
                 type: object
                 properties:
-                  message:
+                  id:
                     type: string
-                  layoutId:
+                  message:
                     type: string
-        400:
+        '400':
           description: Bad request
+          content:
+            application/json:
+              schema:
+                type: object
+                properties:
+                  error:
+                    type: string
 
-  /api/layout/{layout}:
-    delete:
-      summary: Delete a layout
+  /api/templates/{page}/widgets/{widgetId}:
+    put:
+      summary: Update and map a widget to a particular page
       parameters:
         - in: path
-          name: layout
+          name: page
           required: true
           schema:
             type: string
-          description: The layout identifier
+          description: The page to which the widget is to be mapped.
+        - in: path
+          name: widgetId
+          required: true
+          schema:
+            type: string
+          description: The widget ID that needs to be mapped.
+      requestBody:
+        required: true
+        content:
+          application/json:
+            schema:
+              type: object
+              properties:
+                position:
+                  type: integer
+                startTime:
+                  type: string
+                  format: date-time
+                endTime:
+                  type: string
+                  format: date-time
       responses:
-        200:
-          description: Layout deleted successfully
+        '200':
+          description: Widget mapped successfully
           content:
             application/json:
               schema:
@@ -247,5 +216,18 @@ paths:
                 properties:
                   message:
                     type: string
-        404:
-          description: Layout not found
+                  widgetId:
+                    type: string
+                  page:
+                    type: string
+                  position:
+                    type: integer
+        '400':
+          description: Bad request
+          content:
+            application/json:
+              schema:
+                type: object
+                properties:
+                  error:
+                    type: string
-- 
GitLab