added PageTop component, markdown styles, white background on logo svg, and documentation comments
This commit is contained in:
65
server/api/getArticle.ts
Normal file
65
server/api/getArticle.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Getting article from database.
|
||||
* @module api/getArticle
|
||||
*/
|
||||
|
||||
import sqlite3 from "sqlite3";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import { asyncDatabaseRead } from "~/utils/asyncDatabase";
|
||||
import type { ArticleInfo } from "~/utils/news";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
/**
|
||||
* Handler of getArticle event. Entry is selected by linkPath
|
||||
* @name getArticleEventHandler
|
||||
* @param {H3Event<EventHandlerRequest>} event
|
||||
* @returns {ArticleInfo} - object that contains information and content of article
|
||||
* @function
|
||||
*/
|
||||
const getArticleEventHandler = defineEventHandler(async (event: any) => {
|
||||
const database = new sqlite3.Database(
|
||||
path.join(__dirname, "../../assets/databases/news.db")
|
||||
);
|
||||
|
||||
let res: ArticleInfo = {
|
||||
date: 0,
|
||||
cardContent: "",
|
||||
article: "",
|
||||
linkPath: "",
|
||||
coverImagePath: "",
|
||||
};
|
||||
|
||||
const target = "/news/" + getQuery(event).name;
|
||||
const sql = `SELECT date, cardContent, article, linkPath, coverImagePath FROM news WHERE linkPath = "${target}" AND entryType = 0;`;
|
||||
|
||||
try {
|
||||
const entry: ArticleInfo = await asyncDatabaseRead<ArticleInfo>(
|
||||
database,
|
||||
sql,
|
||||
(rows) => {
|
||||
return rows[0];
|
||||
}
|
||||
);
|
||||
res = {
|
||||
date: entry.date,
|
||||
cardContent: entry.cardContent,
|
||||
article: entry.article,
|
||||
linkPath: entry.linkPath,
|
||||
coverImagePath: entry.coverImagePath,
|
||||
};
|
||||
} catch (err: any) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: err.message,
|
||||
});
|
||||
}
|
||||
|
||||
database.close();
|
||||
|
||||
return res;
|
||||
});
|
||||
|
||||
export default getArticleEventHandler;
|
||||
Reference in New Issue
Block a user