Skip to content

Commit

Permalink
feat: Support Search (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Jun 17, 2024
1 parent 8f370a0 commit 50476f3
Show file tree
Hide file tree
Showing 26 changed files with 559 additions and 110 deletions.
1 change: 1 addition & 0 deletions wrestling_scoreboard_client/lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"display": "Anzeige",
"toggleFullscreen": "Vollbildschirm umschalten",
"favorite": "Favorit",
"favorites": "Favoriten",
"name": "Name",
"description": "Beschreibung",
"comment": "Kommentar",
Expand Down
1 change: 1 addition & 0 deletions wrestling_scoreboard_client/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"display": "Display",
"toggleFullscreen": "Toggle Fullscreen",
"favorite": "Favorite",
"favorites": "Favorites",
"name": "Name",
"description": "Description",
"comment": "Comment",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ class MockDataManager extends DataManager {
// TODO: implement organizationImport
throw UnimplementedError();
}

@override
Future<Map<String, List<DataObject>>> search({required String searchTerm, required Type? type}) {
// TODO: implement search
throw UnimplementedError();
}
}

class MockWebSocketManager implements WebSocketManager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ abstract class DataManager {

Future<void> organizationImport(int id, {AuthService? authService});

Future<Map<String, List<DataObject>>> search({required String searchTerm, required Type? type});

final Map<Type, StreamController<DataObject>> _singleStreamControllers = {};
final Map<Type, Map<Type, StreamController<ManyDataObject<dynamic>>>> _manyStreamControllers = {};
final Map<Type, StreamController<Map<String, dynamic>>> _singleRawStreamControllers = {};
Expand Down
46 changes: 46 additions & 0 deletions wrestling_scoreboard_client/lib/services/network/remote/rest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,52 @@ class RestDataManager extends DataManager {
throw RestException('Failed to import from organization $id', response: response);
}
}

@override
Future<Map<String, List<DataObject>>> search({required String searchTerm, required Type? type}) async {
final uri = Uri.parse('$_apiUrl/search').replace(queryParameters: {
if (type != null) 'type': getTableNameFromType(type),
if (searchTerm.isNotEmpty) 'like': searchTerm,
});
final response = await http.get(uri);
if (response.statusCode != 200) {
throw RestException('Failed to search $type with term "$searchTerm"', response: response);
}

final json = jsonDecode(response.body);
if (json is! List) {
throw RestException('Search $type with term "$searchTerm" should return a list', response: response);
}

final Map<String, List<DataObject>> result = {};

Future<int> handleSingle<T extends DataObject>({required CRUD operation, required T single}) async {
throw RestException('There should not be returned single data on a search', response: response);
}

Future<int> handleSingleRaw<T extends DataObject>(
{required CRUD operation, required Map<String, dynamic> single}) async {
throw RestException('There should not be returned single raw data on a search', response: response);
}

Future<void> handleManyRaw<T extends DataObject>(
{required CRUD operation, required ManyDataObject<Map<String, dynamic>> many}) async {
throw RestException('There should not be returned many raw data on a search', response: response);
}

for (final jsonType in json) {
await handleFromJson(
jsonType,
handleSingle: handleSingle,
handleMany: <T extends DataObject>({required CRUD operation, required ManyDataObject<T> many}) async {
result[getTableNameFromType(T)] = many.data;
},
handleSingleRaw: handleSingleRaw,
handleManyRaw: handleManyRaw,
);
}
return result;
}
}

class RestException implements Exception {
Expand Down
Loading

0 comments on commit 50476f3

Please sign in to comment.