Skip to content

Commit

Permalink
Merge pull request #12 from UniversityOfNicosia:refactor/document-gen…
Browse files Browse the repository at this point in the history
…eration-restructure

Refactor: Updated document creation logic and file structure
  • Loading branch information
HlexNC authored May 6, 2024
2 parents 981aa76 + 0cffba8 commit a2ff3e4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 78 deletions.
10 changes: 5 additions & 5 deletions document-generation-lib/src/app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const express = require('express');
const documentLibrary = require('./documentLibrary');
const documentManager = require('./lib/documentManager')

const app = express();
app.use(express.json());

app.post('/api/create-document', async (req, res) => {
router.post('/create-document', async (req, res) => {
try {
const resultPath = await documentLibrary.createDocumentWithStructure(req.body.elements, req.body.styles);
const resultPath = await documentManager.createDocumentWithStructure(req.body.elements, req.body.styles);
res.status(201).send({ message: 'Document created successfully.', path: resultPath });
} catch (error) {
console.error('Failed to create document:', error);
res.status(500).send({ message: 'Failed to create document due to an internal error.' });
}
});

app.post('/api/create-document/buffer', async (req, res) => {
router.post('/create-document/buffer', async (req, res) => {
try {
const buffer = await documentLibrary.createDocumentBuffer(req.body.elements, req.body.styles);
const buffer = await documentManager.createDocumentBuffer(req.body.elements, req.body.styles);
res.setHeader('Content-Disposition', 'attachment; filename=document.docx');
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
res.send(buffer);
Expand Down
27 changes: 0 additions & 27 deletions document-generation-lib/src/controllers/documentController.js

This file was deleted.

31 changes: 0 additions & 31 deletions document-generation-lib/src/documentLibrary.js

This file was deleted.

31 changes: 31 additions & 0 deletions document-generation-lib/src/lib/documentManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const officegenHelper = require('../utils/officegenHelper');

exports.createDocumentWithStructure = async (elements, styles) => {
try {
if (!elements) throw new Error('Document elements are required.');

const timestamp = new Date().getTime();
const outputPath = `document_${timestamp}.docx`;
const resultPath = await officegenHelper.createDocxWithStructure(elements, styles, outputPath);
return resultPath;
} catch (error) {
throw error;
}
};

exports.createDocumentBuffer = async (elements, styles) => {
try {
const buffer = await officegenHelper.createDocxBuffer(elements, styles);
return buffer;
} catch (error) {
throw error;
}
};

exports.createPresentationWithStructure = async (elements, styles) => {
return "Hello, world! Presentation created.";
};

exports.createPresentationBuffer = async (elements, styles) => {
return "Hello, world! Presentation buffer created.";
}
14 changes: 0 additions & 14 deletions document-generation-lib/src/services/documentService.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require("assert");
const fs = require("fs");
const path = require("path");
const { createDocumentWithStructure, createDocumentBuffer } = require("../src/documentLibrary");
const { createDocumentBuffer } = require("../src/lib/documentManager");
const { createDocxWithStructure } = require("../src/utils/officegenHelper");

// Directory for test outputs
Expand Down

0 comments on commit a2ff3e4

Please sign in to comment.