Skip to content

Commit

Permalink
Add controllers callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
stepanenkoxx committed Dec 19, 2024
1 parent 83beaca commit a2ddeba
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 172 deletions.
4 changes: 4 additions & 0 deletions api/registry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ export type {
OnMoveCollectionsListSuccess,
OnUpdateCollectionError,
OnUpdateCollectionSuccess,
OnDeleteCollectionError,
OnDeleteCollectionSuccess,
OnDeleteCollectionsListError,
OnDeleteCollectionsListSuccess,
} from '../../src/registry/common/utils/controllers-callbacks/collections';
export type {GatewayApi} from '../../src/registry';
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;
}
};
59 changes: 59 additions & 0 deletions src/controllers/collections/deleteList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 DeleteCollectionsListReqBody = {
collectionIds: string[];
};

const validateBody = makeSchemaValidator({
type: 'object',
required: ['collectionIds'],
properties: {
collectionIds: {
type: 'array',
items: {type: 'string'},
},
},
});

export const deleteListController = async (req: Request, res: Response) => {
const body = validateBody<DeleteCollectionsListReqBody>(req.body);

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

try {
const result = await deleteCollections(
{ctx: req.ctx},
{
collectionIds: body.collectionIds,
},
);

onDeleteCollectionsListSuccess({
ctx: req.ctx,
reqBody: body,
collections: result.collections,
});

const formattedResponse = formatCollectionModelsList(result);

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

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

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

import {prepareResponseAsync} from '../../components/response-presenter';
import {getCollection} from '../../services/new/collection';
import {formatCollection} from '../../services/new/collection/formatters';
import Utils from '../../utils';

export const getCollectionController = async (req: Request, res: Response) => {
const {params, query} = req;

const result = await getCollection(
{ctx: req.ctx},
{
collectionId: params.collectionId,
includePermissionsInfo: Utils.isTrueArg(query.includePermissionsInfo),
},
);

const formattedResponse = formatCollection(result);

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

res.status(code).send(response);
};
24 changes: 24 additions & 0 deletions src/controllers/collections/getBreadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {Request, Response} from '@gravity-ui/expresskit';

import {prepareResponseAsync} from '../../components/response-presenter';
import {getCollectionBreadcrumbs} from '../../services/new/collection';
import {formatGetCollectionBreadcrumbs} from '../../services/new/collection/formatters';
import Utils from '../../utils';

export const getCollectionBreadcrumbsController = async (req: Request, res: Response) => {
const {params, query} = req;

const result = await getCollectionBreadcrumbs(
{ctx: req.ctx},
{
collectionId: params.collectionId,
includePermissionsInfo: Utils.isTrueArg(query.includePermissionsInfo),
},
);

const formattedResponse = formatGetCollectionBreadcrumbs(result);

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

res.status(code).send(response);
};
20 changes: 20 additions & 0 deletions src/controllers/collections/getCollectionsListByIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {Request, Response} from '@gravity-ui/expresskit';

import {prepareResponseAsync} from '../../components/response-presenter';
import {getCollectionsListByIds} from '../../services/new/collection';
import {formatCollectionModel} from '../../services/new/collection/formatters';

export const getCollectionsListByIdsController = async (req: Request, res: Response) => {
const {body} = req;

const result = await getCollectionsListByIds(
{ctx: req.ctx},
{
collectionIds: body.collectionIds,
},
);

const formattedResponse = result.map((instance) => formatCollectionModel(instance.model));
const {code, response} = await prepareResponseAsync({data: formattedResponse});
res.status(code).send(response);
};
60 changes: 60 additions & 0 deletions src/controllers/collections/getContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {Request, Response} from '@gravity-ui/expresskit';

import {prepareResponseAsync} from '../../components/response-presenter';
import {
Mode,
OrderDirection,
OrderField,
getCollectionContent,
} from '../../services/new/collection';
import {formatCollectionContent} from '../../services/new/collection/formatters';
import Utils from '../../utils';

/**
* @deprecated for structureItemsController.getStructureItems,
* @todo remove, after successful deploy with UI.
* Exists for reverse compatibility.
*/
export const getCollectionContentController = async (req: Request, res: Response) => {
const {query} = req;

let collectionsPage: Optional<Nullable<number>>;
if (query.collectionsPage === 'null') {
collectionsPage = null;
} else {
collectionsPage = query.collectionsPage
? parseInt(query.collectionsPage as string, 10)
: undefined;
}

let workbooksPage: Optional<Nullable<number>>;
if (query.workbooksPage === 'null') {
workbooksPage = null;
} else {
workbooksPage = query.workbooksPage
? parseInt(query.workbooksPage as string, 10)
: undefined;
}

const result = await getCollectionContent(
{ctx: req.ctx},
{
collectionId: (query.collectionId as Optional<string>) ?? null,
includePermissionsInfo: Utils.isTrueArg(query.includePermissionsInfo),
filterString: query.filterString as Optional<string>,
collectionsPage,
workbooksPage,
pageSize: query.pageSize ? parseInt(query.pageSize as string, 10) : undefined,
orderField: query.orderField as Optional<OrderField>,
orderDirection: query.orderDirection as Optional<OrderDirection>,
onlyMy: Utils.isTrueArg(query.onlyMy),
mode: query.mode as Optional<Mode>,
},
);

const formattedResponse = formatCollectionContent(result);

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

res.status(code).send(response);
};
12 changes: 12 additions & 0 deletions src/controllers/collections/getRootPermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Request, Response} from '@gravity-ui/expresskit';

import {prepareResponseAsync} from '../../components/response-presenter';
import {getRootCollectionPermissions} from '../../services/new/collection';

export const getRootCollectionPermissionsController = async (req: Request, res: Response) => {
const result = await getRootCollectionPermissions({ctx: req.ctx});

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

res.status(code).send(response);
};
Loading

0 comments on commit a2ddeba

Please sign in to comment.