From 0f8daaac79ca26659b898cbcd28e2647bf1b0115 Mon Sep 17 00:00:00 2001 From: rijkvanzanten Date: Tue, 30 Jun 2020 15:03:18 -0400 Subject: [PATCH] Add read single --- src/exceptions/collection-not-found.ts | 7 +++++++ src/exceptions/index.ts | 1 + src/routes/collections.ts | 16 ++++++++++++++++ src/services/collections.ts | 16 ++++++++++++++++ src/services/items.ts | 12 ++++++------ 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 src/exceptions/collection-not-found.ts diff --git a/src/exceptions/collection-not-found.ts b/src/exceptions/collection-not-found.ts new file mode 100644 index 0000000000..434ed097c9 --- /dev/null +++ b/src/exceptions/collection-not-found.ts @@ -0,0 +1,7 @@ +import { BaseException } from './base'; + +export class CollectionNotFoundException extends BaseException { + constructor(collection: string) { + super(`Collection "${collection}" doesn't exist.`, 404, 'COLLECTION_NOT_FOUND'); + } +} diff --git a/src/exceptions/index.ts b/src/exceptions/index.ts index 73cc014255..c35145cc91 100644 --- a/src/exceptions/index.ts +++ b/src/exceptions/index.ts @@ -5,3 +5,4 @@ export * from './invalid-query'; export * from './item-limit'; export * from './route-not-found'; export * from './item-not-found'; +export * from './collection-not-found'; diff --git a/src/routes/collections.ts b/src/routes/collections.ts index 9a5fdf3b2c..07a934d426 100644 --- a/src/routes/collections.ts +++ b/src/routes/collections.ts @@ -3,6 +3,8 @@ import asyncHandler from 'express-async-handler'; import sanitizeQuery from '../middleware/sanitize-query'; import validateQuery from '../middleware/validate-query'; import * as CollectionsService from '../services/collections'; +import { schemaInspector } from '../database'; +import { CollectionNotFoundException } from '../exceptions'; const router = Router(); @@ -16,4 +18,18 @@ router.get( }) ); +router.get( + '/:collection', + sanitizeQuery, + validateQuery, + asyncHandler(async (req, res) => { + const exists = await schemaInspector.hasTable(req.params.collection); + + if (exists === false) throw new CollectionNotFoundException(req.params.collection); + + const data = await CollectionsService.readOne(req.params.collection, req.sanitizedQuery); + res.json({ data }); + }) +); + export default router; diff --git a/src/services/collections.ts b/src/services/collections.ts index bbe4cbe88e..669a50f621 100644 --- a/src/services/collections.ts +++ b/src/services/collections.ts @@ -27,3 +27,19 @@ export const readAll = async (query?: Query) => { return data; }; + +export const readOne = async (collection: string, query?: Query) => { + const [table, collectionInfo] = await Promise.all([ + schemaInspector.table(collection), + ItemsService.readItem('directus_collections', collection, query), + ]); + + return { + collection: table.name, + note: table.comment, + hidden: collectionInfo?.hidden || false, + single: collectionInfo?.single || false, + icon: collectionInfo?.icon || null, + translation: collectionInfo?.translation || null, + }; +}; diff --git a/src/services/items.ts b/src/services/items.ts index 79333bb4bb..6a74f448d8 100644 --- a/src/services/items.ts +++ b/src/services/items.ts @@ -65,12 +65,12 @@ export const readItems = async >( return records; }; -export const readItem = async (collection: string, pk: number | string, query: Query = {}) => { - const dbQuery = database.select('*').from(collection).where({ id: pk }); - - const records = await dbQuery; - - return records[0]; +export const readItem = async ( + collection: string, + pk: number | string, + query: Query = {} +): Promise => { + return await database.select('*').from(collection).where({ id: pk }).first(); }; export const updateItem = async (