diff --git a/db/config.ts b/db/config.ts index 1efe154..917424a 100644 --- a/db/config.ts +++ b/db/config.ts @@ -17,6 +17,19 @@ const Food = defineTable({ } }); +export const events = defineTable({ + columns: { + id: column.number({primaryKey: true, unique: true}), + title: column.text(), + description: column.text(), + dateStart: column.text(), + dateEnd: column.text(), + url: column.text(), + type: column.text(), + year: column.number(), + } +}); + export const user = defineTable( { columns: { id: column.text({primaryKey: true, notNull: true, unique: true}), @@ -46,5 +59,5 @@ const list = defineTable({ // https://astro.build/db/config export default defineDb( { - tables: { Food, user, session, list} + tables: { Food, user, session, list, events } } ); diff --git a/src/components/DataForm.svelte b/src/components/DataForm.svelte index b76eaa2..dc26110 100644 --- a/src/components/DataForm.svelte +++ b/src/components/DataForm.svelte @@ -1,5 +1,5 @@
@@ -66,6 +100,7 @@ {/if} {#if data} +

Food

@@ -91,12 +126,33 @@
- +
+

Event Details

+
+ + +
+ +
+

+        {JSON.stringify(data.eventDetails, null, 2)}
+    
+
+ +

-        {JSON.stringify(data, null, 2)}
+        {JSON.stringify(data.food, null, 2)}
     
{/if} diff --git a/src/pages/api/events/addDetails.ts b/src/pages/api/events/addDetails.ts new file mode 100644 index 0000000..5dd8cac --- /dev/null +++ b/src/pages/api/events/addDetails.ts @@ -0,0 +1,20 @@ +import type { APIContext } from "astro"; +import { db, events } from "astro:db"; + +export async function POST(context: APIContext): Promise { + if (!context.locals.session) { + return new Response(null, { + status: 401, + }); + } + + // add event details to events table + const data = await context.request.json(); + const event = await db.insert(events).values(data).run(); + + if (event) { + return new Response(null, { status: 200 }); + } else { + return new Response(null, { status: 400 }); + } +} diff --git a/src/pages/api/fetchDataFromSource.ts b/src/pages/api/fetchDataFromSource.ts index dd0c284..2ca937c 100644 --- a/src/pages/api/fetchDataFromSource.ts +++ b/src/pages/api/fetchDataFromSource.ts @@ -65,8 +65,36 @@ const getPages = async (eventUrls: string[]) => { return await Promise.all(events); }; +// Function to parse the date from the given text +function parseDates(dateStr: string): { dateStart: Date; dateEnd: Date } { + const dateEndText = dateStr.split("through ")[1]; // Extracts "April 21" + const currentYear = new Date().getFullYear(); // Gets the current year + const dateEnd = new Date(`${dateEndText}, ${currentYear}`); // Assumes the end date is within the current year + + // dateStart is dateEnd minus 7 days + const dateStart = new Date(dateEnd); + dateStart.setDate(dateStart.getDate() - 6); + + return { dateStart, dateEnd }; +} + +async function getEventDetails(baseUrl: string) { + const $ = await fetchData(baseUrl); + const title = $("header > h1").text().trim(); + const dateText = $(".date-summary > span").text().trim(); + const { dateStart, dateEnd } = parseDates(dateText); + const url = baseUrl; + const description = $(".descriptions > .description").text().trim(); + const year = dateStart.getFullYear(); + const types = ["sandwich", "nacho", "burger", "pizza"]; + // check which type the title contains + const type = types.find(type => title.toLowerCase().includes(type)); + return { title, dateStart, dateEnd, url, description, year, type }; +} + const getEventUrls = async (baseUrl: string) => { const $ = await fetchData(baseUrl); + const eventUrls: string[] = []; $(".item-detail.event > .row > .col > a").each(function (index, element) { @@ -95,9 +123,10 @@ export async function POST(context: APIContext): Promise { console.log("Using cached data"); return new Response(JSON.stringify(cache)); } + const eventDetails = await getEventDetails(baseUrl); const urls = await getEventUrls(baseUrl); const eventData = await getPages(urls); - cache = eventData; + cache = { eventDetails, food: eventData }; return new Response(JSON.stringify(eventData)); } diff --git a/src/pages/pizza/[year].astro b/src/pages/pizza/[year].astro index 41e7869..90432f5 100644 --- a/src/pages/pizza/[year].astro +++ b/src/pages/pizza/[year].astro @@ -5,6 +5,8 @@ import Main from "@layouts/Main.astro"; import Footer from "@components/Footer.astro"; import CardGrid from "../../components/CardGrid.astro"; import { + formatter, + getEventDetails, getFoodItems, getLists, getUniqueNeighborhoods, @@ -15,6 +17,11 @@ const { year } = Astro.params; const pizzas = await getFoodItems(year!, "pizza"); const neighborhoods = pizzas ? getUniqueNeighborhoods(pizzas) : []; const lists = await getLists(Astro.locals?.user?.id); +const eventDetails = await getEventDetails(year!, "pizza"); + +const dateRange = eventDetails + ? `${formatter.format(new Date(eventDetails.dateStart))} - ${formatter.format(new Date(eventDetails.dateEnd))} ${eventDetails.year}` + : null; ---
+ {dateRange ?

{dateRange}

: null} {pizzas && }