Skip to content

Commit

Permalink
Create getActiveEvents method in Event service and its tests
Browse files Browse the repository at this point in the history
Created the getActiveEvents to fetch present events
  • Loading branch information
jeffplays2005 committed Sep 14, 2024
1 parent 346a959 commit 0f66dc6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
54 changes: 54 additions & 0 deletions NumbersApp.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Integer>(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();}
});
}
}
23 changes: 23 additions & 0 deletions server/src/data-layer/services/EventService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions server/src/data-layer/services/EventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event[]> {
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.
*
Expand Down

0 comments on commit 0f66dc6

Please sign in to comment.