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

Renaming in favorites #35

Merged
merged 6 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions src/const/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const RETURN_NAVIGATION_COLUMNS = [
];

export const RETURN_FAVORITES_COLUMNS = [
'favorites.alias',
'entries.entryId',
'entries.scope',
'entries.type',
Expand Down
13 changes: 13 additions & 0 deletions src/controllers/favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ export default {

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

res.status(code).send(response);
},
renameFavorite: async (req: Request, res: Response) => {
const {params, body} = req;

const result = await FavoriteService.rename({
entryId: params.entryId,
name: body.name,
ctx: req.ctx,
});

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

res.status(code).send(response);
},
};
36 changes: 35 additions & 1 deletion src/db/models/favorite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {AppError} from '@gravity-ui/nodekit';
import * as MT from '../../../types/models';
import {DlsActions} from '../../../types/models';
import Utils from '../../../utils';
import {validateGetFavorites, validateAddFavorite, validateDeleteFavorite} from './scheme';
import {
validateGetFavorites,
validateAddFavorite,
validateDeleteFavorite,
validateRenameFavorite,
} from './scheme';
import {RETURN_FAVORITES_COLUMNS} from '../../../const';
import {registry} from '../../../registry';

Expand Down Expand Up @@ -362,6 +367,35 @@ class Favorite extends Model {

return result;
}

static async rename({
tenantId,
entryId,
name,
requestedBy,
ctx,
dlContext,
}: MT.RenameFavoriteConfig) {
ctx.log('RENAME_FAVORITE_REQUEST', {
tenantId,
entryId,
name,
requestedBy,
dlContext,
});

validateRenameFavorite({entryId, name});

const result = await Favorite.query(this.primary)
.where({entryId})
.update({alias: name})
.returning('*')
.timeout(Model.DEFAULT_QUERY_TIMEOUT);

ctx.log('RENAME_FAVORITE_SUCCESS');

return result;
}
}

export default Favorite;
15 changes: 14 additions & 1 deletion src/db/models/favorite/scheme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import compileSchema from '../../../components/validation-schema-compiler';
import compileSchema, {makeSchemaValidator} from '../../../components/validation-schema-compiler';
import {AJV_PATTERN_KEYS_NOT_OBJECT} from '../../../const';

export const validateGetFavorites = compileSchema({
Expand Down Expand Up @@ -76,3 +76,16 @@ export const validateDeleteFavorite = compileSchema({
},
},
});
export const validateRenameFavorite = makeSchemaValidator({
type: 'object',
required: ['entryId', 'name'],
properties: {
entryId: {
type: 'string',
},
name: {
type: ['string', 'null'],
verifyEntryName: true,
},
},
});
5 changes: 5 additions & 0 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ export function getRoutes(nodekit: NodeKit, options: GetRoutesOptions) {
handler: favoritesController.deleteFavorite,
write: true,
}),
renameFavorite: makeRoute({
route: 'POST /v1/favorites/:entryId/rename',
handler: favoritesController.renameFavorite,
write: true,
}),
};

if (isEnabledFeature(ctx, Feature.CollectionsEnabled)) {
Expand Down
14 changes: 14 additions & 0 deletions src/services/favorite.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ export default class FavoriteService {
ctx,
});
}

static async rename({entryId, name, ctx}: ST.RenameFavorite) {
const {requestId, tenantId, user, dlContext} = ctx.get('info');

return await Favorite.rename({
requestId,
tenantId,
entryId,
name,
requestedBy: user,
dlContext,
ctx,
});
}
}
6 changes: 6 additions & 0 deletions src/types/models/favorite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ export interface DeleteFavoriteConfig extends BasicRequestParams {
entryId?: any;
ctx: CTX;
}
export interface RenameFavoriteConfig extends BasicRequestParams {
entryId: string;
name: string;
ctx: CTX;
dlContext?: string;
}
1 change: 1 addition & 0 deletions src/types/models/tables-columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ export interface FavoriteColumns {
entryId: string;
tenantId: string;
login: string;
alias: string | null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add alias to the new Favorite model plz – src/db/models/new/favorite/index.ts
It is not used now, but it would be better to keep it consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, added

createdAt: string;
}
4 changes: 4 additions & 0 deletions src/types/services.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export interface GetFavorite extends StdServiceParams {
export interface AddFavorite extends StdServiceParams {
entryId: string;
}
export interface RenameFavorite extends StdServiceParams {
entryId: string;
name: string;
}
export interface DeleteFavorite extends StdServiceParams {
entryId: string;
}
Expand Down
Loading