Skip to content

Commit

Permalink
Merge branch 'xCk27x' of https://github.com/xCk27x/NCU-App into xCk27x
Browse files Browse the repository at this point in the history
  • Loading branch information
xCk27x committed Dec 21, 2024
2 parents b15d54a + 3b84979 commit 5f9a4c3
Show file tree
Hide file tree
Showing 21 changed files with 975 additions and 97 deletions.
File renamed without changes.
89 changes: 60 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"test": "playwright test"
},
"dependencies": {
"@heroicons/react": "^2.1.5",
"@supabase/supabase-js": "^2.45.3",
"@tanstack/react-router": "^1.56.2",
"flowbite-react-icons": "^1.1.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"supabase": "^1.200.3"
"supabase": "^1.223.10"
},
"devDependencies": {
"@playwright/test": "^1.47.0",
Expand Down
116 changes: 116 additions & 0 deletions src/backend/event/Controllers/EventController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import ErrorHandler from "../../../utils/ErrorHandler";
import { supabase } from "../../../utils/supabase";

import Event, { DBEvent } from '../Entities/Event';
import EventService from "../Services/EventService";


const EVENT_TABLE_NAME = "events"


export default class EventController {


/**
* Get an array of events
*
* @usage eventController.getEvents(<PARAMS>).then(
* (events: Array<Events>) => { ... }
* )
*
* @param {string} fields - The columns to retrieve (comma-separated)
* @param {string} orderBy - Which field to order by (leave blank if not needed)
* @param {boolean} orderDescending - Whether to order in descending order (defaults to false)
* @param {number} rangeStart - Starting index of fetch (defaults to 0)
* @param {number} rangeEnd - Ending index of fetch (defaults to 100)
*
* @returns {Array<Event>} - Array of events
*
* @see [https://supabase.com/docs/reference/javascript/order]
* @see [https://supabase.com/docs/reference/javascript/range]
*
* @author Henry C. (@yeahlowflicker)
*/
public async getEvents(
fields: string,
orderBy?: string,
orderDescending?: boolean,
rangeStart?: number,
rangeEnd?: number
) : Promise<Array<Event> | null> {

const query = supabase
.from(EVENT_TABLE_NAME)
.select(fields)
.returns<Array<DBEvent>>()

if (orderBy)
query.order(orderBy, { ascending: !orderDescending })

if (rangeStart !== undefined && rangeEnd !== undefined)
query.range(rangeStart, rangeEnd)

const { data, error } = await query

// Error handling
if (error) {
ErrorHandler.handleSupabaseError(error)
return null
}

// Initialize result array
const events : Array<Event> = []


// For each found DBEvent, convert to Event and append to result array
data.forEach((record: DBEvent) => {
events.push(
EventService.parseEvent(record)
)
})

return events
}



/**
* Find a single event by ID
*
* @usage eventController.FindEventByID(<PARAMS>).then(
* (event: Event) => { ... }
* )
*
* @param {string} eventID - Target event ID
* @param {string} fields - The columns to retrieve
*
* @returns {Event} - The target event entity (null if not found)
*
* @author Henry C. (@yeahlowflicker)
*/
public async findEventByID(eventID: string, fields?: string) : Promise<Event | null> {

const { data, error } = await supabase
.from(EVENT_TABLE_NAME)
.select(fields)
.eq("id", eventID)
.returns<DBEvent>()
.limit(1)
.single()

// Error handling
if (error) {
ErrorHandler.handleSupabaseError(error)
return null
}

if (!data)
return null

// Type conversion: DBEvent -> Event
const event : Event = EventService.parseEvent(data)

return event
}

}
22 changes: 22 additions & 0 deletions src/backend/event/Entities/Event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Database } from "../../../utils/database.types";

/**
* This is a dummy-type inherited from the generated Supabase type
*/
export type DBEvent = Database['public']['Tables']['events']['Row'];


export default class Event {

public id: number = 0;
public name: string = "";
public type: number = 0;
public description: string = "";
public startTime: string = "";
public endTime: string = "";
public location: string = "";
public fee: number = 0;
public userID: string = "";
public createdAt: string = "";

}
30 changes: 30 additions & 0 deletions src/backend/event/Services/EventService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Event, { DBEvent } from "../Entities/Event";

const EventService = {

parseEvent(record: DBEvent) : Event {
if (!record || typeof record !== 'object')
throw new Error('Invalid record provided')

if (!record.id)
throw new Error('id is a required field')

const event = new Event()

event.id = record.id
event.name = record.name ?? ""
event.type = typeof record.type === 'number' ? record.type : 0
event.description = record.description ?? ""
event.startTime = record.start_time ? new Date(record.start_time).toISOString() : ""
event.endTime = record.end_time ? new Date(record.end_time).toISOString() : ""
event.location = record.location ?? ""
event.fee = typeof record.fee === 'number' ? record.fee : 0
event.userID = record.user_id
event.createdAt = record.created_at ? new Date(record.created_at).toISOString() : ""

return event
}

}

export default EventService
Loading

0 comments on commit 5f9a4c3

Please sign in to comment.