Added gallery database and refactored src-manager
This commit is contained in:
133
src-manager/api/gallery-image.js
Normal file
133
src-manager/api/gallery-image.js
Normal file
@@ -0,0 +1,133 @@
|
||||
import express from 'express';
|
||||
import sqlite3 from 'sqlite3';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { asyncDatabaseRead, asyncDatabaseWrite } from '../utils/asyncDatabase.js';
|
||||
|
||||
const galleryImageAPI = express.Router();
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const databasePath = path.join(__dirname, "../../assets/databases/gallery.db");
|
||||
|
||||
galleryImageAPI.get('/list', async (request, response) => {
|
||||
const database = new sqlite3.Database(databasePath);
|
||||
const sqlQuery = "SELECT * FROM gallery";
|
||||
|
||||
try {
|
||||
const result = await asyncDatabaseRead(database, sqlQuery, (rows) => {
|
||||
let ret = "";
|
||||
for (const entry of rows) {
|
||||
ret = ret + "<tr>\n";
|
||||
for (const data in entry) {
|
||||
ret = ret + `<td>${entry[data]}</td>\n`;
|
||||
}
|
||||
ret = ret + `<td>\n
|
||||
<button class='delete-button' hx-delete='/api/gallery-image?target=${entry["id"]}'>Delete</button>\n
|
||||
<a href='/update-gallery-image.html?target=${entry["id"]}'><button class='edit-button' hx-confirm='unset'>Edit</button></a>\n
|
||||
</td>\n</tr>\n`;
|
||||
}
|
||||
return ret;
|
||||
});
|
||||
|
||||
database.close()
|
||||
response.send(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
database.close();
|
||||
}
|
||||
});
|
||||
|
||||
galleryImageAPI.get('/', async (request, response) => {
|
||||
const database = new sqlite3.Database(databasePath);
|
||||
|
||||
const target = Number(request.query.target);
|
||||
|
||||
if (isNaN(target)) {
|
||||
response.status(500).send("Query is not number");
|
||||
return;
|
||||
}
|
||||
|
||||
const sqlQuery = `SELECT * FROM gallery WHERE id = ${target}`;
|
||||
|
||||
try {
|
||||
const result = await asyncDatabaseRead(database, sqlQuery, (rows) => {return rows[0]});
|
||||
database.close();
|
||||
response.send(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
database.close();
|
||||
}
|
||||
});
|
||||
|
||||
galleryImageAPI.post('/', async (request, response) => {
|
||||
const database = new sqlite3.Database(databasePath);
|
||||
|
||||
const imagePath = request.body.imagePath;
|
||||
const caption = request.body.caption;
|
||||
|
||||
const sqlQuery = `INSERT INTO gallery (imagePath, caption) VALUES ("${imagePath}", "${caption}");`;
|
||||
|
||||
try {
|
||||
await asyncDatabaseWrite(database, sqlQuery, () => {
|
||||
console.log("Image added successfully.");
|
||||
});
|
||||
} catch (err) {
|
||||
response.status(500).send(err);
|
||||
}
|
||||
|
||||
database.close();
|
||||
response.end();
|
||||
});
|
||||
|
||||
galleryImageAPI.put('/', async (request, response) => {
|
||||
const database = new sqlite3.Database(databasePath);
|
||||
|
||||
const target = Number(request.body.target);
|
||||
|
||||
if (isNaN(target)) {
|
||||
response.status(500).send("Query is not number");
|
||||
return;
|
||||
}
|
||||
|
||||
const imagePath = request.body.imagePath;
|
||||
const caption = request.body.caption;
|
||||
|
||||
const sqlQuery = `UPDATE gallery SET imagePath = "${imagePath}", caption = "${caption}" WHERE id = ${target};`;
|
||||
|
||||
try {
|
||||
await asyncDatabaseWrite(database, sqlQuery, () => {
|
||||
console.log("Image updated successfully.");
|
||||
});
|
||||
} catch (err) {
|
||||
response.status(500).send(err);
|
||||
}
|
||||
|
||||
database.close();
|
||||
response.end();
|
||||
});
|
||||
|
||||
galleryImageAPI.delete('/', async (request, response) => {
|
||||
const database = new sqlite3.Database(databasePath);
|
||||
|
||||
const target = Number(request.query.target);
|
||||
|
||||
if (isNaN(target)) {
|
||||
response.status(404).send('No Entry Found!');
|
||||
return;
|
||||
}
|
||||
|
||||
const sqlQuery = `DELETE FROM gallery WHERE id = ${target};`;
|
||||
|
||||
try {
|
||||
await asyncDatabaseWrite(database, sqlQuery, () => {
|
||||
console.log("Image deleted successfully.");
|
||||
});
|
||||
} catch (err) {
|
||||
response.status(500).send(err);
|
||||
}
|
||||
|
||||
database.close();
|
||||
response.status(200).send();
|
||||
});
|
||||
|
||||
export default galleryImageAPI;
|
||||
13
src-manager/api/index.js
Normal file
13
src-manager/api/index.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import express from 'express';
|
||||
|
||||
import newsAPI from './news.js';
|
||||
import newsListAPI from './news-list.js';
|
||||
import galleryImageAPI from './gallery-image.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use('/news', newsAPI);
|
||||
router.use('/news-list', newsListAPI);
|
||||
router.use('/gallery-image', galleryImageAPI);
|
||||
|
||||
export default router;
|
||||
47
src-manager/api/news-list.js
Normal file
47
src-manager/api/news-list.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import express from 'express';
|
||||
import sqlite3 from 'sqlite3';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { asyncDatabaseRead } from '../utils/asyncDatabase.js';
|
||||
|
||||
const newsListAPI = express.Router();
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const databasePath = path.join(__dirname, "../../assets/databases/news.db");
|
||||
|
||||
newsListAPI.get('/', async (request, response) => {
|
||||
const database = new sqlite3.Database(databasePath);
|
||||
const sqlQuery = `SELECT * FROM news ORDER BY date DESC;`;
|
||||
try {
|
||||
const result = await asyncDatabaseRead(database, sqlQuery, (rows) => {
|
||||
let ret = "";
|
||||
for (const entry of rows) {
|
||||
ret = ret + "<tr>\n";
|
||||
for (const data in entry) {
|
||||
if (data == "article" || data == "coverImagePath" || data == "linkPath") continue;
|
||||
if (data == "entryType") {
|
||||
ret = ret + `<td>${entry[data] == 0 ? "Article" : "Tweet"}</td>\n`;
|
||||
continue;
|
||||
}
|
||||
if (data == "date") {
|
||||
ret = ret + `<td>${new Date(entry[data]).toLocaleString()}</td>\n`;
|
||||
continue;
|
||||
}
|
||||
ret = ret + `<td>${entry[data]}</td>\n`;
|
||||
}
|
||||
ret = ret + `<td>\n
|
||||
<button class='delete-button' hx-delete='/api/news?target=${entry["date"]}'>Delete</button>\n
|
||||
<a href='/update-news.html?target=${entry["date"]}'><button class='edit-button' hx-confirm='unset'>Edit</button></a>\n
|
||||
</td>\n</tr>\n`;
|
||||
}
|
||||
return ret
|
||||
});
|
||||
database.close();
|
||||
response.send(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
database.close();
|
||||
}
|
||||
});
|
||||
|
||||
export default newsListAPI;
|
||||
112
src-manager/api/news.js
Normal file
112
src-manager/api/news.js
Normal file
@@ -0,0 +1,112 @@
|
||||
import express from 'express';
|
||||
import sqlite3 from 'sqlite3';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { asyncDatabaseRead, asyncDatabaseWrite } from '../utils/asyncDatabase.js';
|
||||
|
||||
const newsAPI = express.Router();
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const databasePath = path.join(__dirname, "../../assets/databases/news.db");
|
||||
|
||||
newsAPI.get('/', async (request, response) => {
|
||||
const database = new sqlite3.Database(databasePath);
|
||||
|
||||
const target = Number(request.query.target);
|
||||
|
||||
if (isNaN(target)) {
|
||||
response.status(500).send("Query is not number");
|
||||
return;
|
||||
}
|
||||
|
||||
const sqlQuery = `SELECT * FROM news WHERE date = ${target}`;
|
||||
|
||||
try {
|
||||
const result = await asyncDatabaseRead(database, sqlQuery, (rows) => {return rows[0]});
|
||||
database.close();
|
||||
response.send(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
database.close();
|
||||
}
|
||||
});
|
||||
|
||||
newsAPI.post('/', async (request, response) => {
|
||||
const database = new sqlite3.Database(databasePath);
|
||||
|
||||
const currentDate = new Date();
|
||||
const currentUnixTime = currentDate.valueOf();
|
||||
const entryType = request.body.entryType;
|
||||
const cardContent = request.body.cardContent;
|
||||
const article = request.body.article;
|
||||
const linkPath = entryType == 0 ? `/news/${request.body.linkPath}` : "";
|
||||
const coverImagePath = request.body.coverImagePath;
|
||||
|
||||
const sqlQuery = `INSERT INTO news (date, entryType, cardContent, article, linkPath, coverImagePath) VALUES (${currentUnixTime}, ${entryType}, "${cardContent}", "${article}", "${linkPath}", "${coverImagePath}");`;
|
||||
|
||||
try {
|
||||
await asyncDatabaseWrite(database, sqlQuery, () => {
|
||||
console.log("News added successfully.");
|
||||
});
|
||||
} catch (err) {
|
||||
response.status(500).send(err);
|
||||
}
|
||||
database.close();
|
||||
response.end();
|
||||
});
|
||||
|
||||
newsAPI.put('/', async (request, response) => {
|
||||
const database = new sqlite3.Database(databasePath);
|
||||
|
||||
const target = Number(request.body.target);
|
||||
const entryType = Number(request.body.entryType);
|
||||
const cardContent = request.body.cardContent;
|
||||
const article = request.body.article;
|
||||
const linkPath = entryType == 0 ? `/news/${request.body.linkPath}` : "";
|
||||
const coverImagePath = request.body.coverImagePath;
|
||||
|
||||
if (isNaN(target)) {
|
||||
response.status(500).send('Target is not number');
|
||||
return;
|
||||
}
|
||||
|
||||
const sqlQuery = `UPDATE news SET entryType = ${entryType}, cardContent = "${cardContent}", article = "${article}", linkPath = "${linkPath}", coverImagePath = "${coverImagePath}" WHERE date = ${target};`;
|
||||
|
||||
try {
|
||||
await asyncDatabaseWrite(database, sqlQuery, () => {
|
||||
console.log("News updated successfully.");
|
||||
});
|
||||
} catch (err) {
|
||||
response.status(500).send(err);
|
||||
}
|
||||
|
||||
database.close();
|
||||
response.end();
|
||||
});
|
||||
|
||||
newsAPI.delete('/', async (request, response) => {
|
||||
const database = new sqlite3.Database(databasePath);
|
||||
|
||||
const target = Number(request.query.target);
|
||||
|
||||
if (isNaN(target)) {
|
||||
response.status(404).send('No Entry Found!');
|
||||
return;
|
||||
}
|
||||
|
||||
const sqlQuery = `DELETE FROM news WHERE date = ${target}`;
|
||||
|
||||
try {
|
||||
await asyncDatabaseWrite(database, sqlQuery, () => {
|
||||
console.log("News deleted successfully.");
|
||||
});
|
||||
} catch (err) {
|
||||
response.status(500).send(err);
|
||||
}
|
||||
|
||||
database.close();
|
||||
|
||||
response.status(200).send();
|
||||
});
|
||||
|
||||
export default newsAPI;
|
||||
Reference in New Issue
Block a user