Skip to content

Commit

Permalink
modifyasset support modiying tags after init ul
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Oct 12, 2023
1 parent d9cd67c commit a4d21b2
Showing 1 changed file with 60 additions and 13 deletions.
73 changes: 60 additions & 13 deletions src/v2/routes/auth/assets/modifyAsset.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { responseHeaders } from "@/v2/lib/responseHeaders"
import { getConnection } from "@/v2/db/turso"
import { assets } from "@/v2/db/schema"
import { assetTagsAssets, assets } from "@/v2/db/schema"
import { eq } from "drizzle-orm"

import { SplitQueryByCommas } from "@/v2/lib/helpers/splitQueryByCommas"
import { auth } from "@/v2/lib/auth/lucia"
import { roleFlagsToArray } from "@/v2/lib/helpers/roleFlags"

Expand All @@ -28,6 +28,13 @@ export async function modifyAssetData(c: APIContext): Promise<Response> {

const asset = await drizzle.query.assets.findFirst({
where: (assets, { eq }) => eq(assets.id, parseInt(assetIdToModify)),
with: {
assetTagsAssets: {
with: {
assetTags: true,
},
},
},
})

if (!asset) {
Expand Down Expand Up @@ -55,13 +62,14 @@ export async function modifyAssetData(c: APIContext): Promise<Response> {
name: formData.get("name") as string | null,
game: formData.get("game") as string | null,
assetCategory: formData.get("assetCategory") as string | null,
// tags: SplitQueryByCommas(formData.get("tags") as string | null),
}

Object.keys(metadata).forEach(
(key) => metadata[key] === null && delete metadata[key]
)

const tags = SplitQueryByCommas(formData.get("tags") as string | null)

const updatedAsset = await drizzle
.update(assets)
.set({
Expand All @@ -70,15 +78,54 @@ export async function modifyAssetData(c: APIContext): Promise<Response> {
.where(eq(assets.id, parseInt(assetIdToModify)))
.execute()

const response = c.json(
{
success: true,
status: "ok",
updatedAsset,
},
200,
responseHeaders
)
const validTags = []
const invalidTags = []

if (tags && tags.length > 0) {
// remove all existing tags
await drizzle
.delete(assetTagsAssets)
.where(eq(assetTagsAssets.assetId, parseInt(assetIdToModify)))
.execute()

return response
// add new tags
await drizzle.transaction(async (trx) => {
for (const tag of tags) {
const tagExists = await trx.query.assetTags.findFirst({
where: (assetTags) => {
return eq(assetTags.name, tag)
},
})
if (tagExists) {
await trx
.insert(assetTagsAssets)
.values({
id: crypto.randomUUID(),
assetId: parseInt(assetIdToModify),
assetTagId: tagExists[0].assetTagId,
})
.returning({
assetTagId: assetTagsAssets.assetTagId,
})
validTags.push(tag)
} else {
invalidTags.push(tag)
}
}
})

const response = c.json(
{
success: true,
status: "ok",
updatedAsset,
validTags: validTags ? validTags : undefined,
invalidTags: invalidTags ? invalidTags : undefined,
},
200,
responseHeaders
)

return response
}
}

0 comments on commit a4d21b2

Please sign in to comment.