Skip to content

Commit

Permalink
Add option group module
Browse files Browse the repository at this point in the history
  • Loading branch information
BaharaJr committed Jun 5, 2024
1 parent 5cbc627 commit 2ae6aee
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/d2_touch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:d2_touch/modules/engine/engine.module.dart';
import 'package:d2_touch/modules/engine/http/http.module.dart';
import 'package:d2_touch/modules/file_resource/file_resource.module.dart';
import 'package:d2_touch/modules/metadata/dataset/data_set.module.dart';
import 'package:d2_touch/modules/metadata/option_group/option_group.module.dart';
import 'package:d2_touch/modules/metadata/option_set/option_set.module.dart';
import 'package:d2_touch/modules/metadata/organisation_unit/organisation_unit.module.dart';
import 'package:d2_touch/modules/metadata/program/program.module.dart';
Expand All @@ -31,7 +32,7 @@ import 'modules/metadata/dashboard/dashboard.module.dart';
import 'modules/metadata/data_element/data_element.module.dart';

class D2Touch implements D2TouchModel {
int? instanceVersion ;
int? instanceVersion;
String locale = 'en';
Database? _database;
bool? inMemory;
Expand All @@ -48,6 +49,8 @@ class D2Touch implements D2TouchModel {
DataElementModule(database: _database);

ProgramModule get programModule => ProgramModule(database: _database);
OptionGroupModule get optionGroupModule =>
OptionGroupModule(database: _database);

DataSetModule get dataSetModule => DataSetModule(database: _database);

Expand Down Expand Up @@ -84,7 +87,6 @@ class D2Touch implements D2TouchModel {
DatabaseFactory? databaseFactory,
SharedPreferences? sharedPreferenceInstance,
}) async {

if (_d2Instance == null) {
_d2Instance = D2Touch._internal();

Expand Down Expand Up @@ -151,6 +153,7 @@ class D2Touch implements D2TouchModel {
await DataElementModule.createTables(database: database);
await DataSetModule.createTables(database: database);
await ProgramModule.createTables(database: database);
await OptionGroupModule.createTables(database: database);
await DashboardModule.createTables(database: database);
await TrackedEntityInstanceModule.createTables(database: database);
await AggregateModule.createTables(database: database);
Expand Down Expand Up @@ -184,6 +187,7 @@ class D2Touch implements D2TouchModel {
await DataElementModule.createTables(database: database);
await DataSetModule.createTables(database: database);
await ProgramModule.createTables(database: database);
await OptionGroupModule.createTables(database: database);
await DashboardModule.createTables(database: database);
await TrackedEntityInstanceModule.createTables(database: database);
await AggregateModule.createTables(database: database);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'dart:convert';

import 'package:d2_touch/core/annotations/index.dart';
import 'package:d2_touch/shared/entities/identifiable.entity.dart';

@AnnotationReflectable
@Entity(tableName: 'optiongroup', apiResourceName: 'optionGroups')
class OptionGroup extends IdentifiableEntity {
@Column(type: ColumnType.TEXT)
String options;

OptionGroup({
required String id,
required String name,
required bool dirty,
required this.options,
}) : super(id: id, name: name, dirty: dirty);

factory OptionGroup.fromJson(Map<String, dynamic> jsonData) {
return OptionGroup(
id: jsonData['id'],
name: jsonData['name'],
dirty: jsonData['dirty'],
options: jsonEncode(jsonData['options']),
);
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
data['dirty'] = this.dirty;
data['options'] = this.options;
return data;
}
}
12 changes: 12 additions & 0 deletions lib/modules/metadata/option_group/option_group.module.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:d2_touch/modules/metadata/option_group/queries/option_group.query.dart';
import 'package:sqflite/sqflite.dart';

class OptionGroupModule {
Database? database;
OptionGroupModule({this.database, String? locale});
static createTables({required Database database}) async {
await OptionGroupQuery(database: database).createTable();
}

OptionGroupQuery get optionGroup => OptionGroupQuery(database: database);
}
22 changes: 22 additions & 0 deletions lib/modules/metadata/option_group/queries/option_group.query.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:d2_touch/modules/metadata/option_group/entities/option_group.entity.dart';
import 'package:d2_touch/shared/queries/base.query.dart';
import 'package:d2_touch/shared/utilities/query_filter.util.dart';
import 'package:sqflite/sqflite.dart';

class OptionGroupQuery extends BaseQuery<OptionGroup> {
OptionGroupQuery({Database? database}) : super(database: database);

@override
Future<String> dhisUrl() {
final apiFilter =
QueryFilter.getApiFilters(this.repository.columns, this.filters);

if ((this.selected).isNotEmpty) {
return Future.value(
'optionGroups.json${apiFilter != null ? '?$apiFilter&' : '?'}fields=${this.selected.join(',')}&paging=false');
}

return Future.value(
'optionGroups.json${apiFilter != null ? '?$apiFilter&' : '?'}fields=id,name,shortName,options[code,name]&paging=false');
}
}

0 comments on commit 2ae6aee

Please sign in to comment.