import { useDataStore } from "~/stores/data" export const useEntities = ( relation: string, ) => { const dataStore = useDataStore() const toast = useToast() const router = useRouter() const dataType = dataStore.dataTypes[relation] const select = async ( select: string = "*", sortColumn: string | null = null, ascending: boolean = true, noArchivedFiltering: boolean = false ) => { const res = await useNuxtApp().$api(`/api/resource/${relation}`, { method: "GET", params: { select, sort: sortColumn || undefined, asc: ascending ? "true" : "false" } }) let data = res || [] if (dataType && dataType.isArchivable && !noArchivedFiltering) { data = data.filter((i: any) => !i.archived) } return data } const selectSingle = async ( idToEq: string | number, select: string = "*", withInformation: boolean = false ) => { if (!idToEq) return null const res = await useNuxtApp().$api(`/api/resource/${relation}/${idToEq}/${withInformation}`, { method: "GET", params: { select } }) return res } const create = async ( payload: Record, noRedirect: boolean = false ) => { const res = await useNuxtApp().$api(`/api/resource/${relation}`, { method: "POST", body: payload }) toast.add({title: `${dataType.labelSingle} hinzugefügt`}) if(dataType.redirect && !noRedirect) { if(dataType.isStandardEntity) { await router.push(dataType.redirectToList ? `/standardEntity/${relation}` : `/standardEntity/${relation}/show/${res.id}`) } else { await router.push(dataType.redirectToList ? `/${relation}` : `/${relation}/show/${res.id}`) } } //modal.close() TODO: Modal Close wenn in Modal return res } const update = async ( id: string | number, payload: Record, noRedirect: boolean = false ) => { const res = await useNuxtApp().$api(`/api/resource/${relation}/${id}`, { method: "PUT", body: payload }) toast.add({title: `${dataType.labelSingle} geändert`}) if(dataType.redirect && !noRedirect) { if(dataType.isStandardEntity) { await router.push(dataType.redirectToList ? `/standardEntity/${relation}` : `/standardEntity/${relation}/show/${res.id}`) } else { await router.push(dataType.redirectToList ? `/${relation}` : `/${relation}/show/${res.id}`) } } //modal.close() TODO: Modal Close wenn in Modal return res } /** * Soft Delete = archived = true */ const archive = async ( id: string | number ) => { const res = await useNuxtApp().$api(`/api/resource/${relation}/${id}`, { method: "PUT", body: { archived: true } }) return res } return {select, create, update, archive, selectSingle} }