From 57735337d67af54bb29211c16b6965bdb1bca571 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Wed, 3 Jun 2020 18:37:20 +0200 Subject: [PATCH] client: Add sync support When deleting items, this needs to be explicitly activated. For now, everything will allways be synced as soon as one update with sync set to true is made. In the future, this will be done in the background. --- client/lib/src/client.dart | 36 +++++++++++++++--- client/lib/src/database.dart | 74 +++++++++++++++++++++++++++++++++--- client/lib/src/database.moor | 30 +++++++++++++++ 3 files changed, 129 insertions(+), 11 deletions(-) diff --git a/client/lib/src/client.dart b/client/lib/src/client.dart index b10cc3b..b3200c2 100644 --- a/client/lib/src/client.dart +++ b/client/lib/src/client.dart @@ -297,7 +297,12 @@ class MusicusClient { )); final json = jsonDecode(response.body); - return json.map((j) => Person.fromJson(j)).toList(); + return json + .map((j) => Person.fromJson(j).copyWith( + sync: true, + synced: true, + )) + .toList(); } /// Get a person by ID. @@ -307,7 +312,10 @@ class MusicusClient { )); final json = jsonDecode(response.body); - return Person.fromJson(json); + return Person.fromJson(json).copyWith( + sync: true, + synced: true, + ); } /// Delete a person by ID. @@ -358,7 +366,12 @@ class MusicusClient { )); final json = jsonDecode(response.body); - return json.map((j) => Instrument.fromJson(j)).toList(); + return json + .map((j) => Instrument.fromJson(j).copyWith( + sync: true, + synced: true, + )) + .toList(); } /// Get an instrument by ID. @@ -368,7 +381,10 @@ class MusicusClient { )); final json = jsonDecode(response.body); - return Instrument.fromJson(json); + return Instrument.fromJson(json).copyWith( + sync: true, + synced: true, + ); } /// Create or update an instrument. @@ -500,7 +516,12 @@ class MusicusClient { )); final json = jsonDecode(response.body); - return json.map((j) => Ensemble.fromJson(j)).toList(); + return json + .map((j) => Ensemble.fromJson(j).copyWith( + sync: true, + synced: true, + )) + .toList(); } /// Get an ensemble by ID. @@ -510,7 +531,10 @@ class MusicusClient { )); final json = jsonDecode(response.body); - return Ensemble.fromJson(json); + return Ensemble.fromJson(json).copyWith( + sync: true, + synced: true, + ); } /// Create or update an ensemble. diff --git a/client/lib/src/database.dart b/client/lib/src/database.dart index df74290..66e4058 100644 --- a/client/lib/src/database.dart +++ b/client/lib/src/database.dart @@ -45,6 +45,50 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { this.client, }) : super.connect(connection); + /// Upload all changes to the server. + /// + /// If [update] is true, this will also update existing items with new data + /// from the server. + Future sync([bool update = false]) async { + if (update) { + for (final person in await oldSyncPersons().get()) { + await updatePerson(await client.getPerson(person.id)); + } + for (final instrument in await oldSyncInstruments().get()) { + await updateInstrument(await client.getInstrument(instrument.id)); + } + for (final work in await oldSyncWorks().get()) { + final workInfo = await client.getWork(work.id); + await updateWork(workInfo); + } + for (final ensemble in await oldSyncEnsembles().get()) { + await updateEnsemble(await client.getEnsemble(ensemble.id)); + } + for (final recording in await oldSyncRecordings().get()) { + final recordingInfo = await client.getRecording(recording.id); + await updateRecording(recordingInfo); + } + } + + for (final person in await newSyncPersons().get()) { + await client.putPerson(person); + } + for (final instrument in await newSyncInstruments().get()) { + await client.putInstrument(instrument); + } + for (final work in await newSyncWorks().get()) { + final workInfo = await getWorkInfo(work); + await client.putWork(workInfo); + } + for (final ensemble in await newSyncEnsembles().get()) { + await client.putEnsemble(ensemble); + } + for (final recording in await newSyncRecordings().get()) { + final recordingInfo = await getRecordingInfo(recording); + await client.putRecording(recordingInfo); + } + } + /// Get all available persons. /// /// This will return a list of [pageSize] persons. You can get another page @@ -69,10 +113,14 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { person, mode: InsertMode.insertOrReplace, ); + + if (person.sync) { + await sync(); + } } /// Delete the person by [id]. - /// + /// /// If [sync] is true, the person will be deleted from the server too. If /// that fails, a MusicusNotAuthorizedException or MusicusNotLoggedInException /// willl be thrown and the person will NOT be deleted. @@ -108,10 +156,14 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { instrument, mode: InsertMode.insertOrReplace, ); + + if (instrument.sync) { + await sync(); + } } /// Delete the instrument by [id]. - /// + /// /// If [sync] is true, the instrument will be deleted from the server too. If /// that fails, a MusicusNotAuthorizedException or MusicusNotLoggedInException /// willl be thrown and the instrument will NOT be deleted. @@ -235,10 +287,14 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { await into(workSections).insert(section); } }); + + if (workInfo.work.sync) { + await sync(); + } } /// Delete the work by [id]. - /// + /// /// If [sync] is true, the work will be deleted from the server too. If /// that fails, a MusicusNotAuthorizedException or MusicusNotLoggedInException /// willl be thrown and the work will NOT be deleted. @@ -276,10 +332,14 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { ensemble, mode: InsertMode.insertOrReplace, ); + + if (ensemble.sync) { + await sync(); + } } /// Delete the ensemble by [id]. - /// + /// /// If [sync] is true, the ensemble will be deleted from the server too. If /// that fails, a MusicusNotAuthorizedException or MusicusNotLoggedInException /// willl be thrown and the ensemble will NOT be deleted. @@ -325,6 +385,10 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { )); } }); + + if (recordingInfo.recording.sync) { + await sync(); + } } /// Retreive more information on an already queried recording. @@ -359,7 +423,7 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { } /// Delete a recording by [id]. - /// + /// /// If [sync] is true, the recording will be deleted from the server too. If /// that fails, a MusicusNotAuthorizedException or MusicusNotLoggedInException /// willl be thrown and the recording will NOT be deleted. diff --git a/client/lib/src/database.moor b/client/lib/src/database.moor index 3544348..037006c 100644 --- a/client/lib/src/database.moor +++ b/client/lib/src/database.moor @@ -74,6 +74,12 @@ allPersons: SELECT * FROM persons ORDER BY last_name, first_name LIMIT :limit OFFSET :offset; +newSyncPersons: +SELECT * FROM persons WHERE sync = TRUE AND synced = FALSE; + +oldSyncPersons: +SELECT * FROM persons WHERE sync = TRUE AND synced = TRUE; + searchPersons: SELECT * FROM persons WHERE last_name LIKE :search ORDER BY last_name, first_name LIMIT :limit OFFSET :offset; @@ -84,6 +90,12 @@ SELECT * FROM persons WHERE id = :id LIMIT 1; allInstruments: SELECT * FROM instruments ORDER BY name LIMIT :limit OFFSET :offset; +newSyncInstruments: +SELECT * FROM instruments WHERE sync = TRUE AND synced = FALSE; + +oldSyncInstruments: +SELECT * FROM instruments WHERE sync = TRUE AND synced = TRUE; + searchInstruments: SELECT * FROM instruments WHERE name LIKE :search ORDER BY name LIMIT :limit OFFSET :offset; @@ -91,6 +103,12 @@ SELECT * FROM instruments WHERE name LIKE :search ORDER BY name instrumentById: SELECT * FROM instruments WHERE id = :id LIMIT 1; +newSyncWorks: +SELECT * FROM works WHERE sync = TRUE AND synced = FALSE; + +oldSyncWorks: +SELECT * FROM works WHERE sync = TRUE AND synced = TRUE; + workById: SELECT * FROM works WHERE id = :id LIMIT 1; @@ -131,6 +149,12 @@ SELECT instruments.* FROM part_instrumentations allEnsembles: SELECT * FROM ensembles ORDER BY name LIMIT :limit OFFSET :offset; +newSyncEnsembles: +SELECT * FROM ensembles WHERE sync = TRUE AND synced = FALSE; + +oldSyncEnsembles: +SELECT * FROM ensembles WHERE sync = TRUE AND synced = TRUE; + searchEnsembles: SELECT * FROM ensembles WHERE name LIKE :search ORDER BY name LIMIT :limit OFFSET :offset; @@ -138,6 +162,12 @@ SELECT * FROM ensembles WHERE name LIKE :search ORDER BY name ensembleById: SELECT * FROM ensembles WHERE id = :id LIMIT 1; +newSyncRecordings: +SELECT * FROM recordings WHERE sync = TRUE AND synced = FALSE; + +oldSyncRecordings: +SELECT * FROM recordings WHERE sync = TRUE AND synced = TRUE; + recordingById: SELECT * FROM recordings WHERE id = :id;