Files
directus/api/src/controllers/collections.ts
Rijk van Zanten dd551f3571 Add non-items system Resolvers (#4863)
* Add auth resolvers

* Add password request/reset

* Add up until file import

* Make revisions read only

* Add server resolvers

* Add utils

* Add schema resolvers for schema manipulation
2021-04-06 18:04:35 -04:00

103 lines
2.4 KiB
TypeScript

import { Router } from 'express';
import asyncHandler from '../utils/async-handler';
import { CollectionsService, MetaService } from '../services';
import { ForbiddenException } from '../exceptions';
import { respond } from '../middleware/respond';
const router = Router();
router.post(
'/',
asyncHandler(async (req, res, next) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
});
const collectionKey = await collectionsService.create(req.body);
const record = await collectionsService.readByKey(collectionKey);
res.locals.payload = { data: record || null };
return next();
}),
respond
);
router.get(
'/',
asyncHandler(async (req, res, next) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
});
const metaService = new MetaService({
accountability: req.accountability,
schema: req.schema,
});
const collections = await collectionsService.readByQuery();
const meta = await metaService.getMetaForQuery('directus_collections', {});
res.locals.payload = { data: collections || null, meta };
return next();
}),
respond
);
router.get(
'/:collection',
asyncHandler(async (req, res, next) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
});
const collection = await collectionsService.readByKey(req.params.collection);
res.locals.payload = { data: collection || null };
return next();
}),
respond
);
router.patch(
'/:collection',
asyncHandler(async (req, res, next) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
});
await collectionsService.update(req.body, req.params.collection);
try {
const collection = await collectionsService.readByKey(req.params.collection);
res.locals.payload = { data: collection || null };
} catch (error) {
if (error instanceof ForbiddenException) {
return next();
}
throw error;
}
return next();
}),
respond
);
router.delete(
'/:collection',
asyncHandler(async (req, res, next) => {
const collectionsService = new CollectionsService({
accountability: req.accountability,
schema: req.schema,
});
await collectionsService.delete(req.params.collection);
return next();
}),
respond
);
export default router;