Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/pages/events/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,20 @@ async function saveChanges() {

// Update local event data
event.value = { ...editForm.value }

//update map center if location changed
if (editForm.value.location.address !== event.value.location.address) {
center.value = [editForm.value.location.longitude, editForm.value.location.latitude]
}

isEditMode.value = false

console.log('✅ Event updated successfully')
} catch (error) {
if(error.status === 500){
alert('Failed to geocode the provided location. Please check the address and try again.')
return
}
console.error('❌ Error updating event:', error)
alert('Error updating event. Please try again.')
}
Expand Down
11 changes: 8 additions & 3 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
onlyBuiltDependencies:
- '@prisma/client'
- better-sqlite3
allowBuilds:
"@parcel/watcher": true
"@prisma/engines": true
better-sqlite3: true
esbuild: true
prisma: true
unrs-resolver: true
vue-demi: true
131 changes: 64 additions & 67 deletions server/api/events/[id]/index.patch.ts
Original file line number Diff line number Diff line change
@@ -1,118 +1,115 @@
import prisma from '~~/server/utils/prisma'
import prisma from "~~/server/utils/prisma";

// Geocode location using Nominatim
async function geocodeLocation( location: string) {
async function geocodeLocation(location: string) {
try {
const response = await fetch(
`https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(location)}`,
{
headers: {
'User-Agent': 'Abide-Connect/1.0'
}
}
)
"User-Agent": "Abide-Connect/1.0",
},
},
);

if (!response.ok) {
console.warn(`Nominatim API error: ${response.status}`)
return null
console.warn(`Nominatim API error: ${response.status}`);
return null;
}

const results = await response.json()
const results = await response.json();

if (results.length === 0) {
console.warn(`No geocoding results found for: ${location}`)
return null
console.warn(`No geocoding results found for: ${location}`);
return null;
}

const topResult = results[0]
return{
const topResult = results[0];

return {
latitude: parseFloat(topResult.lat),
longitude: parseFloat(topResult.lon),
address: location
}

address: location,
};
} catch (error) {
console.error('❌ Geocoding error:', error)
return null
console.error("❌ Geocoding error:", error);
return null;
}
}



export default defineEventHandler(async (event) => {
const id = getRouterParam(event, 'id')
const body = await readBody(event)
const id = getRouterParam(event, "id");
const body = await readBody(event);

if (!id) {
throw createError({ statusCode: 400, message: 'Missing event ID' })
throw createError({ statusCode: 400, message: "Missing event ID" });
}

//check if location has already been fetched
const locationData = await prisma.location.findUnique({
where: {
address: body.location || null
},
select: {
latitude: true,
longitude: true,
address: true
}
}) || await geocodeLocation(body.location)
//check if location has already been fetched
const locationData =
(await prisma.location.findUnique({
where: {
address: body.location.address || null,
},
select: {
latitude: true,
longitude: true,
address: true,
},
})) || (await geocodeLocation(body.location.address));

console.log('📍 Location data from DB:', locationData)
if (!locationData) {
throw createError({
statusCode: 500,
message: "Failed to retrieve or create location data",
});
}

console.log("📍 Location data from DB:", locationData);

if (!locationData) {
throw createError({
statusCode: 400,
message: 'Invalid location provided and geocoding failed'
})
throw createError({
statusCode: 400,
message: "Invalid location provided and geocoding failed",
});
}

try {
// Check if event exists
const foundEvent = await prisma.event.findUnique({
where: { id }
})
where: { id },
});

if (!foundEvent) {
throw createError({ statusCode: 404, message: 'Event not found' })
throw createError({ statusCode: 404, message: "Event not found" });
}

// Update the event
const updatedEvent = await prisma.event.update({
where: { id },
data: {
title: body.title,
shortDesc: body.shortDesc,
description: body.description,
...body,
location: {
connectOrCreate: {
where: {
address: body.location || null
address: body.location.address,
},
create: locationData!
}
create: locationData!,
},
},
startTime: new Date(body.startTime),
endTime: new Date(body.endTime),
allowVolunteers: body.allowVolunteers,
allowAttendees: body.allowAttendees,
},
include: {
eventAssets: true
}
})
eventAssets: true,
},
});

console.log('✅ Event updated:', updatedEvent)
return updatedEvent

console.log("✅ Event updated:", updatedEvent);
return updatedEvent;
} catch (error) {
console.error('❌ Error updating event:', error)
throw createError({
statusCode: 500,
message: 'Failed to update event'
})
console.error("❌ Error updating event:", error);
throw createError({
statusCode: 500,
message: "Failed to update event",
});
}
})
});
28 changes: 14 additions & 14 deletions server/api/events/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import prisma from '~~/server/utils/prisma'
import prisma from "~~/server/utils/prisma";

export default defineEventHandler(async (_event) => {
try {
const allEvents = await prisma.event.findMany({
include: {
eventAssets: true,
volunteerHours: true,
participants: true
participants: true,
},
orderBy: {
startTime: 'asc'
}
})
startTime: "asc",
},
});

console.log(`Fetched ${allEvents.length} events`)
return allEvents
} catch {
throw createError({
statusCode: 500,
message: 'Failed to fetch events'
})
console.log(`Fetched ${allEvents.length} events`);
return allEvents;
} catch (e) {
console.error(e);
throw createError({
statusCode: 500,
message: "Failed to fetch events",
});
}
})
});
1 change: 0 additions & 1 deletion server/api/events/index.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export default defineEventHandler(async (event) => {
})
}


try {
// Create the event in the database
const newEvent = await prisma.event.create({
Expand Down