sqlite
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { DELETE_BY_ARTICLE_AND_HASH } from '../../providers/mssql/queries/pic.js';
|
||||
import { DELETE_BY_ARTICLE_AND_HASH as MSSQL_QUERY } from '../../providers/mssql/queries/pic.js';
|
||||
import { DELETE_BY_ARTICLE_AND_HASH as SQLITE_QUERY } from '../../providers/sqlite3/queries/pic.js';
|
||||
import resolveArticleBySku from './resolveArticleBySku.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function deleteImage(pool) {
|
||||
const resolve = resolveArticleBySku(pool);
|
||||
export default function deleteImage(provider) {
|
||||
const resolve = resolveArticleBySku(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_QUERY : MSSQL_QUERY;
|
||||
|
||||
/**
|
||||
* Deletes an image from an article by its SKU and full image hash.
|
||||
@@ -10,23 +13,29 @@ export default function deleteImage(pool) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} sku - The article SKU.
|
||||
* @param {string} fullHash - The full image hash to delete.
|
||||
* @param {object} [tx] - Optional transaction.
|
||||
* @returns {Promise<void>}
|
||||
* @throws {Error} If the image is not found.
|
||||
*
|
||||
* @example
|
||||
* await store.article.deleteImage(42, 'SKU-1', 'abc123');
|
||||
*/
|
||||
return async (googleAuthId, sku, fullHash) => {
|
||||
const { articleId } = await resolve(googleAuthId, sku);
|
||||
return async (googleAuthId, sku, fullHash, tx) => {
|
||||
const { articleId } = await resolve(googleAuthId, sku, tx);
|
||||
const runner = makeRunner(provider, tx);
|
||||
const result = await runner.query(sql, { articleId, fullHash });
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('articleId', articleId)
|
||||
.input('fullHash', fullHash)
|
||||
.query(DELETE_BY_ARTICLE_AND_HASH);
|
||||
|
||||
if (!result.rowsAffected[0]) {
|
||||
if (!result.rowsAffected) {
|
||||
throw new Error('Image not found');
|
||||
}
|
||||
|
||||
if (provider?.dialect === 'sqlite3') {
|
||||
if (runner.deletePic) {
|
||||
await runner.deletePic(fullHash);
|
||||
} else if (provider?.deletePic) {
|
||||
await provider.deletePic(fullHash);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user