From b5830356305c75f96812a899de253533684946ca Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 23 Mar 2022 18:57:29 -0400 Subject: [PATCH] fix(striker-ui-api): allow GET multiple files' detail --- .../files/buildQueryFilesDetail.ts | 31 +++++++++++++++++++ .../request_handlers/files/getFileDetail.ts | 23 ++------------ .../lib/request_handlers/files/getFiles.ts | 24 ++++++++++++++ .../files/getFilesOverview.ts | 13 -------- striker-ui-api/src/routes/files.ts | 4 +-- 5 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 striker-ui-api/src/lib/request_handlers/files/buildQueryFilesDetail.ts create mode 100644 striker-ui-api/src/lib/request_handlers/files/getFiles.ts delete mode 100644 striker-ui-api/src/lib/request_handlers/files/getFilesOverview.ts diff --git a/striker-ui-api/src/lib/request_handlers/files/buildQueryFilesDetail.ts b/striker-ui-api/src/lib/request_handlers/files/buildQueryFilesDetail.ts new file mode 100644 index 00000000..2e21d764 --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/files/buildQueryFilesDetail.ts @@ -0,0 +1,31 @@ +const buildQueryFilesDetail = ({ filesUUID }: { filesUUID?: string[] }) => { + let condFilesUUID = ''; + + if (filesUUID instanceof Array && filesUUID.length > 0) { + const catFilesUUID = `'${filesUUID.join("', '")}'`; + + condFilesUUID = `AND fil.file_uuid IN (${catFilesUUID})`; + } + + return ` + SELECT + fil.file_uuid, + fil.file_name, + fil.file_size, + fil.file_type, + fil.file_md5sum, + fil_loc.file_location_uuid, + fil_loc.file_location_active, + anv.anvil_uuid, + anv.anvil_name, + anv.anvil_description + FROM files AS fil + JOIN file_locations AS fil_loc + ON fil.file_uuid = fil_loc.file_location_file_uuid + JOIN anvils AS anv + ON fil_loc.file_location_anvil_uuid = anv.anvil_uuid + WHERE fil.file_type != 'DELETED' + ${condFilesUUID};`; +}; + +export default buildQueryFilesDetail; diff --git a/striker-ui-api/src/lib/request_handlers/files/getFileDetail.ts b/striker-ui-api/src/lib/request_handlers/files/getFileDetail.ts index c32d8d6f..a8c0da0e 100644 --- a/striker-ui-api/src/lib/request_handlers/files/getFileDetail.ts +++ b/striker-ui-api/src/lib/request_handlers/files/getFileDetail.ts @@ -1,25 +1,8 @@ import buildGetFiles from './buildGetFiles'; +import buildQueryFilesDetail from './buildQueryFilesDetail'; -const getFileDetail = buildGetFiles( - (request) => - `SELECT - fil.file_uuid, - fil.file_name, - fil.file_size, - fil.file_type, - fil.file_md5sum, - fil_loc.file_location_uuid, - fil_loc.file_location_active, - anv.anvil_uuid, - anv.anvil_name, - anv.anvil_description - FROM files AS fil - JOIN file_locations AS fil_loc - ON fil.file_uuid = fil_loc.file_location_file_uuid - JOIN anvils AS anv - ON fil_loc.file_location_anvil_uuid = anv.anvil_uuid - WHERE fil.file_uuid = '${request.params.fileUUID}' - AND fil.file_type != 'DELETED';`, +const getFileDetail = buildGetFiles((request) => + buildQueryFilesDetail({ filesUUID: [request.params.fileUUID] }), ); export default getFileDetail; diff --git a/striker-ui-api/src/lib/request_handlers/files/getFiles.ts b/striker-ui-api/src/lib/request_handlers/files/getFiles.ts new file mode 100644 index 00000000..7665c7e3 --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/files/getFiles.ts @@ -0,0 +1,24 @@ +import buildGetFiles from './buildGetFiles'; +import buildQueryFilesDetail from './buildQueryFilesDetail'; + +const getFiles = buildGetFiles((request) => { + const { filesUUID } = request.body; + + let query = ` + SELECT + file_uuid, + file_name, + file_size, + file_type, + file_md5sum + FROM files + WHERE file_type != 'DELETED';`; + + if (filesUUID) { + query = buildQueryFilesDetail({ filesUUID }); + } + + return query; +}); + +export default getFiles; diff --git a/striker-ui-api/src/lib/request_handlers/files/getFilesOverview.ts b/striker-ui-api/src/lib/request_handlers/files/getFilesOverview.ts deleted file mode 100644 index 1acd9b5a..00000000 --- a/striker-ui-api/src/lib/request_handlers/files/getFilesOverview.ts +++ /dev/null @@ -1,13 +0,0 @@ -import buildGetFiles from './buildGetFiles'; - -const getFilesOverview = buildGetFiles(` -SELECT - file_uuid, - file_name, - file_size, - file_type, - file_md5sum -FROM files -WHERE file_type != 'DELETED';`); - -export default getFilesOverview; diff --git a/striker-ui-api/src/routes/files.ts b/striker-ui-api/src/routes/files.ts index d1e093ae..13bf74e7 100644 --- a/striker-ui-api/src/routes/files.ts +++ b/striker-ui-api/src/routes/files.ts @@ -6,7 +6,7 @@ import { dbSubRefreshTimestamp, dbWrite, } from '../lib/accessDB'; -import getFilesOverview from '../lib/request_handlers/files/getFilesOverview'; +import getFiles from '../lib/request_handlers/files/getFiles'; import getFileDetail from '../lib/request_handlers/files/getFileDetail'; import uploadSharedFiles from '../middlewares/uploadSharedFiles'; @@ -37,7 +37,7 @@ router response.status(204).send(); }) - .get('/', getFilesOverview) + .get('/', getFiles) .get('/:fileUUID', getFileDetail) .post('/', uploadSharedFiles.single('file'), ({ file, body }, response) => { console.log('Receiving shared file.');