Skip to content

Commit

Permalink
feat: Cloudinary uploads for Event Assets (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abeeujah authored Dec 3, 2024
1 parent e426af4 commit 48919b0
Show file tree
Hide file tree
Showing 17 changed files with 486 additions and 129 deletions.
2 changes: 1 addition & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
> Copy `.env.example` to `.env` file and update the postgresql credentials for production environment also set NODE_ENV to production if you want to run production, you can either use DB URL or set DB host,username,password and port with SSL for your DB connection.
### STEP 4
> Start project with `npm start` on production environment and `npm run dev` on development environment.
> Start project with `bun start` on production environment and `bun dev` on development environment.
Binary file modified backend/bun.lockb
Binary file not shown.
10 changes: 5 additions & 5 deletions backend/config/events.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const events = {
NewEventAdded: "NewEventAdded", //replace with event identify
RegisteredForEvent: "RegisteredForEvent",
EventAttendanceMark: "EventAttendanceMark",
EndEventRegistration: "EndEventRegistration",
RSVPForEvent: "RSVPForEvent"
NewEventAdded: "0x1", //replace with event identify
RegisteredForEvent: "0x2",
EventAttendanceMark: "0x3",
EndEventRegistration: "0x4",
RSVPForEvent: "0x5"
};
96 changes: 95 additions & 1 deletion backend/controllers/EventController.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const fetchEventRegistrationAttendeesForOneEvent = async (req, res) => {
const { page = 1, limit = 10 } = req.query;

const event = await Event.findByEventId(event_id);

if (!event) {
return failure(res, "Event not found", [], 404);
}
Expand All @@ -116,3 +116,97 @@ export const fetchEventRegistrationAttendeesForOneEvent = async (req, res) => {
return failure(res, err.message, [], 500);
}
};

export const uploadEventNft = async (req, res) => {
try {
const { cloudinaryUpload } = req;
if (!cloudinaryUpload) {
return failure(res, "Failed to upload image", [], 400);
}

const { event_id } = req.params;
const event = await Event.findByEventId(event_id);

if (!event) {
return failure(res, "Event not found", [], 404);
}

const nft = await Event.persistNFT({ event_id, nft: cloudinaryUpload.url });

return success(res, "Event NFT uploaded successfully.", nft, 200);
} catch (err) {
return failure(res, err.message, [], 500);
}
};

export const fetchEventNft = async (req, res) => {
try {
const { event_id } = req.params;

const event = await Event.findByEventId(event_id);
if (!event) {
return failure(res, "Event not found", [], 404);
}

const nft = await Event.retrieveEventNFT(event_id);
if (!nft) {
return failure(res, "NFT not found", [], 404);
}

return success(res, "Event NFT fetched successfully", nft, 200);
} catch (err) {
return failure(res, err.message, [], 500);
}
};

export const deleteEventNft = async (req, res) => {
try {
const { event_id } = req.params;

const event = await Event.findByEventId(event_id);
if (!event) {
return failure(res, "Event not found", [], 404);
}

const nft = await Event.retrieveEventNFT(event_id);
if (!nft) {
return failure(res, "NFT not found", [], 404);
}

await Event.deleteEventNFT(event_id);

return success(res, "Event NFT deleted successfully", [], 200);
} catch (err) {
return failure(res, err.message, [], 500);
}
};

export const updateEventNft = async (req, res) => {
try {
const { cloudinaryUpload } = req;
if (!cloudinaryUpload) {
return failure(res, "Failed to upload image", [], 400);
}

const { event_id } = req.params;

const event = await Event.findByEventId(event_id);
if (!event) {
return failure(res, "Event not found", [], 404);
}

const existingNft = await Event.retrieveEventNFT(event_id);
if (!existingNft) {
return failure(res, "NFT not found", [], 404);
}

const updatedNft = await Event.updateEventNFT({
event_id,
nft: cloudinaryUpload.url,
});

return success(res, "Event NFT updated successfully.", updatedNft, 200);
} catch (err) {
return failure(res, err.message, [], 500);
}
};
203 changes: 114 additions & 89 deletions backend/indexer/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,131 +1,156 @@
import {
NewEventAdded,
RegisteredForEvent,
EventAttendanceMark,
EndEventRegistration,
RSVPForEvent
import {
NewEventAdded,
RegisteredForEvent,
EventAttendanceMark,
EndEventRegistration,
RSVPForEvent,
} from "./types";
import { FieldElement, v1alpha2 as starknet } from '@apibara/starknet';
import { FieldElement, v1alpha2 as starknet } from "@apibara/starknet";
import Event from "../models/Event.js";
import { uint256 } from 'starknet';
import { uint256 } from "starknet";
import { hexToAscii } from "../utils/tohexAscii.js";

export async function handleNewEventAdded(event: starknet.IEvent) {
const data = event.data!;
const data = event.data!;

const eventDetails: NewEventAdded = {
name: hexToAscii(FieldElement.toHex(data[0]).toString()),
event_id: parseInt(uint256
const eventDetails: NewEventAdded = {
name: hexToAscii(FieldElement.toHex(data[0]).toString()),
event_id: parseInt(
uint256
.uint256ToBN({
low: FieldElement.toBigInt(data[1]),
high: FieldElement.toBigInt(data[2]),
})
.toString()),
location: hexToAscii(FieldElement.toHex(data[3]).toString()),
event_owner: FieldElement.toHex(data[4]).toString()
};

//Debugging purposes
console.log(eventDetails);

const eventExists = await Event.findByEventId(eventDetails.event_id);
if (eventExists) {
console.log("Event already exists");
return;
}
await Event.create(eventDetails);
.toString()
),
location: hexToAscii(FieldElement.toHex(data[3]).toString()),
event_owner: FieldElement.toHex(data[4]).toString(),
};

//Debugging purposes
console.log(eventDetails);

const eventExists = await Event.findByEventId(eventDetails.event_id);
if (eventExists) {
console.log("Event already exists");
return;
}
await Event.create(eventDetails);
}

export async function handleRegisteredForEvent(event: starknet.IEvent) {
const data = event.data!;
const data = event.data!;

const registeredForEvent: RegisteredForEvent = {
event_id: parseInt(uint256
const registeredForEvent: RegisteredForEvent = {
event_id: parseInt(
uint256
.uint256ToBN({
low: FieldElement.toBigInt(data[0]),
high: FieldElement.toBigInt(data[1]),
})
.toString()),
event_name: hexToAscii(FieldElement.toHex(data[2]).toString()),
user_address: FieldElement.toHex(data[3]).toString()
};

console.log(registeredForEvent);

const hasRegistered = await Event.isUserRegistered(registeredForEvent.event_id, registeredForEvent.user_address);
if (hasRegistered) {
console.log("User has already registered");
return;
}
await Event.registerUser(registeredForEvent.event_id, registeredForEvent.user_address);
.toString()
),
event_name: hexToAscii(FieldElement.toHex(data[2]).toString()),
user_address: FieldElement.toHex(data[3]).toString(),
};

console.log(registeredForEvent);

const hasRegistered = await Event.isUserRegistered(
registeredForEvent.event_id,
registeredForEvent.user_address
);
if (hasRegistered) {
console.log("User has already registered");
return;
}
await Event.registerUser(
registeredForEvent.event_id,
registeredForEvent.user_address
);
}

export async function handleEventAttendanceMark(event: starknet.IEvent) {
const data = event.data!;
const data = event.data!;

const eventAttendanceMark: EventAttendanceMark = {
event_id: parseInt(uint256
const eventAttendanceMark: EventAttendanceMark = {
event_id: parseInt(
uint256
.uint256ToBN({
low: FieldElement.toBigInt(data[0]),
high: FieldElement.toBigInt(data[1]),
})
.toString()),
user_address: FieldElement.toHex(data[2]).toString()
};

console.log(eventAttendanceMark);

const hasMarkedAttendance = await Event.hasUserAttended(eventAttendanceMark.event_id, eventAttendanceMark.user_address);
if (hasMarkedAttendance) {
console.log("User has already marked attendance");
return;
}
await Event.markAttendance(eventAttendanceMark.event_id, eventAttendanceMark.user_address);
.toString()
),
user_address: FieldElement.toHex(data[2]).toString(),
};

console.log(eventAttendanceMark);

const hasMarkedAttendance = await Event.hasUserAttended(
eventAttendanceMark.event_id,
eventAttendanceMark.user_address
);
if (hasMarkedAttendance) {
console.log("User has already marked attendance");
return;
}
await Event.markAttendance(
eventAttendanceMark.event_id,
eventAttendanceMark.user_address
);
}

export async function handleEndEventRegistration(event: starknet.IEvent) {
const data = event.data!;
const data = event.data!;

const endEventRegistration: EndEventRegistration = {
event_id: parseInt(uint256
const endEventRegistration: EndEventRegistration = {
event_id: parseInt(
uint256
.uint256ToBN({
low: FieldElement.toBigInt(data[0]),
high: FieldElement.toBigInt(data[1]),
})
.toString()),
event_name: hexToAscii(FieldElement.toHex(data[2]).toString()),
event_owner: FieldElement.toHex(data[3]).toString()
};

console.log(endEventRegistration);

const eventExists = await Event.findByEventId(endEventRegistration.event_id);
if (!eventExists) {
console.log("Event does not exist");
return;
}
await Event.endRegistration(endEventRegistration.event_id);
.toString()
),
event_name: hexToAscii(FieldElement.toHex(data[2]).toString()),
event_owner: FieldElement.toHex(data[3]).toString(),
};

console.log(endEventRegistration);

const eventExists = await Event.findByEventId(endEventRegistration.event_id);
if (!eventExists) {
console.log("Event does not exist");
return;
}
await Event.endRegistration(endEventRegistration.event_id);
}

export async function handleRSVPForEvent(event: starknet.IEvent) {
const data = event.data!;
const data = event.data!;

const rsvpForEvent: RSVPForEvent = {
event_id: parseInt(uint256
const rsvpForEvent: RSVPForEvent = {
event_id: parseInt(
uint256
.uint256ToBN({
low: FieldElement.toBigInt(data[0]),
high: FieldElement.toBigInt(data[1]),
})
.toString()),
attendee_address: FieldElement.toHex(data[2]).toString()
};

console.log(rsvpForEvent);

const hasRSVPed = await Event.hasUserRSVPed(rsvpForEvent.event_id, rsvpForEvent.attendee_address);
if (hasRSVPed) {
console.log("User has already RSVPed");
return;
}
await Event.addRSVP(rsvpForEvent.event_id, rsvpForEvent.attendee_address);
}
.toString()
),
attendee_address: FieldElement.toHex(data[2]).toString(),
};

console.log(rsvpForEvent);

const hasRSVPed = await Event.hasUserRSVPed(
rsvpForEvent.event_id,
rsvpForEvent.attendee_address
);
if (hasRSVPed) {
console.log("User has already RSVPed");
return;
}
await Event.addRSVP(rsvpForEvent.event_id, rsvpForEvent.attendee_address);
}
Loading

0 comments on commit 48919b0

Please sign in to comment.