Skip to content

Commit

Permalink
move create collection validatation to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
stepanenkoxx committed Dec 17, 2024
1 parent 074024f commit e8ea9ab
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 73 deletions.
71 changes: 71 additions & 0 deletions src/controllers/collections/create.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
};

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);
};
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<string>;
};
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;
Expand Down
23 changes: 1 addition & 22 deletions src/services/new/collection/create-collection.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,30 +10,14 @@ 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;
parentId: Nullable<string>;
}

export const createCollection = async (
{ctx, trx, skipValidation = false, skipCheckPermissions = false}: ServiceArgs,
{ctx, trx, skipCheckPermissions = false}: ServiceArgs,
args: CreateCollectionArgs,
) => {
const {title, description, parentId} = args;
Expand All @@ -47,10 +30,6 @@ export const createCollection = async (
parentId: parentId ? Utils.encodeId(parentId) : null,
});

if (!skipValidation) {
validateArgs(args);
}

const {
user: {userId},
tenantId,
Expand Down

0 comments on commit e8ea9ab

Please sign in to comment.