From 0f66dc670854b22b4dd1d7084e114ed02b40986a Mon Sep 17 00:00:00 2001 From: jeffplays2005 Date: Sat, 14 Sep 2024 23:54:19 +1200 Subject: [PATCH] Create getActiveEvents method in Event service and its tests Created the getActiveEvents to fetch present events --- NumbersApp.java | 54 +++++++++++++++++++ .../data-layer/services/EventService.test.ts | 23 ++++++++ .../src/data-layer/services/EventService.ts | 16 ++++++ 3 files changed, 93 insertions(+) create mode 100644 NumbersApp.java diff --git a/NumbersApp.java b/NumbersApp.java new file mode 100644 index 000000000..78a205c5d --- /dev/null +++ b/NumbersApp.java @@ -0,0 +1,54 @@ +import java.awt.*; +import javax.swing.*; +import java.awt.event.*; +import java.awt.FlowLayout; +import javax.swing.event.*; +import java.util.ArrayList; +import java.util.Arrays; + +public class NumbersApp extends JFrame { + private JTextField factorField; + private JList list; + private JButton addButton; + private Integer[] numbers = new Integer[]{Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4)}; + public NumbersApp() { + factorField = new JTextField("3", 10); + list = new JList(numbers); + addButton = new JButton("Add"); + JPanel commandpanel = new JPanel(); + commandpanel.add(list); + commandpanel.add(factorField); + commandpanel.add(addButton); + getContentPane().add(commandpanel, BorderLayout.NORTH); + addButton.addActionListener( new AddFactorListener() ); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(600, 200); + setVisible(true); + } + //complete this + // We create a AddFactorListener class that implements ActionListener + class AddFactorListener implements ActionListener { + // This actionPerformed method runs when the button clicks + public void actionPerformed(ActionEvent e) { + // jijiku use try n catch because if not an integer, we dont do anything + try { + // parse the text field as an integer + int factor = Integer.parseInt(factorField.getText()); + // loop all numbers n try to add the factor + for(int i = 0; i < numbers.length; i++){ + numbers[i] += factor; + } + // update the java thingy + list.updateUI(); + } catch (NumberFormatException ex) { + // if not an integer, we set the text field to 3 + } + } + } + + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + public void run() {new NumbersApp();} + }); + } +} diff --git a/server/src/data-layer/services/EventService.test.ts b/server/src/data-layer/services/EventService.test.ts index 6842daed0..0a08db7dd 100644 --- a/server/src/data-layer/services/EventService.test.ts +++ b/server/src/data-layer/services/EventService.test.ts @@ -6,6 +6,7 @@ import { } from "data-layer/adapters/DateUtils" import { Event, EventReservation } from "data-layer/models/firebase" import FirestoreCollections from "data-layer/adapters/FirestoreCollections" +import { Timestamp } from "firebase-admin/firestore" const eventService = new EventService() @@ -73,6 +74,28 @@ describe("EventService integration tests", () => { }).toEqual(event1) }) + it("Should be able to get current existing events", async () => { + // Create past events + await eventService.createEvent(event1) + await eventService.createEvent(event2) + // Create a future event + const now = new Date(Date.now()) + const scheduledEvent: Event = { + title: "Scheduled event", + location: "Future event", + start_date: Timestamp.fromDate(new Date(now.getUTCFullYear() + 1, 1, 1)), + end_date: Timestamp.fromDate(new Date(now.getUTCFullYear() + 1, 1, 1)) + } + await eventService.createEvent(scheduledEvent) + + const futureEvents = await eventService.getActiveEvents() + + expect(futureEvents.length).toBe(1) + expect({ + ...futureEvents[0] + }).toEqual(scheduledEvent) + }) + it("Should be able to update an event", async () => { const newEvent = await eventService.createEvent(event1) diff --git a/server/src/data-layer/services/EventService.ts b/server/src/data-layer/services/EventService.ts index c0af72859..1e075de32 100644 --- a/server/src/data-layer/services/EventService.ts +++ b/server/src/data-layer/services/EventService.ts @@ -25,6 +25,22 @@ class EventService { return result.data() } + /** + * Fetches all events that have a start date in the future. + * Note that "active" means any event that is still upcoming, or "ready" to signup. + * + * @returns a list of events that have a start_date that is later to the current date. + */ + public async getActiveEvents(): Promise { + const now = new Date(Date.now()) + + const result = await FirestoreCollections.events + .where("start_date", ">=", now) + .get() + + return result.docs.map((doc) => doc.data() as Event) + } + /** * Updates an existing event document by ID with new Event data. *