import { define } from "../../utils.ts"; import RentalsOwned from "../../islands/RentalsOwned.tsx"; import { page } from "fresh"; import { createProperty, getProperties, } from "../../services/propertyService.ts"; import { SessionCookie } from "../auth/callback.ts"; import { Property } from "../../db/interfaces.ts"; export const handler = define.handlers({ GET: async (ctx) => { const stateData: SessionCookie = ctx.state.data; let properties: Property[] = []; if (stateData?.user_id) { properties = await getProperties(stateData.user_id); } return page({ stateData, properties, }); }, POST: async (ctx) => { const stateData: SessionCookie = ctx.state.data; const form = await ctx.req.formData(); const properties = JSON.parse(form.get("properties")?.toString() || "[]"); const timestampNow = Date.now(); const property = { id: crypto.randomUUID(), owner_id: stateData.user_id, title: form.get("title")?.toString() || null, description: form.get("description")?.toString() || null, address_1: form.get("address_1")?.toString() || null, address_2: form.get("address_2")?.toString() || null, city: form.get("city")?.toString() || null, state: form.get("state")?.toString() || null, zip: form.get("zip")?.toString() || null, country: form.get("country")?.toString() || null, bedrooms: form.get("bedrooms") ? Number(form.get("bedrooms")) : null, bathrooms: form.get("bathrooms") ? Number(form.get("bathrooms")) : null, square_footage: form.get("square_footage") ? Number(form.get("square_footage")) : null, lot_size: form.get("lot_size") ? Number(form.get("lot_size")) : null, year_built: form.get("year_built") ? Number(form.get("year_built")) : null, price: form.get("price") ? Number(form.get("price")) : null, property_type: form.get("property_type")?.toString() || null, listing_status: form.get("listing_status")?.toString() || null, image_url: form.get("image_url")?.toString() || null, rental_status: "draft", created_on: timestampNow, updated_on: timestampNow, }; function _emptyToNull(obj: Record) { for (const key in obj) { if (obj[key] === "") obj[key] = null; } return obj; } //const cleanedProperty = emptyToNull(property); const [newProperty] = await createProperty(property); if (newProperty) { properties.push(newProperty); } return page({ stateData, properties, }); }, }); export default define.page( ( { data }: { data: { stateData: SessionCookie; properties: Property[]; }; }, ) => (
), );