From aab4cb92b8daca80284dfcccacadd5c7850fb0d0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 3 Apr 2022 11:48:25 -0500 Subject: [PATCH 1/6] content_cid not id is used from nft data upload not uploads --- packages/api/src/routes/nfts-upload.js | 11 ++++++++--- packages/api/src/utils/db-client-types.ts | 2 +- packages/api/src/utils/db-client.js | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/api/src/routes/nfts-upload.js b/packages/api/src/routes/nfts-upload.js index e2e09b6fd9..b30353be26 100644 --- a/packages/api/src/routes/nfts-upload.js +++ b/packages/api/src/routes/nfts-upload.js @@ -172,15 +172,20 @@ export async function nftUpdateUpload(event, ctx) { const { params, db } = ctx try { const { user } = await validate(event, ctx) - const id = params.id + const content_cid = params.id // id is required for updating - if (!id) return new JSONResponse({ ok: false, value: 'ID is required' }) + if (!content_cid) + return new JSONResponse({ ok: false, value: 'ID is required' }) const body = await event.request.json() const { name } = body - const updatedRecord = await db.updateUpload({ id, name, user_id: user.id }) + const updatedRecord = await db.updateUpload({ + content_cid, + name, + user_id: user.id, + }) return new JSONResponse({ ok: true, value: updatedRecord }) } catch (/** @type {any} */ err) { diff --git a/packages/api/src/utils/db-client-types.ts b/packages/api/src/utils/db-client-types.ts index 1875356a33..0900fd733c 100644 --- a/packages/api/src/utils/db-client-types.ts +++ b/packages/api/src/utils/db-client-types.ts @@ -59,7 +59,7 @@ export interface CreateUploadInput { } export interface UpdateUploadInput { - id: string + content_cid: string name?: string user_id: number } diff --git a/packages/api/src/utils/db-client.js b/packages/api/src/utils/db-client.js index 8f2606a94f..7a200d9278 100644 --- a/packages/api/src/utils/db-client.js +++ b/packages/api/src/utils/db-client.js @@ -125,13 +125,13 @@ export class DBClient { updated_at: now, }) .select(this.uploadQuery) - .eq('id', data.id) + .eq('content_cid', data.content_cid) .eq('user_id', data.user_id) .is('deleted_at', null) .single() if (status === 406) { - throw new Error(`Status 406, cannot update ${data.id}`) + throw new Error(`Status 406, cannot update ${data.content_cid}`) } if (!upload) { From 8aeda4fa53a4b365def7affa008513b1dcbdc600 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 3 Apr 2022 11:52:40 -0500 Subject: [PATCH 2/6] missed an updated key --- packages/api/src/utils/db-client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/api/src/utils/db-client.js b/packages/api/src/utils/db-client.js index 7a200d9278..67652e1a0a 100644 --- a/packages/api/src/utils/db-client.js +++ b/packages/api/src/utils/db-client.js @@ -136,9 +136,9 @@ export class DBClient { if (!upload) { throw new Error( - `Cannot update upload ${JSON.stringify(data.id)} ${JSON.stringify( - status - )}` + `Cannot update upload ${JSON.stringify( + data.content_cid + )} ${JSON.stringify(status)}` ) } From 971021c6b905a451490e7a9481b453f8eee20d54 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 3 Apr 2022 12:08:10 -0500 Subject: [PATCH 3/6] Update api test to use content_cid not id --- packages/api/test/nfts-upload.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api/test/nfts-upload.spec.js b/packages/api/test/nfts-upload.spec.js index cc6d5b7906..8118319670 100644 --- a/packages/api/test/nfts-upload.spec.js +++ b/packages/api/test/nfts-upload.spec.js @@ -371,14 +371,14 @@ describe('NFT Upload ', () => { const { data } = await rawClient .from('upload') .select('*') - .match({ source_cid: cid, user_id: client.userId }) + .match({ content_cid: cid, user_id: client.userId }) .single() // update file we just created above const name = 'test updated name' - const uploadRes = await fetch(`upload/${data.id}`, { + const uploadRes = await fetch(`upload/${data.content_cid}`, { method: 'PATCH', headers: { Authorization: `Bearer ${client.token}`, From 3e80e6bf79a9f60c806681d0bbc2de1a1a29b90a Mon Sep 17 00:00:00 2001 From: Daniel Ashcraft Date: Mon, 4 Apr 2022 11:01:41 -0500 Subject: [PATCH 4/6] Update packages/api/src/routes/nfts-upload.js Co-authored-by: Alan Shaw --- packages/api/src/routes/nfts-upload.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api/src/routes/nfts-upload.js b/packages/api/src/routes/nfts-upload.js index b30353be26..868a6aa5db 100644 --- a/packages/api/src/routes/nfts-upload.js +++ b/packages/api/src/routes/nfts-upload.js @@ -176,7 +176,7 @@ export async function nftUpdateUpload(event, ctx) { // id is required for updating if (!content_cid) - return new JSONResponse({ ok: false, value: 'ID is required' }) + return new JSONResponse({ ok: false, value: 'upload CID is required' }) const body = await event.request.json() const { name } = body From aa205eb05e7bcc278e921784f7222bbd68cddc6e Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Apr 2022 11:01:53 -0500 Subject: [PATCH 5/6] Updated per alan's feedback --- packages/api/src/index.js | 2 +- packages/api/src/routes/nfts-upload.js | 6 +++--- packages/api/src/utils/db-client-types.ts | 2 +- packages/api/src/utils/db-client.js | 10 +++------- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/packages/api/src/index.js b/packages/api/src/index.js index 30b72faf1f..6c074eb132 100644 --- a/packages/api/src/index.js +++ b/packages/api/src/index.js @@ -98,7 +98,7 @@ r.add('get', '/check/:cid', withMode(nftCheck, RO), [postCors]) r.add('get', '', withMode(nftList, RO), [postCors]) r.add('get', '/:cid', withMode(nftGet, RO), [postCors]) r.add('post', '/upload', withMode(nftUpload, RW), [postCors]) -r.add('patch', '/upload/:id', withMode(nftUpdateUpload, RW), [postCors]) +r.add('patch', '/upload/:cid', withMode(nftUpdateUpload, RW), [postCors]) r.add('post', '/store', withMode(nftStore, RW), [postCors]) r.add('delete', '/:cid', withMode(nftDelete, RW), [postCors]) diff --git a/packages/api/src/routes/nfts-upload.js b/packages/api/src/routes/nfts-upload.js index 868a6aa5db..ca7953f5f0 100644 --- a/packages/api/src/routes/nfts-upload.js +++ b/packages/api/src/routes/nfts-upload.js @@ -172,17 +172,17 @@ export async function nftUpdateUpload(event, ctx) { const { params, db } = ctx try { const { user } = await validate(event, ctx) - const content_cid = params.id + const { cid } = params // id is required for updating - if (!content_cid) + if (!cid) return new JSONResponse({ ok: false, value: 'upload CID is required' }) const body = await event.request.json() const { name } = body const updatedRecord = await db.updateUpload({ - content_cid, + cid, name, user_id: user.id, }) diff --git a/packages/api/src/utils/db-client-types.ts b/packages/api/src/utils/db-client-types.ts index 0900fd733c..daf0ae4e63 100644 --- a/packages/api/src/utils/db-client-types.ts +++ b/packages/api/src/utils/db-client-types.ts @@ -59,7 +59,7 @@ export interface CreateUploadInput { } export interface UpdateUploadInput { - content_cid: string + cid: string name?: string user_id: number } diff --git a/packages/api/src/utils/db-client.js b/packages/api/src/utils/db-client.js index 67652e1a0a..9df8e72f78 100644 --- a/packages/api/src/utils/db-client.js +++ b/packages/api/src/utils/db-client.js @@ -125,21 +125,17 @@ export class DBClient { updated_at: now, }) .select(this.uploadQuery) - .eq('content_cid', data.content_cid) + .eq('source_cid', data.cid) .eq('user_id', data.user_id) .is('deleted_at', null) .single() if (status === 406) { - throw new Error(`Status 406, cannot update ${data.content_cid}`) + throw new Error(`Status 406, cannot update ${data.cid}`) } if (!upload) { - throw new Error( - `Cannot update upload ${JSON.stringify( - data.content_cid - )} ${JSON.stringify(status)}` - ) + throw new Error(`Cannot update upload ${data.cid} ${status}`) } return upload From 5ce1404718d7106dd65d44b0cb37674f2430a4a7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Apr 2022 11:04:09 -0500 Subject: [PATCH 6/6] String validation for cid --- packages/api/src/routes/nfts-upload.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api/src/routes/nfts-upload.js b/packages/api/src/routes/nfts-upload.js index ca7953f5f0..d93399f5e0 100644 --- a/packages/api/src/routes/nfts-upload.js +++ b/packages/api/src/routes/nfts-upload.js @@ -175,8 +175,8 @@ export async function nftUpdateUpload(event, ctx) { const { cid } = params // id is required for updating - if (!cid) - return new JSONResponse({ ok: false, value: 'upload CID is required' }) + if (!cid || typeof cid !== 'string') + return new JSONResponse({ ok: false, value: 'Upload CID is required' }) const body = await event.request.json() const { name } = body