Files
sera-new-hp/src-manager/docs/manager/1.0.0/api_gallery-image.js.html

287 lines
8.7 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: api/gallery-image.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: api/gallery-image.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/** @module api/gallery-image */
import express from 'express';
import sqlite3 from 'sqlite3';
import path from 'path';
import { fileURLToPath } from 'url';
import { asyncDatabaseRead, asyncDatabaseWrite } from '../utils/asyncDatabase.js';
import { wrapInTable } from '../utils/tableWrapper.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");
function generateActionButtons(target) {
return `&lt;button class='delete-button' hx-delete='/api/gallery-image?target=${target}'>Delete&lt;/button>&lt;a href='/update-gallery-image.html?target=${target}'>&lt;button class='edit-button' hx-confirm='unset'>Edit&lt;/button>&lt;/a>`;
}
/**
* Gets list of gallery image entries in HTML table body tr+td
* @param {Object} request
* @param {Object} response
* @returns {string} HTML table body tr+td
* @example
* $ curl -X GET http://localhost:3001/api/gallery-image/list
* // &lt;tr>
* // &lt;td>1&lt;/td>
* // &lt;td>/image/test.png&lt;/td>
* // ...
* // &lt;/tr>
* // ...
*/
const getGalleryImageList = async (request, response) => {
const database = new sqlite3.Database(databasePath);
const sqlQuery = "SELECT * FROM gallery";
try {
const result = await asyncDatabaseRead(database, sqlQuery, (rows) => {
let ret = "";
let rowsCopy = [...rows];
const withActionButtons = rowsCopy.map((entry) => {
return { ...entry, buttons: generateActionButtons(entry.id) };
});
ret = wrapInTable(withActionButtons);
return ret;
});
database.close()
response.send(result);
} catch (err) {
console.error(err);
database.close();
response.status(500).send(err);
}
};
galleryImageAPI.get('/list',getGalleryImageList);
/**
* Gets list of gallery image entries in Unformatted JSON string
* @param {Object} request
* @param {Object} response
* @returns {string} Unformatted JSON string that contains gallery image entries
* @example
* $ curl -X GET \
* http://localhost:3001/api/gallery-image/list-unwrapped
* // gets raw JSON containing gallery image entries
*/
const getGalleryImageListUnwrapped = async (request, response) => {
const database = new sqlite3.Database(databasePath);
const sqlQuery = "SELECT * FROM gallery";
try {
const result = await asyncDatabaseRead(database, sqlQuery, (rows) => { return rows; });
database.close();
response.send(result);
} catch (err) {
console.error(err);
database.close();
response.status(500).send(err);
}
};
galleryImageAPI.get('/list-unwrapped', getGalleryImageListUnwrapped);
/**
* Get a gallery image information
* @param {Object} request
* @param {number} request.query.target - ID to specify gallery image entry
* @param {Object} response
* @returns {JSON} JSON that contains information about gallery image
* @example
* $ curl -X GET http://localhost:3001/api/gallery-image?target=1
* // gets gallery image information with ID of 1
*/
const getGalleryImage = 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();
response.status(500).send(err);
}
};
galleryImageAPI.get('/', getGalleryImage);
/**
* Posts gallery image
* @param {Object} request
* @param {string} request.body.imagePath - URL path to image
* @param {string} request.body.caption - Caption of image
* @param {Object} response
* @returns Result is logged into console
* @example
* $ curl -X POST \
* -H 'Content-Type: application/x-www-form-urlencoded' \
* --data-raw 'imagePath=/images/launch.png&amp;caption=Launch of our new rocket' \
* http://localhost:3001/api/gallery-image/
* // Posts gallery image with given information
*/
const postGalleryImage = 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) {
console.error(err);
response.status(500).send(err);
}
database.close();
response.end();
};
galleryImageAPI.post('/', postGalleryImage);
/**
* Updates gallery image
* @param {Object} request
* @param {number} request.body.target - ID to specify gallery image entry
* @param {string} request.body.imagePath - URL path to image
* @param {string} request.body.caption - Caption of image
* @param {Object} response
* @returns Result is logged into console
* @example
* $ curl -X PUT \
* -H 'Content-Type: application/x-www-form-urlencoded' \
* --data-raw 'imagePath=/images/launch.png&amp;caption=Launch of our new rocket' \
* http://localhost:3001/api/gallery-image/
* // Updates gallery image with given information
*/
const putGalleryImage = 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) {
console.error(err);
response.status(500).send(err);
}
database.close();
response.end();
};
galleryImageAPI.put('/', putGalleryImage);
/**
* Deletes specified gallery image
* @param {Object} request
* @param {string} request.query.target - ID to specify gallery image entry
* @param {Object} response
* @returns Result is logged into console
* @example
* $ curl -X DELETE \
* http://localhost:3001/api/gallery-image?target=1
* // Deletes gallery image with ID of 1
*/
const deleteGalleryImage = 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) {
console.error(err);
response.status(500).send(err);
}
database.close();
response.status(200).send();
};
galleryImageAPI.delete('/', deleteGalleryImage);
export default galleryImageAPI;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-api_gallery-image.html">api/gallery-image</a></li><li><a href="module-api_news.html">api/news</a></li><li><a href="module-utils_asyncDatabase.html">utils/asyncDatabase</a></li><li><a href="module-utils_tableWrapper.html">utils/tableWrapper</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Tue Nov 26 2024 23:18:12 GMT+0900 (日本標準時)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>