boring.tools-poc/apps/api/src/changelog/version/update.ts
Lars Hampe 8657228e02
Some checks failed
Build and Push Docker Image / build (push) Has been cancelled
Build and Push Docker Image / tests (push) Has been cancelled
feat(api): add redis cache for public page
2024-11-05 15:33:27 +01:00

80 lines
1.8 KiB
TypeScript

import { changelog, changelog_version, db } from '@boring.tools/database'
import { VersionUpdateInput, VersionUpdateOutput } from '@boring.tools/schema'
import { createRoute, type z } from '@hono/zod-openapi'
import { and, eq } from 'drizzle-orm'
import { HTTPException } from 'hono/http-exception'
import { redis } from '../../utils/redis'
export const update = createRoute({
method: 'put',
path: '/:id',
tags: ['version'],
request: {
body: {
content: {
'application/json': { schema: VersionUpdateInput },
},
},
},
responses: {
200: {
content: {
'application/json': { schema: VersionUpdateOutput },
},
description: 'Return updated version',
},
400: {
description: 'Bad Request',
},
500: {
description: 'Internal Server Error',
},
},
})
export const updateFunc = async ({
userId,
payload,
id,
}: {
userId: string
payload: z.infer<typeof VersionUpdateInput>
id: string
}) => {
const changelogResult = await db.query.changelog.findMany({
where: and(eq(changelog.userId, userId)),
with: {
versions: {
where: eq(changelog_version.id, id),
},
},
})
const findChangelog = changelogResult.find((change) =>
change.versions.find((ver) => ver.id === id),
)
if (!findChangelog?.versions.length) {
throw new HTTPException(404, {
message: 'Version not found',
})
}
const [versionUpdateResult] = await db
.update(changelog_version)
.set({
version: payload.version,
status: payload.status,
markdown: payload.markdown,
releasedAt: payload.releasedAt ? new Date(payload.releasedAt) : null,
})
.where(and(eq(changelog_version.id, id)))
.returning()
if (findChangelog.pageId) {
redis.del(findChangelog.pageId)
}
return versionUpdateResult
}