Tar bort dubbel kod

This commit is contained in:
2025-01-24 23:37:19 +01:00
parent 87feaa9880
commit 90d7f2a5d2
4 changed files with 240 additions and 93 deletions

View File

@@ -1,8 +1,8 @@
const express = require('express');
const fs = require('fs');
const path = require('path');
const debug = require('debug')('app');
const auth = require('../middleware/auth');
const logger = require('../logger');
const router = express.Router();
const dataDir = '/data';
@@ -28,7 +28,8 @@ router.post('/add-task', auth, async (req, res) => {
SCHEDULED: <${scheduled}>
:LOGBOOK:
- State "TODO" from "TODO" [${currentDateTime}]
:END:`;
:END:
`;
if (description) {
orgFormattedData = `
@@ -37,7 +38,8 @@ router.post('/add-task', auth, async (req, res) => {
SCHEDULED: <${scheduled}>
:LOGBOOK:
- State "TODO" from "TODO" [${currentDateTime}]
:END:`;
:END:
`;
}
const filePath = path.join(dataDir, 'tasks.org');
@@ -45,7 +47,7 @@ router.post('/add-task', auth, async (req, res) => {
await fs.promises.appendFile(filePath, orgFormattedData);
res.send({ message: 'Task added successfully!' });
} catch (err) {
debug('Error writing to file:', err);
logger.error('Error writing to file:', err);
res.status(500).send('Error writing to file.');
}
});
@@ -58,7 +60,7 @@ router.post('/save-tags', auth, async (req, res) => {
await fs.promises.writeFile(filePath, JSON.stringify(tags));
res.send({ message: 'Tags saved successfully!' });
} catch (err) {
debug('Error saving tags:', err);
logger.error('Error saving tags:', err);
res.status(500).send('Error saving tags.');
}
});
@@ -67,12 +69,12 @@ router.post('/save-tags', auth, async (req, res) => {
router.get('/get-tags', auth, async (req, res) => {
const filePath = path.join(dataDir, 'tags.json');
try {
const data = await fs.promises.readFile(filePath);
const data = await fs.promises.readFile(filePath, 'utf-8');
const tags = JSON.parse(data);
res.send(tags);
res.json(tags);
} catch (err) {
debug('Error retrieving tags:', err);
res.status(500).send('Error retrieving tags.');
logger.error('Error retrieving tags:', err);
res.status(500).json({ error: 'Error retrieving tags' });
}
});