From 22d1cd17e240e8b80a5af7a6b3d3b1b5f83b0b3a Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Sat, 16 Sep 2023 14:02:26 +0330 Subject: [PATCH] feat: update profile when adding preexisting url --- lib/data/local/dao/profiles_dao.dart | 12 ++++++++++++ lib/data/repository/profiles_repository_impl.dart | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/data/local/dao/profiles_dao.dart b/lib/data/local/dao/profiles_dao.dart index d6cefced..b1c0f23d 100644 --- a/lib/data/local/dao/profiles_dao.dart +++ b/lib/data/local/dao/profiles_dao.dart @@ -24,6 +24,13 @@ class ProfilesDao extends DatabaseAccessor .getSingleOrNull(); } + Future getProfileByUrl(String url) async { + return (select(profileEntries)..where((tbl) => tbl.url.like('%$url%'))) + .map(ProfileMapper.fromEntry) + .get() + .then((value) => value.firstOrNull); + } + Stream watchActiveProfile() { return (profileEntries.select()..where((tbl) => tbl.active.equals(true))) .map(ProfileMapper.fromEntry) @@ -87,6 +94,11 @@ class ProfilesDao extends DatabaseAccessor Future 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()); }, diff --git a/lib/data/repository/profiles_repository_impl.dart b/lib/data/repository/profiles_repository_impl.dart index 7a299034..e5d07e43 100644 --- a/lib/data/repository/profiles_repository_impl.dart +++ b/lib/data/repository/profiles_repository_impl.dart @@ -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(