From 1fb1f8d758137792015f00a59d6f1a972d26d660 Mon Sep 17 00:00:00 2001 From: Lykke Axlin Date: Tue, 12 Oct 2021 15:34:07 +0200 Subject: [PATCH] move entity_ref from request header to params Signed-off-by: Lykke Axlin Co-authored-by: klaraab --- plugins/bazaar-backend/src/service/router.ts | 52 ++++++++---------- plugins/bazaar/package.json | 4 +- plugins/bazaar/src/api.ts | 53 +++++++++---------- .../EntityBazaarInfoCard.tsx | 2 +- 4 files changed, 52 insertions(+), 59 deletions(-) diff --git a/plugins/bazaar-backend/src/service/router.ts b/plugins/bazaar-backend/src/service/router.ts index dd6fd94b4e..8d4866408a 100644 --- a/plugins/bazaar-backend/src/service/router.ts +++ b/plugins/bazaar-backend/src/service/router.ts @@ -40,10 +40,9 @@ export async function createRouter( const router = Router(); router.use(express.json()); - router.get('/members', async (request, response) => { - const entityRef = request.headers.entity_ref; - - const data = await dbHandler.getMembers(entityRef); + router.get('/members/:ref', async (request, response) => { + const entity_ref = decodeURIComponent(request.params.ref); + const data = await dbHandler.getMembers(entity_ref); if (data?.length) { response.json({ status: 'ok', data: data }); @@ -53,21 +52,18 @@ export async function createRouter( }); router.put('/member', async (request, response) => { - const userId = request.headers.user_id; - const entityRef = request.headers.entity_ref; - - await dbHandler.addMember(userId, entityRef); + const { user_id, entity_ref } = request.body; + await dbHandler.addMember(user_id, entity_ref); response.json({ status: 'ok' }); }); - router.delete('/member', async (request, response) => { - const userId = request.headers.user_id; - const entityRef = request.headers.entity_ref; + router.delete('/member/:ref/:id', async (request, response) => { + const { ref, id } = request.params; const count = await db('public.members') - .where({ entity_ref: entityRef }) - .andWhere('user_id', userId) + .where({ entity_ref: decodeURIComponent(ref) }) + .andWhere('user_id', id) .del(); if (count) { @@ -77,12 +73,9 @@ export async function createRouter( } }); - router.delete('/members', async (request, response) => { - const entityRef = request.headers.entity_ref; - - const count = await db('public.members') - .where({ entity_ref: entityRef }) - .del(); + router.delete('/members/:ref', async (request, response) => { + const ref = decodeURIComponent(request.params.ref); + const count = await db('public.members').where({ entity_ref: ref }).del(); if (count) { response.json({ status: 'ok' }); @@ -91,8 +84,8 @@ export async function createRouter( } }); - router.get('/metadata', async (request, response) => { - const entityRef = request.headers.entity_ref; + router.get('/metadata/:ref', async (request, response) => { + const entity_ref = decodeURIComponent(request.params.ref); const coalesce = db.raw( 'coalesce(count(members.entity_ref), 0) as members_count', @@ -105,11 +98,12 @@ export async function createRouter( 'metadata.announcement', 'metadata.status', 'metadata.updated_at', + 'metadata.community', ]; const data = await db('public.members as members') .select([...columns, coalesce]) - .where({ 'metadata.entity_ref': entityRef }) + .where({ 'metadata.entity_ref': entity_ref }) .groupBy(columns) .rightJoin( 'public.metadata as metadata', @@ -133,6 +127,7 @@ export async function createRouter( 'metadata.announcement', 'metadata.status', 'metadata.updated_at', + 'metadata.community', ]; const data = await db('public.members as members') @@ -149,11 +144,10 @@ export async function createRouter( }); router.put('/metadata', async (request, response) => { - const entityRef = request.headers.entity_ref; - const { name, announcement, status, community } = request.body; + const { name, announcement, status, community, entity_ref } = request.body; const count = await db('public.metadata') - .where({ entity_ref: entityRef }) + .where({ entity_ref: entity_ref }) .update({ announcement: announcement, community: community, @@ -166,7 +160,7 @@ export async function createRouter( await db .insert({ name: name, - entity_ref: entityRef, + entity_ref: entity_ref, community: community, announcement: announcement, status: status, @@ -176,11 +170,11 @@ export async function createRouter( } }); - router.delete('/metadata', async (request, response) => { - const entityRef = request.headers.entity_ref; + router.delete('/metadata/:ref', async (request, response) => { + const entity_ref = decodeURIComponent(request.params.ref); const count = await db('public.metadata') - .where({ entity_ref: entityRef }) + .where({ entity_ref: entity_ref }) .del(); if (count) { diff --git a/plugins/bazaar/package.json b/plugins/bazaar/package.json index 8789ed4acf..df5afc6025 100644 --- a/plugins/bazaar/package.json +++ b/plugins/bazaar/package.json @@ -23,9 +23,9 @@ "dependencies": { "@backstage/catalog-model": "^0.9.0", "@backstage/cli": "^0.7.2", - "@backstage/core-components": "^0.5.0", + "@backstage/core-components": "^0.6.1", "@backstage/core-plugin-api": "^0.1.3", - "@backstage/plugin-catalog": "^0.6.6", + "@backstage/plugin-catalog": "^0.7.0", "@backstage/plugin-catalog-react": "^0.5.0", "@material-ui/core": "^4.11.0", "@material-ui/icons": "^4.9.1", diff --git a/plugins/bazaar/src/api.ts b/plugins/bazaar/src/api.ts index 5f0a753aac..e825d8d46a 100644 --- a/plugins/bazaar/src/api.ts +++ b/plugins/bazaar/src/api.ts @@ -73,11 +73,11 @@ export class BazaarClient implements BazaarApi { return await fetch(`${baseUrl}/metadata`, { method: 'PUT', headers: { - entity_ref: stringifyEntityRef(entity), Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ + entity_ref: stringifyEntityRef(entity), name: name, announcement: announcement, community: community, @@ -89,12 +89,12 @@ export class BazaarClient implements BazaarApi { async getMetadata(entity: Entity): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); - const response = await fetch(`${baseUrl}/metadata`, { - method: 'GET', - headers: { - entity_ref: stringifyEntityRef(entity), + const response = await fetch( + `${baseUrl}/metadata/${encodeURIComponent(stringifyEntityRef(entity))}`, + { + method: 'GET', }, - }); + ); return response.ok ? response : null; } @@ -102,12 +102,12 @@ export class BazaarClient implements BazaarApi { async getMembers(entity: Entity): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); - return await fetch(`${baseUrl}/members`, { - method: 'GET', - headers: { - entity_ref: stringifyEntityRef(entity), + return await fetch( + `${baseUrl}/members/${encodeURIComponent(stringifyEntityRef(entity))}`, + { + method: 'GET', }, - }).then(resp => resp.json()); + ).then(resp => resp.json()); } async addMember(entity: Entity): Promise { @@ -116,22 +116,27 @@ export class BazaarClient implements BazaarApi { await fetch(`${baseUrl}/member`, { method: 'PUT', headers: { - user_id: this.identityApi.getUserId(), - entity_ref: stringifyEntityRef(entity), + Accept: 'application/json', + 'Content-Type': 'application/json', }, + body: JSON.stringify({ + entity_ref: stringifyEntityRef(entity), + user_id: this.identityApi.getUserId(), + }), }); } async deleteMember(entity: Entity): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); - await fetch(`${baseUrl}/member`, { - method: 'DELETE', - headers: { - user_id: this.identityApi.getUserId(), - entity_ref: stringifyEntityRef(entity), + await fetch( + `${baseUrl}/member/${encodeURIComponent( + stringifyEntityRef(entity), + )}/${this.identityApi.getUserId()}`, + { + method: 'DELETE', }, - }); + ); } async getEntities(): Promise { @@ -146,19 +151,13 @@ export class BazaarClient implements BazaarApi { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); const entityRef = bazaarProject.entityRef as string; - await fetch(`${baseUrl}/metadata`, { + await fetch(`${baseUrl}/metadata/${encodeURIComponent(entityRef)}`, { method: 'DELETE', - headers: { - entity_ref: entityRef, - }, }); if (bazaarProject.membersCount > 0) { - await fetch(`${baseUrl}/members`, { + await fetch(`${baseUrl}/members/${encodeURIComponent(entityRef)}`, { method: 'DELETE', - headers: { - entity_ref: entityRef, - }, }); } } diff --git a/plugins/bazaar/src/components/EntityBazaarInfoCard/EntityBazaarInfoCard.tsx b/plugins/bazaar/src/components/EntityBazaarInfoCard/EntityBazaarInfoCard.tsx index c74f186488..24eb064951 100644 --- a/plugins/bazaar/src/components/EntityBazaarInfoCard/EntityBazaarInfoCard.tsx +++ b/plugins/bazaar/src/components/EntityBazaarInfoCard/EntityBazaarInfoCard.tsx @@ -183,7 +183,7 @@ export const EntityBazaarInfoCard = () => { label: 'Community', icon: , href: bazaarProject?.value?.community, - disabled: bazaarProject?.value?.community === '', + disabled: !bazaarProject?.value?.community || !isMember, }, ];