Skip to content

Commit

Permalink
feat: content_cid not id is used from nft data upload not uploads (#1755
Browse files Browse the repository at this point in the history
)

* content_cid not id is used from nft data upload not uploads

* missed an updated key

* Update api test to use content_cid not id

* Update packages/api/src/routes/nfts-upload.js

Co-authored-by: Alan Shaw <alan.shaw@protocol.ai>

* Updated per alan's feedback

* String validation for cid

Co-authored-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
dashcraft and Alan Shaw authored Apr 4, 2022
1 parent bef54dc commit 21e8eb0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
11 changes: 8 additions & 3 deletions packages/api/src/routes/nfts-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 { cid } = params

// id is required for updating
if (!id) return new JSONResponse({ ok: false, value: 'ID 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

const updatedRecord = await db.updateUpload({ id, name, user_id: user.id })
const updatedRecord = await db.updateUpload({
cid,
name,
user_id: user.id,
})

return new JSONResponse({ ok: true, value: updatedRecord })
} catch (/** @type {any} */ err) {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/utils/db-client-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface CreateUploadInput {
}

export interface UpdateUploadInput {
id: string
cid: string
name?: string
user_id: number
}
Expand Down
10 changes: 3 additions & 7 deletions packages/api/src/utils/db-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,17 @@ export class DBClient {
updated_at: now,
})
.select(this.uploadQuery)
.eq('id', data.id)
.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.id}`)
throw new Error(`Status 406, cannot update ${data.cid}`)
}

if (!upload) {
throw new Error(
`Cannot update upload ${JSON.stringify(data.id)} ${JSON.stringify(
status
)}`
)
throw new Error(`Cannot update upload ${data.cid} ${status}`)
}

return upload
Expand Down
4 changes: 2 additions & 2 deletions packages/api/test/nfts-upload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down

0 comments on commit 21e8eb0

Please sign in to comment.