-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Split into OrganizationalController (#44)
- Loading branch information
Showing
16 changed files
with
136 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
wrestling_scoreboard_server/lib/controllers/membership_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
wrestling_scoreboard_server/lib/controllers/organizational_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import 'package:postgres/postgres.dart' as psql; | ||
import 'package:wrestling_scoreboard_common/common.dart'; | ||
import 'package:wrestling_scoreboard_server/controllers/entity_controller.dart'; | ||
import 'package:wrestling_scoreboard_server/services/postgres_db.dart'; | ||
|
||
abstract class OrganizationalController<T extends Organizational> extends EntityController<T> { | ||
OrganizationalController({required super.tableName}); | ||
|
||
late Future<psql.Statement> getSingleOfOrgRawStmt; | ||
|
||
@override | ||
void init() { | ||
getSingleOfOrgRawStmt = PostgresDb().connection.prepare( | ||
psql.Sql.named('SELECT * FROM $tableName WHERE organization_id = @orgId AND org_sync_id = @orgSyncId;')); | ||
super.init(); | ||
} | ||
|
||
/// Get a single data object via a foreign id (sync id), given by an organization. | ||
Future<T> getSingleOfOrg(String orgSyncId, {required int orgId}) async { | ||
final single = await getSingleOfOrgRaw(orgSyncId, orgId: orgId); | ||
return DataObject.fromRaw<T>(single, EntityController.getSingleFromDataType); | ||
} | ||
|
||
Future<Map<String, dynamic>> getSingleOfOrgRaw(String orgSyncId, {required int orgId}) async { | ||
if (orgSyncId != orgSyncId.trim()) { | ||
orgSyncId = orgSyncId.trim(); | ||
print('$T with orgSyncId "$orgSyncId" was trimmed'); | ||
} | ||
final resStream = (await getSingleOfOrgRawStmt).bind({'orgSyncId': orgSyncId, 'orgId': orgId}); | ||
final many = await resStream.toColumnMap().toList(); | ||
if (many.isEmpty) throw InvalidParameterException('$T with orgSyncId "$orgSyncId" not found'); | ||
return many.first; | ||
} | ||
|
||
Future<T> getOrCreateSingleOfOrg(T dataObject) async { | ||
if (dataObject.id != null) { | ||
throw Exception('Data object already has an id: $dataObject'); | ||
} | ||
final organizational = (dataObject as Organizational); | ||
if (organizational.organization?.id == null || organizational.orgSyncId == null) { | ||
throw Exception('Organization id and sync id must not be null: $dataObject'); | ||
} | ||
try { | ||
final single = await getSingleOfOrg(organizational.orgSyncId!, orgId: organizational.organization!.id!); | ||
return single; | ||
} on InvalidParameterException catch (_) { | ||
return createSingleReturn(dataObject); | ||
} | ||
} | ||
|
||
Future<List<T>> getOrCreateManyOfOrg(List<T> dataObjects) async { | ||
return await Future.wait(dataObjects.map((element) => getOrCreateSingleOfOrg(element))); | ||
} | ||
|
||
static Future<T> getSingleFromDataTypeOfOrg<T extends Organizational>(String orgSyncId, {required int orgId}) { | ||
return (EntityController.getControllerFromDataType(T) as OrganizationalController<T>) | ||
.getSingleOfOrg(orgSyncId, orgId: orgId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
wrestling_scoreboard_server/lib/controllers/team_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.