Add category image add/delete with categorypic SQL layer
This commit is contained in:
66
src/store/category/addImage.js
Normal file
66
src/store/category/addImage.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import { ADD as ADD_CATEGORYPIC, GET_NEXT_SORT } from '../../sql/categorypic.js';
|
||||
import { CREATE as CREATE_PIC } from '../../sql/pic.js';
|
||||
import processAvifImage from '../../util/processAvifImage.js';
|
||||
import newFullHash from '../../util/newFullHash.js';
|
||||
import request from '../request.js';
|
||||
import resolveCategoryAccess from './resolveCategoryAccess.js';
|
||||
|
||||
export default function addImage(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
|
||||
return async (googleAuthId, categoryId, image) => {
|
||||
await access(googleAuthId, categoryId);
|
||||
|
||||
const processed = await processAvifImage(image);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const sortResult = await request(pool, tx)
|
||||
.input('categoryId', categoryId)
|
||||
.query(GET_NEXT_SORT);
|
||||
|
||||
const sort = sortResult.recordset[0].sort;
|
||||
const fullHash = newFullHash();
|
||||
|
||||
const picResult = await request(pool, tx)
|
||||
.input('w', processed.full.w)
|
||||
.input('h', processed.full.h)
|
||||
.input('size', processed.full.size)
|
||||
.input('hash', fullHash)
|
||||
.input('thumbw', processed.thumb.w)
|
||||
.input('thumbh', processed.thumb.h)
|
||||
.input('thumbhash', processed.thumb.hash)
|
||||
.input('source', '')
|
||||
.input('fullbinary', processed.full.buffer)
|
||||
.input('thumbbinary', processed.thumb.buffer)
|
||||
.query(CREATE_PIC);
|
||||
|
||||
const picId = picResult.recordset[0].id;
|
||||
|
||||
await request(pool, tx)
|
||||
.input('categoryId', categoryId)
|
||||
.input('picId', picId)
|
||||
.input('sort', sort)
|
||||
.query(ADD_CATEGORYPIC);
|
||||
|
||||
await tx.commit();
|
||||
|
||||
return {
|
||||
id: categoryId,
|
||||
fullHash,
|
||||
thumbW: processed.thumb.w,
|
||||
thumbH: processed.thumb.h,
|
||||
thumbHash: processed.thumb.hash,
|
||||
};
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
26
src/store/category/deleteImage.js
Normal file
26
src/store/category/deleteImage.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { DELETE_BY_CATEGORY } from '../../sql/categorypic.js';
|
||||
import resolveCategoryAccess from './resolveCategoryAccess.js';
|
||||
|
||||
export default function deleteImage(pool) {
|
||||
const access = resolveCategoryAccess(pool);
|
||||
|
||||
return async (googleAuthId, categoryId) => {
|
||||
await access(googleAuthId, categoryId);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('categoryId', categoryId)
|
||||
.query(DELETE_BY_CATEGORY);
|
||||
|
||||
if (!result.rowsAffected[0]) {
|
||||
throw new Error('Image not found');
|
||||
}
|
||||
|
||||
return {
|
||||
id: categoryId,
|
||||
thumbW: null,
|
||||
thumbH: null,
|
||||
thumbHash: null,
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user