-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'package:nyxx/nyxx.dart'; | ||
|
||
class KavitaUserConfig { | ||
static String tableName = 'kavita_user_configs'; | ||
|
||
final Snowflake userId; | ||
final String authToken; | ||
final String apiKey; | ||
final int kavitaConfigId; | ||
|
||
KavitaConfig? config; | ||
int? id; | ||
|
||
KavitaUserConfig( | ||
{required this.userId, required this.authToken, required this.apiKey, required this.kavitaConfigId, this.id}); | ||
|
||
factory KavitaUserConfig.fromDatabaseRow(Map<String, dynamic> row) { | ||
return KavitaUserConfig( | ||
userId: Snowflake.parse(row['user_id']), | ||
authToken: row['auth_token'], | ||
apiKey: row['api_key'], | ||
kavitaConfigId: row['kavita_config_id'], | ||
id: row['id'], | ||
); | ||
} | ||
|
||
factory KavitaUserConfig.fromDatabaseRowWithConfig(Map<String, dynamic> row) { | ||
return KavitaUserConfig.fromDatabaseRow(row)..config = KavitaConfig.fromDatabaseRow(row); | ||
} | ||
} | ||
|
||
class KavitaConfig { | ||
static String tableName = 'kavita_configs'; | ||
|
||
final String name; | ||
final String basePath; | ||
final bool isDefault; | ||
final Snowflake parentId; | ||
|
||
/// The ID of this config, or `null` if this config has not yet been added to the database. | ||
int? id; | ||
|
||
KavitaConfig({ | ||
required this.name, | ||
required this.basePath, | ||
required this.isDefault, | ||
required this.parentId, | ||
this.id, | ||
}); | ||
|
||
factory KavitaConfig.fromDatabaseRow(Map<String, dynamic> row) { | ||
return KavitaConfig( | ||
id: row['id'] as int?, | ||
name: row['name'], | ||
basePath: row['base_path'], | ||
isDefault: row['is_default'] as bool, | ||
parentId: Snowflake.parse(row['parent_id']), | ||
); | ||
} | ||
} |
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,36 @@ | ||
import 'package:injector/injector.dart'; | ||
import 'package:running_on_dart/src/models/kavita.dart'; | ||
import 'package:running_on_dart/src/services/db.dart'; | ||
import 'package:running_on_dart/src/util/query_builder.dart'; | ||
|
||
class KavitaRepository { | ||
final DatabaseService _database = Injector.appInstance.get(); | ||
|
||
Future<Iterable<KavitaConfig>> findAllForParent(String parentId) async { | ||
final query = SelectQuery.selectAll(KavitaConfig.tableName)..andWhere("parent_id = @parent_id"); | ||
|
||
final result = await _database.executeQuery(query, parameters: {'parent_id': parentId}); | ||
|
||
return result.map((row) => KavitaConfig.fromDatabaseRow(row.toColumnMap())); | ||
} | ||
|
||
Future<KavitaUserConfig?> findUserConfig(String configName, String parentId, String userId) async { | ||
final query = SelectQuery.selectAll(KavitaConfig.tableName, alias: "c") | ||
..select("uc.*") | ||
..addJoin(KavitaUserConfig.tableName, "uc", ["uc.kavita_config_id = c.id", "uc.user_id = @userId"]) | ||
..andWhere("c.name = @configName") | ||
..andWhere("c.parent_id = @parentId"); | ||
|
||
final result = await _database.executeQuery(query, parameters: { | ||
'configName': configName, | ||
'parentId': parentId, | ||
'userId': userId, | ||
}); | ||
|
||
if (result.isEmpty) { | ||
return null; | ||
} | ||
|
||
return KavitaUserConfig.fromDatabaseRowWithConfig(result.first.toColumnMap()); | ||
} | ||
} |
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