Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add controllers callbacks #221

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions api/registry/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
export type {DLSConstructor} from '../../src/registry/common/components/dls/types';
export type {
OnCreateCollectionError,
OnCreateCollectionSuccess,
OnMoveCollectionError,
OnMoveCollectionSuccess,
OnMoveCollectionsListError,
OnMoveCollectionsListSuccess,
OnUpdateCollectionError,
OnUpdateCollectionSuccess,
OnDeleteCollectionError,
OnDeleteCollectionSuccess,
OnDeleteCollectionsListError,
OnDeleteCollectionsListSuccess,
} from '../../src/registry/common/utils/controllers-callbacks/collections';
export type {GatewayApi} from '../../src/registry';
1 change: 0 additions & 1 deletion src/components/validation-schema-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import US_ERRORS from '../const/us-error-constants';

const ajv = new Ajv({
allErrors: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this issue of "Code scanning results / CodeQL" check

Screenshot 2024-12-23 at 14 53 02

I guess we don't need to check all errors

verbose: true,
});

Expand Down
248 changes: 0 additions & 248 deletions src/controllers/collections.ts

This file was deleted.

67 changes: 67 additions & 0 deletions src/controllers/collections/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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';

export 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, res: Response) => {
const body = validateBody<CreateCollectionReqBody>(req.body);

const registry = req.ctx.get('registry');
const {
controllersCallbacks: {onCreateCollectionError, onCreateCollectionSuccess},
} = registry.common.functions.get();

try {
const 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,
});

const formattedResponse = formatCollectionWithOperation(
result.collection,
result.operation,
);

const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
} catch (error) {
onCreateCollectionError({ctx: req.ctx, reqBody: body, error});

throw error;
}
};
54 changes: 54 additions & 0 deletions src/controllers/collections/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {Request, Response} from 'express';

import {prepareResponseAsync} from '../../components/response-presenter';
import {makeSchemaValidator} from '../../components/validation-schema-compiler';
import {deleteCollections} from '../../services/new/collection';
import {formatCollectionModelsList} from '../../services/new/collection/formatters';

export type DeleteCollectionReqParams = {
collectionId: string;
};

const validateParams = makeSchemaValidator({
type: 'object',
required: ['collectionId'],
properties: {
collectionId: {
type: 'string',
},
},
});

export const deleteCollectionController = async (req: Request, res: Response) => {
const params = validateParams(req.params) as DeleteCollectionReqParams;

const registry = req.ctx.get('registry');
const {
controllersCallbacks: {onDeleteCollectionError, onDeleteCollectionSuccess},
} = registry.common.functions.get();

try {
const result = await deleteCollections(
{ctx: req.ctx},
{
collectionIds: [params.collectionId],
},
);

onDeleteCollectionSuccess({
ctx: req.ctx,
reqParams: params,
collections: result.collections,
});

const formattedResponse = formatCollectionModelsList(result);

const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
} catch (error) {
onDeleteCollectionError({ctx: req.ctx, reqParams: params, error});

throw error;
}
};
Loading
Loading