feat: update profile when adding preexisting url
This commit is contained in:
@@ -24,6 +24,13 @@ class ProfilesDao extends DatabaseAccessor<AppDatabase>
|
|||||||
.getSingleOrNull();
|
.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() {
|
Stream<Profile?> watchActiveProfile() {
|
||||||
return (profileEntries.select()..where((tbl) => tbl.active.equals(true)))
|
return (profileEntries.select()..where((tbl) => tbl.active.equals(true)))
|
||||||
.map(ProfileMapper.fromEntry)
|
.map(ProfileMapper.fromEntry)
|
||||||
@@ -87,6 +94,11 @@ class ProfilesDao extends DatabaseAccessor<AppDatabase>
|
|||||||
Future<void> edit(Profile patch) async {
|
Future<void> edit(Profile patch) async {
|
||||||
await transaction(
|
await transaction(
|
||||||
() async {
|
() 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)))
|
await (update(profileEntries)..where((tbl) => tbl.id.equals(patch.id)))
|
||||||
.write(patch.toCompanion());
|
.write(patch.toCompanion());
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -71,6 +71,15 @@ class ProfilesRepositoryImpl
|
|||||||
}) {
|
}) {
|
||||||
return exceptionHandler(
|
return exceptionHandler(
|
||||||
() async {
|
() 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();
|
final profileId = const Uuid().v4();
|
||||||
return fetch(url, profileId)
|
return fetch(url, profileId)
|
||||||
.flatMap(
|
.flatMap(
|
||||||
|
|||||||
Reference in New Issue
Block a user