diff --git a/api/bin/migrate.dart b/api/bin/migrate.dart index 44584a5..b9fb36a 100644 --- a/api/bin/migrate.dart +++ b/api/bin/migrate.dart @@ -33,6 +33,7 @@ void main(List args) async { SchemeMigration(), DownloadHistoryMigration(), LikeRecordMigration(), + AppVersionMigration(), ]); await runMigrations(migrationRunner, args); } @@ -64,3 +65,30 @@ Future doUserSeed() async { } return connection.close(); } + +class AppVersionSeed extends Migration { + @override + void up(Schema schema) async { + await doAppVersionSeed(); + } + + @override + void down(Schema schema) async {} +} + +Future doAppVersionSeed() async { + var connection = await connectToPostgres(configuration); + await connection.open(); + var executor = PostgreSqlExecutor(connection); + var appVersionQuery = AppVersionQuery(); + var one = await appVersionQuery.getOne(executor); + if (one.isEmpty) { + appVersionQuery = AppVersionQuery(); + appVersionQuery.values.copyFrom(AppVersion( + versionCode: 1, + versionName: '1.0.0', + )); + return appVersionQuery.insert(executor).then((value) => connection.close()); + } + return connection.close(); +} diff --git a/api/lib/src/models/app_version.dart b/api/lib/src/models/app_version.dart index e52b2b3..dc71cf4 100644 --- a/api/lib/src/models/app_version.dart +++ b/api/lib/src/models/app_version.dart @@ -1,8 +1,25 @@ +import 'package:angel3_orm/angel3_orm.dart'; import 'package:angel3_serialize/angel3_serialize.dart'; +import 'package:dde_gesture_manager_api/src/models/base_model.dart'; +import 'package:angel3_migration/angel3_migration.dart'; +import 'package:optional/optional.dart'; part 'app_version.g.dart'; @serializable -abstract class _AppVersion { +@orm +abstract class _AppVersion extends BaseModel { + @SerializableField(isNullable: false) + @Column(isNullable: false) + String? get versionName; + + @SerializableField(isNullable: false) + @Column(isNullable: false) + int? get versionCode; +} + +@serializable +@Orm(tableName: 'app_versions', generateMigrations: false) +abstract class _AppVersionResp { @SerializableField(isNullable: false) String? get versionName; diff --git a/api/lib/src/routes/controllers/system_controllers.dart b/api/lib/src/routes/controllers/system_controllers.dart index 9ff1c39..3c80c48 100644 --- a/api/lib/src/routes/controllers/system_controllers.dart +++ b/api/lib/src/routes/controllers/system_controllers.dart @@ -3,24 +3,18 @@ import 'dart:async'; import 'package:angel3_framework/angel3_framework.dart'; import 'package:dde_gesture_manager_api/apis.dart'; import 'package:dde_gesture_manager_api/src/models/app_version.dart'; -import 'package:file/file.dart'; -import 'package:yaml/yaml.dart'; - -late FileSystem fs; +import 'controller_extensions.dart'; Future configureServer(Angel app) async { app.get( Apis.system.appVersion, (req, res) async { - var pubspec = fs.currentDirectory.parent.childDirectory('app').childFile('pubspec.yaml').readAsStringSync(); - var version = loadYaml(pubspec)['version'] as String; - var versions = version.split('+'); - return res.json(AppVersion(versionName: versions.first, versionCode: int.parse(versions.last))); + var appVersionQuery = AppVersionQuery(); + appVersionQuery.orderBy(AppVersionFields.versionCode, descending: true); + return appVersionQuery.getOne(req.queryExecutor).then((value) => AppVersionResp( + versionName: value.value.versionName, + versionCode: value.value.versionCode, + )); }, ); } - -configureServerWithFileSystem(FileSystem fileSystem) { - fs = fileSystem; - return configureServer; -} diff --git a/api/lib/src/routes/routes.dart b/api/lib/src/routes/routes.dart index 83f4a8e..ef4df2b 100644 --- a/api/lib/src/routes/routes.dart +++ b/api/lib/src/routes/routes.dart @@ -21,7 +21,7 @@ AngelConfigurer configureServer(FileSystem fileSystem) { }); // Typically, you want to mount controllers first, after any global middleware. - await app.configure(system_controllers.configureServerWithFileSystem(fileSystem)); + await app.configure(system_controllers.configureServer); await app.configure(auth_controllers.configureServer); await app.configure(scheme_controllers.configureServer); diff --git a/api/pubspec.yaml b/api/pubspec.yaml index 2b58d05..e7f991c 100644 --- a/api/pubspec.yaml +++ b/api/pubspec.yaml @@ -16,7 +16,6 @@ dependencies: belatuk_pretty_logging: ^4.0.0 optional: ^6.0.0 logging: ^1.0.0 - yaml: ^3.1.0 mailer: ^5.0.2 uuid: ^3.0.5 neat_cache: