Skip to content

Commit

Permalink
feature: Kavita
Browse files Browse the repository at this point in the history
  • Loading branch information
l7ssha committed Nov 11, 2024
1 parent dbe7565 commit b16f546
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ services:
image: postgres:17
container_name: running_on_dart_db
restart: always
ports:
- "13323:5432"
env_file:
- .env
volumes:
Expand Down
60 changes: 60 additions & 0 deletions lib/src/models/kavita.dart
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']),
);
}
}
36 changes: 36 additions & 0 deletions lib/src/repository/kavita.dart
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());
}
}
31 changes: 30 additions & 1 deletion lib/src/services/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,36 @@ class DatabaseService implements RequiresInitialization {
..enqueueMigration("2.11",
'CREATE UNIQUE INDEX idx_jellyfin_configs_user_id ON jellyfin_user_configs(user_id, jellyfin_config_id);')
..enqueueMigration("2.12",
'ALTER TABLE jellyfin_user_configs ADD CONSTRAINT jellyfin_configs_user_id_unique UNIQUE (user_id, jellyfin_config_id);');
'ALTER TABLE jellyfin_user_configs ADD CONSTRAINT jellyfin_configs_user_id_unique UNIQUE (user_id, jellyfin_config_id);')
..enqueueMigration("2.13", '''
CREATE TABLE kavita_configs (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
base_path VARCHAR NOT NULL,
is_default BOOLEAN NOT NULL DEFAULT FALSE,
parent_id VARCHAR NOT NULL
);
''')
..enqueueMigration(
"2.14", 'CREATE UNIQUE INDEX idx_kavita_configs_unique_name ON kavita_configs(name, parent_id);')
..enqueueMigration("2.15",
'CREATE UNIQUE INDEX idx_kavita_configs_unique_default ON kavita_configs(parent_id, is_default) WHERE is_default = TRUE;')
..enqueueMigration("2.16", '''
CREATE TABLE kavita_user_configs (
id SERIAL PRIMARY KEY,
user_id VARCHAR NOT NULL,
auth_token VARCHAR NOT NULL,
api_key VARCHAR NOT NULL,
kavita_config_id INT NOT NULL,
CONSTRAINT fk_kavita_configs
FOREIGN KEY(kavita_config_id)
REFERENCES kavita_configs(id)
);
''')
..enqueueMigration(
"2.17", 'CREATE UNIQUE INDEX idx_kavita_user_configs_id ON kavita_user_configs(user_id, kavita_config_id);')
..enqueueMigration("2.18",
'ALTER TABLE kavita_user_configs ADD CONSTRAINT kavita_user_configs_user_id_unique UNIQUE (user_id, kavita_config_id);');

await migrator.runMigrations();

Expand Down

0 comments on commit b16f546

Please sign in to comment.