Skip to content

Commit

Permalink
Create events.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent 2b7c90e commit 1b95adc
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/community/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// events.js

class Events {
constructor() {
this.eventsList = []; // Store community events
}

// Create a new event
createEvent(title, description, date, location) {
const event = {
id: this.eventsList.length + 1,
title,
description,
date,
location,
attendees: [],
};
this.eventsList.push(event);
console.log(`Event created:`, event);
return event;
}

// Get all events
getAllEvents() {
return this.eventsList;
}

// Register a user for an event
registerForEvent(eventId, userId) {
const event = this.eventsList.find(e => e.id === eventId);
if (event) {
if (!event.attendees.includes(userId)) {
event.attendees.push(userId);
console.log(`User ${userId} registered for event:`, event.title);
return event;
} else {
throw new Error('User is already registered for this event.');
}
} else {
throw new Error('Event not found.');
}
}

// Get attendees for an event
getEventAttendees(eventId) {
const event = this.eventsList.find(e => e.id === eventId);
if (event) {
return event.attendees;
} else {
throw new Error('Event not found.');
}
}
}

// Example usage
const eventsManager = new Events();
const newEvent = eventsManager.createEvent('Webinar on User Experience', 'Join us for a discussion on improving user experience.', '2023-10-15', 'Online');
eventsManager.createEvent('Community Meetup', 'A chance to meet and network with other users.', '2023-11-01', 'Local Park');

const allEvents = eventsManager.getAllEvents();
console.log('All Community Events:', allEvents);

eventsManager.registerForEvent(newEvent.id, 'user123');
const attendees = eventsManager.getEventAttendees(newEvent.id);
console.log('Attendees for event:', attendees);

export default Events;

0 comments on commit 1b95adc

Please sign in to comment.