1
0
Fork 0

feat: add query and mutation helpers for orte and fahrzeuge

This commit is contained in:
Jonny 2026-07-06 19:35:25 +02:00
parent 4795ea1f32
commit d8990ba57b
2 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,90 @@
import { createClient } from "@/lib/supabase/server";
export type MutationResult = { success: true; id: string } | { success: false; error: string };
export async function insertOrt(input: {
adresse: string;
ansprechpartner: string;
createdBy: string;
}): Promise<MutationResult> {
const supabase = await createClient();
const { data, error } = await supabase
.from("orte")
.insert({ adresse: input.adresse, ansprechpartner: input.ansprechpartner, created_by: input.createdBy })
.select("id")
.single();
if (error || !data) {
return { success: false, error: "Ort konnte nicht angelegt werden." };
}
return { success: true, id: data.id };
}
export async function updateOrtZeile(input: {
id: string;
adresse: string;
ansprechpartner: string;
}): Promise<MutationResult> {
const supabase = await createClient();
const { data, error } = await supabase
.from("orte")
.update({ adresse: input.adresse, ansprechpartner: input.ansprechpartner })
.eq("id", input.id)
.select("id")
.single();
if (error || !data) {
return { success: false, error: "Ort konnte nicht aktualisiert werden." };
}
return { success: true, id: data.id };
}
export async function insertFahrzeug(input: {
kennzeichen: string;
marke: string | null;
modell: string | null;
}): Promise<MutationResult> {
const supabase = await createClient();
const { data, error } = await supabase
.from("fahrzeuge")
.insert({ kennzeichen: input.kennzeichen, marke: input.marke, modell: input.modell })
.select("id")
.single();
if (error) {
if (error.code === "23505") {
return { success: false, error: "Dieses Kennzeichen ist bereits vergeben." };
}
return { success: false, error: "Fahrzeug konnte nicht angelegt werden." };
}
if (!data) {
return { success: false, error: "Fahrzeug konnte nicht angelegt werden." };
}
return { success: true, id: data.id };
}
export async function updateFahrzeugZeile(input: {
id: string;
kennzeichen: string;
marke: string | null;
modell: string | null;
}): Promise<MutationResult> {
const supabase = await createClient();
const { data, error } = await supabase
.from("fahrzeuge")
.update({ kennzeichen: input.kennzeichen, marke: input.marke, modell: input.modell })
.eq("id", input.id)
.select("id")
.single();
if (error) {
if (error.code === "23505") {
return { success: false, error: "Dieses Kennzeichen ist bereits vergeben." };
}
return { success: false, error: "Fahrzeug konnte nicht aktualisiert werden." };
}
if (!data) {
return { success: false, error: "Fahrzeug konnte nicht aktualisiert werden." };
}
return { success: true, id: data.id };
}

View File

@ -0,0 +1,18 @@
import { createClient } from "@/lib/supabase/server";
import type { Ort, Fahrzeug } from "@/lib/auftraege/queries";
export async function loadOrt(id: string): Promise<Ort | null> {
const supabase = await createClient();
const { data } = await supabase.from("orte").select("id, adresse, ansprechpartner").eq("id", id).single();
return data ?? null;
}
export async function loadFahrzeug(id: string): Promise<Fahrzeug | null> {
const supabase = await createClient();
const { data } = await supabase
.from("fahrzeuge")
.select("id, kennzeichen, marke, modell")
.eq("id", id)
.single();
return data ?? null;
}