diff --git a/api/apiBaseRouter.js b/api/apiBaseRouter.js index ebf3692bfd9c1afd30137ed0ea24b123ded6d3cc..113178f5e277258fabceb34b49def1416c55bd36 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 ece8010705cbba631da3ffeaf8968b24477936ca..9dc4c6cfc1e6b48288b7df6f5a127d63d060f38b 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 b543c6954facf1618bfa48b12095248242c05156..b53a331ea2b038ba5bf9055b1940263556be8d60 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 157994b1cf9b9d77f0eb638151748814afed2555..6d1628ab390857c22db888452f77e86dc87dec47 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 6a3529f444c40b8d0d5806ad7371372fa66e202c..705512b899812413ba854892fa4f60fe91ac8f50 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 0000000000000000000000000000000000000000..b72e0ae51dbd514eea38ad2efd5952bf0204e132 --- /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 0000000000000000000000000000000000000000..ffb6154e096952e4951ce6893a6ad8d74f492a4c --- /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 0000000000000000000000000000000000000000..63d69c3e6e30928410736dcef2bcf4908397ee9c --- /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 0000000000000000000000000000000000000000..1e1554b7631ef2c7fd260796c42937737cf1acf3 --- /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 0f8c0f66d228e61047c45141317ca474bf65a459..b170eb5bc2afe077fc5dc30432445a85b084d78d 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."