move entity_ref from request header to params

Signed-off-by: Lykke Axlin <lykkeaxlin@hotmail.com>

Co-authored-by: klaraab <klarabroman@live.se>
This commit is contained in:
Lykke Axlin
2021-10-12 15:34:07 +02:00
parent bc14419fab
commit 1fb1f8d758
4 changed files with 52 additions and 59 deletions
+23 -29
View File
@@ -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) {
+2 -2
View File
@@ -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",
+26 -27
View File
@@ -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<any> {
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<any> {
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<void> {
@@ -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<void> {
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<any> {
@@ -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,
},
});
}
}
@@ -183,7 +183,7 @@ export const EntityBazaarInfoCard = () => {
label: 'Community',
icon: <ChatIcon />,
href: bazaarProject?.value?.community,
disabled: bazaarProject?.value?.community === '',
disabled: !bazaarProject?.value?.community || !isMember,
},
];