feat(frontend,backend): Create and load comments

This commit is contained in:
2025-05-13 08:46:02 +02:00
parent d4ea9cd3d9
commit cdbf527ea6
14 changed files with 1294 additions and 304 deletions

View File

@@ -0,0 +1,73 @@
import { type CreateCommentDto, type CommentDto, type PagedCommentDto, ResponseError } from '~/.api-client'
import { useCommentApi } from './useCommentApi'
export function useComment() {
const commentApi = useCommentApi()
async function createComment(
applicationFormId: string,
formElementId: string,
createCommentDto: CreateCommentDto
): Promise<CommentDto> {
try {
return await commentApi.createComment(applicationFormId, formElementId, createCommentDto)
} catch (e: unknown) {
if (e instanceof ResponseError) {
console.error('Failed creating comment:', e.response)
} else {
console.error('Failed creating comment:', e)
}
return Promise.reject(e)
}
}
async function getCommentsByApplicationFormId(applicationFormId: string): Promise<PagedCommentDto> {
try {
return await commentApi.getCommentsByApplicationFormId(applicationFormId)
} catch (e: unknown) {
if (e instanceof ResponseError) {
console.error('Failed retrieving comments:', e.response)
} else {
console.error('Failed retrieving comments:', e)
}
return Promise.reject(e)
}
}
async function updateComment(id?: string, commentDto?: CommentDto): Promise<CommentDto> {
if (!id || !commentDto) {
return Promise.reject(new Error('ID or comment DTO missing'))
}
try {
return await commentApi.updateComment(id, commentDto)
} catch (e: unknown) {
if (e instanceof ResponseError) {
console.error(`Failed updating comment with ID ${id}:`, e.response)
} else {
console.error(`Failed updating comment with ID ${id}:`, e)
}
return Promise.reject(e)
}
}
async function deleteCommentById(id: string): Promise<void> {
try {
return await commentApi.deleteCommentById(id)
} catch (e: unknown) {
if (e instanceof ResponseError) {
console.error(`Failed deleting comment with ID ${id}:`, e.response)
} else {
console.error(`Failed deleting comment with ID ${id}:`, e)
}
return Promise.reject(e)
}
}
return {
createComment,
getCommentsByApplicationFormId,
updateComment,
deleteCommentById
}
}

View File

@@ -0,0 +1,43 @@
import { CommentApi, Configuration, type CommentDto, type CreateCommentDto, type PagedCommentDto } from '~/.api-client'
import { cleanDoubleSlashes, withoutTrailingSlash } from 'ufo'
export function useCommentApi() {
const appBaseUrl = useRuntimeConfig().app.baseURL
const { serverApiBaseUrl, serverApiBasePath, clientProxyBasePath } = useRuntimeConfig().public
const { jwt } = useAuth()
const basePath = withoutTrailingSlash(
cleanDoubleSlashes(import.meta.client ? appBaseUrl + clientProxyBasePath : serverApiBaseUrl + serverApiBasePath)
)
const commentApiClient = new CommentApi(
new Configuration({ basePath, headers: { Authorization: jwt.value ? `Bearer ${jwt.value}` : '' } })
)
async function createComment(
applicationFormId: string,
formElementId: string,
createCommentDto: CreateCommentDto
): Promise<CommentDto> {
return commentApiClient.createComment({ applicationFormId, formElementId, createCommentDto })
}
async function getCommentsByApplicationFormId(applicationFormId: string): Promise<PagedCommentDto> {
return commentApiClient.getCommentsByApplicationFormId({ applicationFormId })
}
async function updateComment(id: string, commentDto: CommentDto): Promise<CommentDto> {
return commentApiClient.updateComment({ id, commentDto })
}
async function deleteCommentById(id: string): Promise<void> {
return commentApiClient.deleteComment({ id })
}
return {
createComment,
getCommentsByApplicationFormId,
updateComment,
deleteCommentById
}
}