Skip to content

Commit

Permalink
fix: Improve database imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Sep 9, 2024
1 parent f64f9e9 commit a60d361
Show file tree
Hide file tree
Showing 22 changed files with 255 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,12 @@ class MockDataManager extends DataManager {
throw UnimplementedError();
}

@override
Future<void> organizationTeamMatchImport(int id, {AuthService? authService}) {
// TODO: implement organizationLeagueImport
throw UnimplementedError();
}

@override
Future<void> organizationCompetitionImport(int id, {AuthService? authService}) {
// TODO: implement organizationCompetitionImport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ abstract class DataManager implements AuthManager {

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

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

Future<Map<String, List<DataObject>>> search({
required String searchTerm,
Type? type,
Expand Down
15 changes: 15 additions & 0 deletions wrestling_scoreboard_client/lib/services/network/remote/rest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ class RestDataManager extends DataManager {
}
}

@override
Future<void> organizationTeamMatchImport(int id, {AuthService? authService}) async {
String? body;
if (authService != null) {
if (authService is BasicAuthService) {
body = jsonEncode(singleToJson(authService, BasicAuthService, CRUD.update));
}
}
final uri = Uri.parse('$_apiUrl/team_match/$id/api/import');
final response = await http.post(uri, body: body, headers: _headers);
if (response.statusCode != 200) {
throw RestException('Failed to import from team_match $id', response: response);
}
}

@override
Future<void> organizationCompetitionImport(int id, {AuthService? authService}) async {
String? body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class OrganizationImportAction extends ConsumerWidget {
await dataManager.organizationLeagueImport(id, authService: authService);
case OrganizationImportType.competition:
await dataManager.organizationCompetitionImport(id, authService: authService);
case OrganizationImportType.teamMatch:
await dataManager.organizationTeamMatchImport(id, authService: authService);
}
if (context.mounted) {
await showOkDialog(context: context, child: Text(localizations.actionSuccessful));
Expand All @@ -60,4 +62,5 @@ enum OrganizationImportType {
team,
league,
competition,
teamMatch,
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:wrestling_scoreboard_client/view/screens/edit/lineup_edit.dart';
import 'package:wrestling_scoreboard_client/view/screens/edit/team_match/team_match_bout_edit.dart';
import 'package:wrestling_scoreboard_client/view/screens/edit/team_match/team_match_edit.dart';
import 'package:wrestling_scoreboard_client/view/screens/overview/common.dart';
import 'package:wrestling_scoreboard_client/view/screens/overview/shared/actions.dart';
import 'package:wrestling_scoreboard_client/view/screens/overview/team_match/team_match_bout_overview.dart';
import 'package:wrestling_scoreboard_client/view/widgets/auth.dart';
import 'package:wrestling_scoreboard_client/view/widgets/consumer.dart';
Expand Down Expand Up @@ -74,6 +75,8 @@ class TeamMatchOverview extends ConsumerWidget {
label: localizations.match,
details: '${match.home.team.name} - ${match.guest.team.name}',
actions: [
OrganizationImportAction(
id: id, orgId: match.organization!.id!, importType: OrganizationImportType.teamMatch),
// TODO: replace with file_save when https://github.com/flutter/flutter/issues/102560 is merged, also replace in settings.
IconButton(
onPressed: () async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mixin _$Membership {
int? get id => throw _privateConstructorUsedError;
String? get orgSyncId => throw _privateConstructorUsedError;
Organization? get organization => throw _privateConstructorUsedError;
String? get no => throw _privateConstructorUsedError; // Vereinsnummer
String? get no => throw _privateConstructorUsedError; // Mitgliedsnummer
Club get club => throw _privateConstructorUsedError;
Person get person => throw _privateConstructorUsedError;

Expand Down Expand Up @@ -211,7 +211,7 @@ class _$MembershipImpl extends _Membership {
final Organization? organization;
@override
final String? no;
// Vereinsnummer
// Mitgliedsnummer
@override
final Club club;
@override
Expand Down Expand Up @@ -274,7 +274,7 @@ abstract class _Membership extends Membership {
@override
Organization? get organization;
@override
String? get no; // Vereinsnummer
String? get no; // Mitgliedsnummer
@override
Club get club;
@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ part 'team_match_bout.freezed.dart';
part 'team_match_bout.g.dart';

@freezed
class TeamMatchBout with _$TeamMatchBout implements DataObject {
class TeamMatchBout with _$TeamMatchBout implements DataObject, Organizational {
const TeamMatchBout._();

const factory TeamMatchBout({
int? id,
String? orgSyncId,
Organization? organization,
required int pos,
required TeamMatch teamMatch,
required Bout bout,
Expand All @@ -21,9 +23,12 @@ class TeamMatchBout with _$TeamMatchBout implements DataObject {
static Future<TeamMatchBout> fromRaw(Map<String, dynamic> e, GetSingleOfTypeCallback getSingle) async {
final teamMatch = await getSingle<TeamMatch>(e['team_match_id'] as int);
final bout = await getSingle<Bout>(e['bout_id'] as int);
final organizationId = e['organization_id'] as int?;

return TeamMatchBout(
id: e['id'] as int?,
orgSyncId: e['org_sync_id'] as String?,
organization: organizationId == null ? null : await getSingle<Organization>(organizationId),
teamMatch: teamMatch,
bout: bout,
pos: e['pos'],
Expand All @@ -34,6 +39,8 @@ class TeamMatchBout with _$TeamMatchBout implements DataObject {
Map<String, dynamic> toRaw() {
return {
if (id != null) 'id': id,
if (orgSyncId != null) 'org_sync_id': orgSyncId,
if (organization != null) 'organization_id': organization?.id!,
'pos': pos,
'team_match_id': teamMatch.id!,
'bout_id': bout.id!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ TeamMatchBout _$TeamMatchBoutFromJson(Map<String, dynamic> json) {
/// @nodoc
mixin _$TeamMatchBout {
int? get id => throw _privateConstructorUsedError;
String? get orgSyncId => throw _privateConstructorUsedError;
Organization? get organization => throw _privateConstructorUsedError;
int get pos => throw _privateConstructorUsedError;
TeamMatch get teamMatch => throw _privateConstructorUsedError;
Bout get bout => throw _privateConstructorUsedError;
Expand All @@ -39,8 +41,9 @@ abstract class $TeamMatchBoutCopyWith<$Res> {
factory $TeamMatchBoutCopyWith(TeamMatchBout value, $Res Function(TeamMatchBout) then) =
_$TeamMatchBoutCopyWithImpl<$Res, TeamMatchBout>;
@useResult
$Res call({int? id, int pos, TeamMatch teamMatch, Bout bout});
$Res call({int? id, String? orgSyncId, Organization? organization, int pos, TeamMatch teamMatch, Bout bout});

$OrganizationCopyWith<$Res>? get organization;
$TeamMatchCopyWith<$Res> get teamMatch;
$BoutCopyWith<$Res> get bout;
}
Expand All @@ -60,6 +63,8 @@ class _$TeamMatchBoutCopyWithImpl<$Res, $Val extends TeamMatchBout> implements $
@override
$Res call({
Object? id = freezed,
Object? orgSyncId = freezed,
Object? organization = freezed,
Object? pos = null,
Object? teamMatch = null,
Object? bout = null,
Expand All @@ -69,6 +74,14 @@ class _$TeamMatchBoutCopyWithImpl<$Res, $Val extends TeamMatchBout> implements $
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as int?,
orgSyncId: freezed == orgSyncId
? _value.orgSyncId
: orgSyncId // ignore: cast_nullable_to_non_nullable
as String?,
organization: freezed == organization
? _value.organization
: organization // ignore: cast_nullable_to_non_nullable
as Organization?,
pos: null == pos
? _value.pos
: pos // ignore: cast_nullable_to_non_nullable
Expand All @@ -84,6 +97,20 @@ class _$TeamMatchBoutCopyWithImpl<$Res, $Val extends TeamMatchBout> implements $
) as $Val);
}

/// Create a copy of TeamMatchBout
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$OrganizationCopyWith<$Res>? get organization {
if (_value.organization == null) {
return null;
}

return $OrganizationCopyWith<$Res>(_value.organization!, (value) {
return _then(_value.copyWith(organization: value) as $Val);
});
}

/// Create a copy of TeamMatchBout
/// with the given fields replaced by the non-null parameter values.
@override
Expand Down Expand Up @@ -111,8 +138,10 @@ abstract class _$$TeamMatchBoutImplCopyWith<$Res> implements $TeamMatchBoutCopyW
__$$TeamMatchBoutImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({int? id, int pos, TeamMatch teamMatch, Bout bout});
$Res call({int? id, String? orgSyncId, Organization? organization, int pos, TeamMatch teamMatch, Bout bout});

@override
$OrganizationCopyWith<$Res>? get organization;
@override
$TeamMatchCopyWith<$Res> get teamMatch;
@override
Expand All @@ -131,6 +160,8 @@ class __$$TeamMatchBoutImplCopyWithImpl<$Res> extends _$TeamMatchBoutCopyWithImp
@override
$Res call({
Object? id = freezed,
Object? orgSyncId = freezed,
Object? organization = freezed,
Object? pos = null,
Object? teamMatch = null,
Object? bout = null,
Expand All @@ -140,6 +171,14 @@ class __$$TeamMatchBoutImplCopyWithImpl<$Res> extends _$TeamMatchBoutCopyWithImp
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as int?,
orgSyncId: freezed == orgSyncId
? _value.orgSyncId
: orgSyncId // ignore: cast_nullable_to_non_nullable
as String?,
organization: freezed == organization
? _value.organization
: organization // ignore: cast_nullable_to_non_nullable
as Organization?,
pos: null == pos
? _value.pos
: pos // ignore: cast_nullable_to_non_nullable
Expand All @@ -159,13 +198,19 @@ class __$$TeamMatchBoutImplCopyWithImpl<$Res> extends _$TeamMatchBoutCopyWithImp
/// @nodoc
@JsonSerializable()
class _$TeamMatchBoutImpl extends _TeamMatchBout {
const _$TeamMatchBoutImpl({this.id, required this.pos, required this.teamMatch, required this.bout}) : super._();
const _$TeamMatchBoutImpl(
{this.id, this.orgSyncId, this.organization, required this.pos, required this.teamMatch, required this.bout})
: super._();

factory _$TeamMatchBoutImpl.fromJson(Map<String, dynamic> json) => _$$TeamMatchBoutImplFromJson(json);

@override
final int? id;
@override
final String? orgSyncId;
@override
final Organization? organization;
@override
final int pos;
@override
final TeamMatch teamMatch;
Expand All @@ -174,7 +219,7 @@ class _$TeamMatchBoutImpl extends _TeamMatchBout {

@override
String toString() {
return 'TeamMatchBout(id: $id, pos: $pos, teamMatch: $teamMatch, bout: $bout)';
return 'TeamMatchBout(id: $id, orgSyncId: $orgSyncId, organization: $organization, pos: $pos, teamMatch: $teamMatch, bout: $bout)';
}

@override
Expand All @@ -183,14 +228,16 @@ class _$TeamMatchBoutImpl extends _TeamMatchBout {
(other.runtimeType == runtimeType &&
other is _$TeamMatchBoutImpl &&
(identical(other.id, id) || other.id == id) &&
(identical(other.orgSyncId, orgSyncId) || other.orgSyncId == orgSyncId) &&
(identical(other.organization, organization) || other.organization == organization) &&
(identical(other.pos, pos) || other.pos == pos) &&
(identical(other.teamMatch, teamMatch) || other.teamMatch == teamMatch) &&
(identical(other.bout, bout) || other.bout == bout));
}

@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType, id, pos, teamMatch, bout);
int get hashCode => Object.hash(runtimeType, id, orgSyncId, organization, pos, teamMatch, bout);

/// Create a copy of TeamMatchBout
/// with the given fields replaced by the non-null parameter values.
Expand All @@ -211,6 +258,8 @@ class _$TeamMatchBoutImpl extends _TeamMatchBout {
abstract class _TeamMatchBout extends TeamMatchBout {
const factory _TeamMatchBout(
{final int? id,
final String? orgSyncId,
final Organization? organization,
required final int pos,
required final TeamMatch teamMatch,
required final Bout bout}) = _$TeamMatchBoutImpl;
Expand All @@ -221,6 +270,10 @@ abstract class _TeamMatchBout extends TeamMatchBout {
@override
int? get id;
@override
String? get orgSyncId;
@override
Organization? get organization;
@override
int get pos;
@override
TeamMatch get teamMatch;
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ class ByGermanyWrestlingApi extends WrestlingApi {
final boutDuration = boutDurationSeconds == null ? Duration.zero : Duration(seconds: boutDurationSeconds);

var bout = Bout(
orgSyncId: '${event.orgSyncId}_${weightClass.name.replaceAll(' ', '_')}',
orgSyncId: '${event.orgSyncId}_${weightClass.name}_${weightClass.style.name}'.replaceAll(' ', '_'),
organization: organization,
duration: boutDuration,
weightClass: weightClass,
Expand Down
2 changes: 1 addition & 1 deletion wrestling_scoreboard_common/lib/src/util/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Future<T> runSynchronized<T>({
required String key,
bool Function()? canAbort,
required Future<T> Function() runAsync,
Duration timeout = const Duration(seconds: 30),
Duration timeout = const Duration(seconds: 60),
}) async {
Completer? completer;
try {
Expand Down
2 changes: 1 addition & 1 deletion wrestling_scoreboard_common/test/services/apis_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ void main() {
// TODO: Evaluate this bout: https://www.brv-ringen.de/index.php?option=com_rdb&view=rdb&Itemid=512&tk=cs&sid=2023&yid=M&menu=1&op=lc&lid=Bayernliga&cntl=Ergebnisse&from=ll&cid=005029c
// Why activity and passivity at different times, or points at different times?
final expectedBout = Bout(
orgSyncId: '005029c_61_kg',
orgSyncId: '005029c_61_kg_free',
duration: Duration(minutes: 6),
result: BoutResult.vpo,
weightClass: weightClass,
Expand Down
Loading

0 comments on commit a60d361

Please sign in to comment.