Neue Datenbank eingebunden
This commit is contained in:
@@ -4,66 +4,62 @@ import { db } from "src/lib/shared";
|
||||
import { z } from "zod";
|
||||
|
||||
const AusweisUploadChecker = z.object({
|
||||
ausweisart: z.enum(["VA", "BA", "VANW"]),
|
||||
ausweis: z.object({
|
||||
ausweisart: z.enum(["VA", "BA", "VANW"]),
|
||||
ausstellgrund: z.enum([
|
||||
"Vermietung",
|
||||
"Neubau",
|
||||
"Verkauf",
|
||||
"Modernisierung",
|
||||
"Sonstiges",
|
||||
]),
|
||||
}),
|
||||
|
||||
objekt_typ: z.string(),
|
||||
objekt_plz: z.string(),
|
||||
objekt_ort: z.string(),
|
||||
objekt_strasse: z.string(),
|
||||
objekt_gebaeudeteil: z.string(),
|
||||
objekt_saniert: z.boolean() ,
|
||||
baujahr_gebaeude: z.number(),
|
||||
baujahr_anlage: z.number(),
|
||||
anzahl_einheiten: z.number(),
|
||||
gebaeude: z.object({
|
||||
typ: z.string(),
|
||||
plz: z.string(),
|
||||
ort: z.string(),
|
||||
strasse: z.string(),
|
||||
gebaeudeteil: z.string(),
|
||||
saniert: z.boolean(),
|
||||
baujahr: z.number(),
|
||||
einheiten: z.number(),
|
||||
wohnflaeche: z.number(),
|
||||
keller_beheizt: z.boolean(),
|
||||
dachgeschoss_beheizt: z.number(),
|
||||
lueftungskonzept: z.enum([
|
||||
"Fensterlüftung",
|
||||
"Schachtlüftung",
|
||||
"Lüftungsanlage ohne Wärmerückgewinnung",
|
||||
"Lüftungsanlage mit Wärmerückgewinnung",
|
||||
]),
|
||||
wird_gekuehlt: z.boolean(),
|
||||
leerstand: z.number(),
|
||||
versorgungssysteme: z.number(),
|
||||
fenster_dach: z.number(),
|
||||
energiequelle_2_nutzung: z.number(),
|
||||
daemmung: z.number(),
|
||||
}),
|
||||
|
||||
ausstellgrund: z.enum(["Vermietung"
|
||||
,"Neubau"
|
||||
, "Verkauf"
|
||||
, "Modernisierung"
|
||||
, "Sonstiges"]),
|
||||
kennwerte: z.object({
|
||||
zeitraum: z.string(),
|
||||
verbrauch_1: z.number(),
|
||||
verbrauch_2: z.number(),
|
||||
verbrauch_3: z.number(),
|
||||
verbrauch_4: z.number(),
|
||||
verbrauch_5: z.number(),
|
||||
verbrauch_6: z.number(),
|
||||
einheit_1: z.string(),
|
||||
einheit_2: z.string(),
|
||||
energietraeger_1: z.string(),
|
||||
energietraeger_2: z.string(),
|
||||
anteil_warmwasser_1: z.number(),
|
||||
anteil_warmwasser_2: z.number(),
|
||||
}),
|
||||
|
||||
energieverbrauch_zeitraum: z.string(),
|
||||
energieverbrauch_1_heizquelle_1: z.number(),
|
||||
energieverbrauch_2_heizquelle_1: z.number(),
|
||||
energieverbrauch_3_heizquelle_1: z.number(),
|
||||
energieverbrauch_1_heizquelle_2: z.number(),
|
||||
energieverbrauch_2_heizquelle_2: z.number(),
|
||||
energieverbrauch_3_heizquelle_2: z.number(),
|
||||
|
||||
energietraeger_einheit_heizquelle_1: z.string(),
|
||||
energietraeger_einheit_heizquelle_2: z.string(),
|
||||
energietraeger_1: z.string(),
|
||||
energietraeger_2: z.string(),
|
||||
|
||||
anteil_warmwasser_1: z.number(),
|
||||
anteil_warmwasser_2: z.number(),
|
||||
|
||||
wohnflaeche: z.number(),
|
||||
|
||||
keller_beheizt: z.boolean(),
|
||||
dachgeschoss: z.number(),
|
||||
zusaetzliche_heizquelle: z.boolean(),
|
||||
warmwasser_enthalten: z.boolean(),
|
||||
lueftungskonzept: z.enum([
|
||||
"Fensterlüftung",
|
||||
"Schachtlüftung",
|
||||
"Lüftungsanlage ohne Wärmerückgewinnung",
|
||||
"Lüftungsanlage mit Wärmerückgewinnung",
|
||||
]),
|
||||
wird_gekuehlt: z.boolean(),
|
||||
leerstand: z.number(),
|
||||
images: z.array(z.string()),
|
||||
|
||||
versorgungssysteme: z.number(),
|
||||
fenster_dach: z.number(),
|
||||
energiequelle_2_nutzung: z.number(),
|
||||
daemmung: z.number(),
|
||||
|
||||
anrede: z.string().optional(),
|
||||
name: z.string().optional(),
|
||||
vorname: z.string().optional(),
|
||||
email: z.string().optional(),
|
||||
telefonnummer: z.string().optional(),
|
||||
gebaeude_uid: z.string().optional(),
|
||||
kennwerte_uid: z.string().optional(),
|
||||
ausweis_uid: z.string().optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -72,7 +68,7 @@ const AusweisUploadChecker = z.object({
|
||||
* @returns
|
||||
*/
|
||||
export const post: APIRoute = async ({ request }) => {
|
||||
const body = await request.json();
|
||||
const body: z.infer<typeof AusweisUploadChecker> = await request.json();
|
||||
|
||||
const validation = AusweisUploadChecker.safeParse(body);
|
||||
|
||||
@@ -80,43 +76,63 @@ export const post: APIRoute = async ({ request }) => {
|
||||
return error(validation.error.issues);
|
||||
}
|
||||
|
||||
const result = await db("ausweise").insert(body).returning(["uid", "id"]);
|
||||
if (!result) {
|
||||
let gebaeude, kennwerte, ausweis;
|
||||
if (body.gebaeude_uid) {
|
||||
gebaeude = await db("gebaeude")
|
||||
.update(body.gebaeude)
|
||||
.where("uid", body.gebaeude_uid)
|
||||
.returning(["uid", "id"]);
|
||||
} else {
|
||||
gebaeude = await db("gebaeude")
|
||||
.insert(body.gebaeude)
|
||||
.returning(["uid", "id"]);
|
||||
}
|
||||
|
||||
if (!gebaeude) {
|
||||
return ActionFailedError();
|
||||
}
|
||||
|
||||
const { uid, id } = result[0];
|
||||
if (body.kennwerte_uid) {
|
||||
kennwerte = await db("energiekennwerte")
|
||||
.update({ ...body.kennwerte, gebaeude_id: gebaeude[0].id })
|
||||
.where("uid", body.kennwerte_uid)
|
||||
.returning(["uid", "id"]);
|
||||
} else {
|
||||
kennwerte = await db("energiekennwerte")
|
||||
.insert({ ...body.kennwerte, gebaeude_id: gebaeude[0].id })
|
||||
.returning(["uid", "id"]);
|
||||
}
|
||||
|
||||
if (!uid || !id) {
|
||||
if (!kennwerte) {
|
||||
return ActionFailedError();
|
||||
}
|
||||
|
||||
if (body.ausweis_uid) {
|
||||
ausweis = await db("energieausweise")
|
||||
.update({
|
||||
...body.ausweis,
|
||||
gebaeude_id: gebaeude[0].id,
|
||||
energiekennwerte_id: kennwerte[0].id,
|
||||
})
|
||||
.where("uid", body.ausweis_uid)
|
||||
.returning(["uid", "id"]);
|
||||
} else {
|
||||
ausweis = await db("energieausweise")
|
||||
.insert({
|
||||
...body.ausweis,
|
||||
gebaeude_id: gebaeude[0].id,
|
||||
energiekennwerte_id: kennwerte[0].id,
|
||||
})
|
||||
.returning(["uid", "id"]);
|
||||
}
|
||||
|
||||
if (!ausweis) {
|
||||
return ActionFailedError();
|
||||
}
|
||||
|
||||
return success({
|
||||
uid, id
|
||||
ausweis: ausweis[0],
|
||||
kennwerte: kennwerte[0],
|
||||
gebaeude: gebaeude[0],
|
||||
});
|
||||
};
|
||||
|
||||
export const put: APIRoute = async ({ request }) => {
|
||||
const body = await request.json();
|
||||
|
||||
const validation = AusweisUploadChecker.safeParse(body);
|
||||
|
||||
if (!validation.success) {
|
||||
return error(validation.error.issues);
|
||||
}
|
||||
|
||||
const result = await db("ausweise").insert(body).returning(["uid", "id"]);
|
||||
if (!result) {
|
||||
return ActionFailedError();
|
||||
}
|
||||
|
||||
const { uid, id } = result[0];
|
||||
|
||||
if (!uid || !id) {
|
||||
return ActionFailedError();
|
||||
}
|
||||
|
||||
return success({
|
||||
uid, id
|
||||
});
|
||||
};
|
||||
};
|
||||
@@ -1,13 +1,20 @@
|
||||
import type { APIRoute } from "astro";
|
||||
import { ActionFailedError, error, success } from "src/lib/APIResponse";
|
||||
import {
|
||||
ActionFailedError,
|
||||
MissingEntityError,
|
||||
error,
|
||||
success,
|
||||
} from "src/lib/APIResponse";
|
||||
import * as jimp from "jimp";
|
||||
import { v4 as uuid } from "uuid";
|
||||
import { z } from "zod";
|
||||
import * as path from "path";
|
||||
import { db } from "src/lib/shared";
|
||||
|
||||
const ImageUploadChecker = z.object({
|
||||
data: z.string(),
|
||||
name: z.string()
|
||||
name: z.string(),
|
||||
gebaeude_uid: z.string().optional(),
|
||||
kategorie: z.string(),
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -16,29 +23,60 @@ const ImageUploadChecker = z.object({
|
||||
* @returns
|
||||
*/
|
||||
export const put: APIRoute = async ({ request }) => {
|
||||
const body = await request.json();
|
||||
const body: z.infer<typeof ImageUploadChecker> = await request.json();
|
||||
|
||||
const result = ImageUploadChecker.safeParse(body);
|
||||
const validation = ImageUploadChecker.safeParse(body);
|
||||
|
||||
if (!result.success) {
|
||||
return error(result.error.issues);
|
||||
if (!validation.success) {
|
||||
return error(validation.error.issues);
|
||||
}
|
||||
|
||||
const image = Buffer.from(body.data, "base64url");
|
||||
|
||||
|
||||
let jimpResult;
|
||||
try {
|
||||
jimpResult = await jimp.read(image);
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
return ActionFailedError();
|
||||
}
|
||||
|
||||
const uid = uuid();
|
||||
const location = path.join(uid);
|
||||
let gebaeude;
|
||||
if (!body.gebaeude_uid) {
|
||||
gebaeude = await db("gebaeude").insert({}).returning(["uid", "id"]);
|
||||
|
||||
jimpResult.quality(0.75).write(location);
|
||||
if (!gebaeude) {
|
||||
return ActionFailedError();
|
||||
}
|
||||
} else {
|
||||
gebaeude = await db("gebaeude")
|
||||
.select("*")
|
||||
.where("uid", body.gebaeude_uid);
|
||||
|
||||
if (!gebaeude) {
|
||||
return MissingEntityError("gebaeude");
|
||||
}
|
||||
}
|
||||
|
||||
const result = await db("gebaeude_bilder")
|
||||
.insert({ gebaeude_id: gebaeude[0].id, kategorie: body.kategorie })
|
||||
.returning(["uid"]);
|
||||
|
||||
if (!result) {
|
||||
return ActionFailedError();
|
||||
}
|
||||
|
||||
const location = path.join("uploads", `${result[0].uid}.jpg`);
|
||||
|
||||
const buffer = await jimpResult.getBufferAsync(jimp.MIME_JPEG)
|
||||
|
||||
if (buffer.length > 3_000_000) {
|
||||
jimpResult.quality(75).write(location);
|
||||
} else {
|
||||
jimpResult.write(location);
|
||||
}
|
||||
|
||||
return success({
|
||||
uid
|
||||
uid: result[0].uid,
|
||||
gebaeude_uid: gebaeude[0].uid,
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user