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

feat: content_cid not id is used from nft data upload not uploads #1755

Merged
merged 6 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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
dashcraft marked this conversation as resolved.
Show resolved Hide resolved

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