feat: update profile when adding preexisting url
This commit is contained in:
@@ -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());
|
||||
},
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user