diff --git a/src/controllers/collections/create.ts b/src/controllers/collections/create.ts new file mode 100644 index 0000000..e8ccaf2 --- /dev/null +++ b/src/controllers/collections/create.ts @@ -0,0 +1,71 @@ +import {Request, Response} from '@gravity-ui/expresskit'; + +import {prepareResponseAsync} from '../../components/response-presenter'; +import {makeSchemaValidator} from '../../components/validation-schema-compiler'; +import {createCollection} from '../../services/new/collection'; +import {formatCollectionWithOperation} from '../../services/new/collection/formatters'; + +type CreateCollectionReqBody = { + title: string; + description?: string; + parentId: Nullable; +}; + +const validateBody = makeSchemaValidator({ + type: 'object', + required: ['title', 'parentId'], + properties: { + title: { + type: 'string', + }, + description: { + type: 'string', + }, + parentId: { + type: ['string', 'null'], + }, + }, +}); + +export const createCollectionController = async ( + req: Request<{}, {}, CreateCollectionReqBody>, + res: Response, +) => { + const {body} = req; + + validateBody(body); + + const registry = req.ctx.get('registry'); + const { + controllersCallbacks: {onCreateCollectionError, onCreateCollectionSuccess}, + } = registry.common.functions.get(); + + let result; + + try { + result = await createCollection( + {ctx: req.ctx}, + { + title: body.title, + description: body.description, + parentId: body.parentId, + }, + ); + + onCreateCollectionSuccess({ + ctx: req.ctx, + reqBody: body, + collection: result.collection.model, + }); + } catch (error) { + onCreateCollectionError({ctx: req.ctx, reqBody: body, error}); + + throw error; + } + + const formattedResponse = formatCollectionWithOperation(result.collection, result.operation); + + const {code, response} = await prepareResponseAsync({data: formattedResponse}); + + res.status(code).send(response); +}; diff --git a/src/controllers/collections.ts b/src/controllers/collections/index.ts similarity index 81% rename from src/controllers/collections.ts rename to src/controllers/collections/index.ts index a8a5b19..94bc877 100644 --- a/src/controllers/collections.ts +++ b/src/controllers/collections/index.ts @@ -1,11 +1,10 @@ import {Request, Response} from '@gravity-ui/expresskit'; -import {prepareResponseAsync} from '../components/response-presenter'; +import {prepareResponseAsync} from '../../components/response-presenter'; import { Mode, OrderDirection, OrderField, - createCollection, deleteCollections, getCollection, getCollectionBreadcrumbs, @@ -15,64 +14,20 @@ import { moveCollection, moveCollectionsList, updateCollection, -} from '../services/new/collection'; +} from '../../services/new/collection'; import { formatCollection, formatCollectionContent, formatCollectionModel, formatCollectionModelsList, - formatCollectionWithOperation, formatGetCollectionBreadcrumbs, -} from '../services/new/collection/formatters'; -import Utils from '../utils'; +} from '../../services/new/collection/formatters'; +import Utils from '../../utils'; -export type CreateCollectionReqBody = { - title: string; - description?: string; - parentId: Nullable; -}; +import {createCollectionController} from './create'; export default { - create: async (req: Request<{}, {}, CreateCollectionReqBody>, res: Response) => { - const {body} = req; - - const registry = req.ctx.get('registry'); - const { - controllersCallbacks: {onCreateCollectionError, onCreateCollectionSuccess}, - } = registry.common.functions.get(); - - let result; - - try { - result = await createCollection( - {ctx: req.ctx}, - { - title: body.title, - description: body.description, - parentId: body.parentId, - }, - ); - - onCreateCollectionSuccess({ - ctx: req.ctx, - reqBody: body, - collection: result.collection.model, - }); - } catch (error) { - onCreateCollectionError({ctx: req.ctx, reqBody: body, error}); - - throw error; - } - - const formattedResponse = formatCollectionWithOperation( - result.collection, - result.operation, - ); - - const {code, response} = await prepareResponseAsync({data: formattedResponse}); - - res.status(code).send(response); - }, + create: createCollectionController, get: async (req: Request, res: Response) => { const {params, query} = req; diff --git a/src/services/new/collection/create-collection.ts b/src/services/new/collection/create-collection.ts index 8e44777..45ad9a9 100644 --- a/src/services/new/collection/create-collection.ts +++ b/src/services/new/collection/create-collection.ts @@ -1,7 +1,6 @@ import {AppError} from '@gravity-ui/nodekit'; import {transaction} from 'objection'; -import {makeSchemaValidator} from '../../../components/validation-schema-compiler'; import {US_ERRORS} from '../../../const'; import {CollectionModel, CollectionModelColumn} from '../../../db/models/new/collection'; import Utils from '../../../utils'; @@ -11,22 +10,6 @@ import {getPrimary} from '../utils'; import {checkCollectionByTitle} from './check-collection-by-title'; import {getParentIds} from './utils/get-parents'; -const validateArgs = makeSchemaValidator({ - type: 'object', - required: ['title', 'parentId'], - properties: { - title: { - type: 'string', - }, - description: { - type: 'string', - }, - parentId: { - type: ['string', 'null'], - }, - }, -}); - export interface CreateCollectionArgs { title: string; description?: string; @@ -34,7 +17,7 @@ export interface CreateCollectionArgs { } export const createCollection = async ( - {ctx, trx, skipValidation = false, skipCheckPermissions = false}: ServiceArgs, + {ctx, trx, skipCheckPermissions = false}: ServiceArgs, args: CreateCollectionArgs, ) => { const {title, description, parentId} = args; @@ -47,10 +30,6 @@ export const createCollection = async ( parentId: parentId ? Utils.encodeId(parentId) : null, }); - if (!skipValidation) { - validateArgs(args); - } - const { user: {userId}, tenantId,