Files
directus/api/src/controllers/relations.ts
Rijk van Zanten 2cdf18545e Remove support for CSV in PK param (#4514)
* Remove csv in pk param

Closes #3933

* Remove last uses of csv in route pk params in app
2021-03-12 18:06:35 -05:00

136 lines
2.9 KiB
TypeScript

import express from 'express';
import asyncHandler from '../utils/async-handler';
import { RelationsService, MetaService } from '../services';
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
import useCollection from '../middleware/use-collection';
import { respond } from '../middleware/respond';
import { PrimaryKey } from '../types';
const router = express.Router();
router.use(useCollection('directus_relations'));
router.post(
'/',
asyncHandler(async (req, res, next) => {
const service = new RelationsService({
accountability: req.accountability,
schema: req.schema,
});
const primaryKey = await service.create(req.body);
try {
const item = await service.readByKey(primaryKey, req.sanitizedQuery);
res.locals.payload = { data: item || null };
} catch (error) {
if (error instanceof ForbiddenException) {
return next();
}
throw error;
}
return next();
}),
respond
);
router.get(
'/',
asyncHandler(async (req, res, next) => {
const service = new RelationsService({
accountability: req.accountability,
schema: req.schema,
});
const metaService = new MetaService({
accountability: req.accountability,
schema: req.schema,
});
const records = await service.readByQuery(req.sanitizedQuery);
const meta = await metaService.getMetaForQuery(req.collection, req.sanitizedQuery);
res.locals.payload = { data: records || null, meta };
return next();
}),
respond
);
router.get(
'/:pk',
asyncHandler(async (req, res, next) => {
const service = new RelationsService({
accountability: req.accountability,
schema: req.schema,
});
const record = await service.readByKey(req.params.pk, req.sanitizedQuery);
res.locals.payload = { data: record || null };
return next();
}),
respond
);
router.patch(
'/:pk',
asyncHandler(async (req, res, next) => {
const service = new RelationsService({
accountability: req.accountability,
schema: req.schema,
});
const primaryKey = await service.update(req.body, req.params.pk);
try {
const item = await service.readByKey(primaryKey, req.sanitizedQuery);
res.locals.payload = { data: item || null };
} catch (error) {
if (error instanceof ForbiddenException) {
return next();
}
throw error;
}
return next();
}),
respond
);
router.delete(
'/',
asyncHandler(async (req, res, next) => {
if (!req.body || Array.isArray(req.body) === false) {
throw new InvalidPayloadException(`Body has to be an array of primary keys`);
}
const service = new RelationsService({
accountability: req.accountability,
schema: req.schema,
});
await service.delete(req.body as PrimaryKey[]);
return next();
}),
respond
);
router.delete(
'/:pk',
asyncHandler(async (req, res, next) => {
const service = new RelationsService({
accountability: req.accountability,
schema: req.schema,
});
await service.delete(req.params.pk);
return next();
}),
respond
);
export default router;