1+ import ErrorHandler from "../../../utils/ErrorHandler" ;
2+ import { supabase } from "../../../utils/supabase" ;
3+
4+ import Restaurant , { DBRestaurant } from '../Entities/Restaurant' ;
5+ import RestaurantService from '../Services/RestaurantService' ;
6+
7+
8+ const RESTAURANT_TABLE_NAME = "restaurants"
9+
10+
11+ export default class RestaurantController {
12+
13+ /**
14+ * Get an array of restaurants
15+ *
16+ * @usage restaurantConroller.getAllRestaurants(<PARAMS>).then(
17+ * (users: Array<Restaurant>) => { ... }
18+ * )
19+ *
20+ * @param {string } fields - The columns to retrieve (comma-separated)
21+ * @param {string } orderBy - Which field to order by (leave blank if not needed)
22+ * @param {boolean } orderDescending - Whether to order in descending order (defaults to false)
23+ *
24+ * @returns {Array<Restaurant> } - Array of restaurants
25+ *
26+ * @see [https://supabase.com/docs/reference/javascript/order]
27+ * @see [https://supabase.com/docs/reference/javascript/range]
28+ *
29+ * @author Susan Chen. (@1989ONCE)
30+ */
31+ public async getAllRestaurants (
32+ fields : string ,
33+ orderBy ?: string ,
34+ orderDescending ?: boolean ,
35+ ) : Promise < Array < Restaurant > | null > {
36+
37+ const query = supabase
38+ . from ( RESTAURANT_TABLE_NAME )
39+ . select ( fields )
40+ . returns < Array < DBRestaurant > > ( )
41+
42+ if ( orderBy )
43+ query . order ( orderBy , { ascending : ! orderDescending } )
44+
45+ const { data, error } = await query
46+
47+ // Error handling
48+ if ( error ) {
49+ ErrorHandler . handleSupabaseError ( error )
50+ return null
51+ }
52+
53+ // Initialize result array
54+ const restaurants : Array < Restaurant > = [ ]
55+
56+ // For each found DBRestaurant, convert to Restaurant and append to result array
57+ data . forEach ( ( record : DBRestaurant ) => {
58+ restaurants . push (
59+ RestaurantService . parseRestaurant ( record )
60+ )
61+ } )
62+
63+ return restaurants
64+ }
65+
66+
67+
68+ /**
69+ * Find a single restaurant by ID
70+ *
71+ * @usage restaurantController.findRestaurantByID(<PARAMS>).then(
72+ * (restaurant: Restaurant) => { ... }
73+ * )
74+ *
75+ * @param {string } restaurantID - Target restaurant ID
76+ * @param {string } fields - The columns to retrieve
77+ *
78+ * @returns {User } - The target user entity (null if not found)
79+ *
80+ * @author Susan Chen. (@1989ONCE)
81+ */
82+ public async findRestaurantByID ( restaurantID : string , fields ?: string ) : Promise < Restaurant | null > {
83+
84+ const { data, error } = await supabase
85+ . from ( RESTAURANT_TABLE_NAME )
86+ . select ( fields )
87+ . eq ( "id" , restaurantID )
88+ . returns < DBRestaurant > ( )
89+ . limit ( 1 )
90+ . single ( )
91+
92+
93+ // Error handling
94+ if ( error ) {
95+ ErrorHandler . handleSupabaseError ( error )
96+ return null
97+ }
98+
99+ if ( ! data )
100+ return null
101+
102+ // Type conversion: DBUser -> User
103+ const restaurant : Restaurant = RestaurantService . parseRestaurant ( data )
104+
105+ return restaurant
106+ }
107+
108+
109+ }
0 commit comments