feat: update profile when adding preexisting url

This commit is contained in:
problematicconsumer
2023-09-16 14:02:26 +03:30
parent 03c4cc7330
commit 22d1cd17e2
2 changed files with 21 additions and 0 deletions

View File

@@ -24,6 +24,13 @@ class ProfilesDao extends DatabaseAccessor<AppDatabase>
.getSingleOrNull();
}
Future<Profile?> getProfileByUrl(String url) async {
return (select(profileEntries)..where((tbl) => tbl.url.like('%$url%')))
.map(ProfileMapper.fromEntry)
.get()
.then((value) => value.firstOrNull);
}
Stream<Profile?> watchActiveProfile() {
return (profileEntries.select()..where((tbl) => tbl.active.equals(true)))
.map(ProfileMapper.fromEntry)
@@ -87,6 +94,11 @@ class ProfilesDao extends DatabaseAccessor<AppDatabase>
Future<void> edit(Profile patch) async {
await transaction(
() async {
if (patch.active) {
await (update(profileEntries)
..where((tbl) => tbl.id.isNotValue(patch.id)))
.write(const ProfileEntriesCompanion(active: Value(false)));
}
await (update(profileEntries)..where((tbl) => tbl.id.equals(patch.id)))
.write(patch.toCompanion());
},

View File

@@ -71,6 +71,15 @@ class ProfilesRepositoryImpl
}) {
return exceptionHandler(
() async {
final existingProfile = await profilesDao.getProfileByUrl(url);
if (existingProfile != null) {
loggy.info("profile with url[$url] already exists, updating");
final baseProfile = markAsActive
? existingProfile.copyWith(active: true)
: existingProfile;
return update(baseProfile).run();
}
final profileId = const Uuid().v4();
return fetch(url, profileId)
.flatMap(