From 059a90d2f54990627a122ebb5d9c5a3ba60c8ce8 Mon Sep 17 00:00:00 2001 From: AhsanAli13503 Date: Fri, 17 May 2024 17:15:49 +0500 Subject: [PATCH] feat: database operation needed to work in app for draft and publish list --- evently/i18n/de.json | 5 +- evently/i18n/en-US.json | 3 +- evently/i18n/es.json | 4 +- evently/i18n/ru-RU.json | 4 +- evently/lib/evently_provider.dart | 27 +++++++-- evently/lib/generated/locale_keys.g.dart | 2 + evently/lib/models/events.dart | 9 ++- evently/lib/repository/repository.dart | 7 +-- evently/lib/screens/create_event.dart | 5 ++ .../event_hub/event_hub_view_model.dart | 2 +- .../datasources/local_datasource.dart | 34 ++++++++++- .../third_party_services/database.g.dart | 59 +++++++++++++++++-- .../third_party_services/event_dao.dart | 27 ++++++++- evently/lib/utils/enums.dart | 1 + 14 files changed, 164 insertions(+), 25 deletions(-) create mode 100644 evently/lib/utils/enums.dart diff --git a/evently/i18n/de.json b/evently/i18n/de.json index f97e482361..42dceb774f 100644 --- a/evently/i18n/de.json +++ b/evently/i18n/de.json @@ -65,5 +65,8 @@ "cookbook_not_found": "Cookbook nicht gefunden", "loading": "Wird geladen ...", "something_wrong": "Etwas ist schief gelaufen. Bitte versuchen Sie, die Anwendung neu zu starten.", - "get_error": "Entwurf konnte nicht abgerufen werden" + "get_error": "Entwurf konnte nicht abgerufen werden", + "delete_error": "Entwurf konnte nicht gelöscht werden", + "save_error": "Entwurf konnte nicht gespeichert werden", + "save_error": "Failed to save draft" } \ No newline at end of file diff --git a/evently/i18n/en-US.json b/evently/i18n/en-US.json index 973207bd10..40c0782be3 100644 --- a/evently/i18n/en-US.json +++ b/evently/i18n/en-US.json @@ -65,5 +65,6 @@ "cookbook_not_found": "Cookbook not found", "loading": "loading ...", "something_wrong": "Something went wrong. Please try restarting the application.", - "get_error": "Failed to get draft" + "get_error": "Failed to get draft", + "delete_error": "Failed to delete draft" } \ No newline at end of file diff --git a/evently/i18n/es.json b/evently/i18n/es.json index 0a9e319a8c..d8a8ead539 100644 --- a/evently/i18n/es.json +++ b/evently/i18n/es.json @@ -65,5 +65,7 @@ "cookbook_not_found": "Cookbook extraviado", "loading": "cargando ...", "something_wrong": "Algo salió mal. Intenta reiniciar la aplicación.", - "get_error": "No se pudo obtener el Plan Preliminar" + "get_error": "No se pudo obtener el Plan Preliminar", + "delete_error": "No se pudo eliminar el Plan Preliminar", + "save_error": "No se pudo guardar el Plan Preliminar" } \ No newline at end of file diff --git a/evently/i18n/ru-RU.json b/evently/i18n/ru-RU.json index 24d581e1bc..0128535959 100644 --- a/evently/i18n/ru-RU.json +++ b/evently/i18n/ru-RU.json @@ -65,5 +65,7 @@ "cookbook_not_found": "Cookbook не найден", "loading": "cargando ...", "something_wrong": "Что-то пошло не так. Попробуйте перезапустить приложение.", - "get_error": "Не удалось получить черновик" + "get_error": "Не удалось получить черновик", + "delete_error": "Не удалось удалить черновик", + "save_error": "Не удалось сохранить черновик" } \ No newline at end of file diff --git a/evently/lib/evently_provider.dart b/evently/lib/evently_provider.dart index 4e2275e7e8..f9b30d491f 100644 --- a/evently/lib/evently_provider.dart +++ b/evently/lib/evently_provider.dart @@ -19,7 +19,6 @@ import 'package:flutter/widgets.dart'; import 'package:get_it/get_it.dart'; import 'package:injectable/injectable.dart'; import 'package:pylons_sdk/low_level.dart'; - import 'services/third_party_services/quick_node.dart'; @LazySingleton() @@ -218,7 +217,6 @@ class EventlyProvider extends ChangeNotifier { setThumbnail = "$ipfsDomain/${fileUploadResponse.value?.cid}"; } - String currentUserName = ""; bool stripeAccountExists = false; String? _cookbookId; @@ -231,7 +229,6 @@ class EventlyProvider extends ChangeNotifier { final sdkResponse = await PylonsWallet.instance.getProfile(); if (sdkResponse.success) { - currentUserName = sdkResponse.data!.username; stripeAccountExists = sdkResponse.data!.stripeExists; supportedDenomList = Denom.availableDenoms.where((Denom e) => sdkResponse.data!.supportedCoins.contains(e.symbol)).toList(); @@ -278,7 +275,7 @@ class EventlyProvider extends ChangeNotifier { price: price.toString(), listOfPerks: perks.join(','), cookbookID: _cookbookId!, - recipeID: _recipeId, + recipeID: _recipeId, step: '', ); final recipe = event.createRecipe( @@ -331,4 +328,26 @@ class EventlyProvider extends ChangeNotifier { navigatorKey.showMsg(message: response.error); return false; } + + void initStore() { + _eventName = ""; + _hostName = ""; + _thumbnail = ""; + _isOverviewEnable = false; + + _startDate = ""; + _endDate = ""; + _startTime = ""; + _endTime = ""; + _location = ""; + _description = ""; + _selectedDenom = Denom.availableDenoms.first; + + _isFreeDrop = FreeDrop.unselected; + _numberOfTickets = 0; + _price = 0; + _cookbookId = ''; + + notifyListeners(); + } } diff --git a/evently/lib/generated/locale_keys.g.dart b/evently/lib/generated/locale_keys.g.dart index 51700b00fe..1abafc579b 100644 --- a/evently/lib/generated/locale_keys.g.dart +++ b/evently/lib/generated/locale_keys.g.dart @@ -66,5 +66,7 @@ abstract class LocaleKeys { static const loading = 'loading'; static const something_wrong = 'something_wrong'; static const get_error = 'get_error'; + static const delete_error = 'delete_error'; + static const save_error = 'save_error'; } diff --git a/evently/lib/models/events.dart b/evently/lib/models/events.dart index de4ffa4b5b..e4bf31cd73 100644 --- a/evently/lib/models/events.dart +++ b/evently/lib/models/events.dart @@ -9,7 +9,7 @@ enum FreeDrop { yes, no, unselected } class Events extends Equatable { @primaryKey final int? id; - + final String recipeID; final String eventName; final String hostName; final String thumbnail; @@ -24,9 +24,11 @@ class Events extends Equatable { final String listOfPerks; final String isFreeDrops; final String cookbookID; - final String recipeID; + final String step; const Events({ + this.id, + ///* overview data this.eventName = '', this.hostName = '', @@ -51,7 +53,7 @@ class Events extends Equatable { ///* other this.cookbookID = '', this.recipeID = '', - this.id, + required this.step, }); factory Events.fromRecipe(Recipe recipe) { @@ -69,6 +71,7 @@ class Events extends Equatable { numberOfTickets: map[kNumberOfTickets]!, price: map[kPrice]!, listOfPerks: map[kPerks]!, + step: '', ); } diff --git a/evently/lib/repository/repository.dart b/evently/lib/repository/repository.dart index e265ad0cf1..1105c8d17d 100644 --- a/evently/lib/repository/repository.dart +++ b/evently/lib/repository/repository.dart @@ -57,7 +57,7 @@ abstract class Repository { /// This method will get the drafts List from the local database /// Output: [List] returns that contains a number of [NFT] - Future>> getEvents(); + Future>> getAllEvents(); } @LazySingleton(as: Repository) @@ -136,10 +136,9 @@ class RepositoryImp implements Repository { } @override - Future>> getEvents() async { + Future>> getAllEvents() async { try { - final response = await localDataSource.getEvents(); - + final response = await localDataSource.getAllEvents(); return Right(response); } on Exception catch (_) { return Left(CacheFailure(LocaleKeys.something_wrong.tr())); diff --git a/evently/lib/screens/create_event.dart b/evently/lib/screens/create_event.dart index 6ec350472d..f0b9a1d812 100644 --- a/evently/lib/screens/create_event.dart +++ b/evently/lib/screens/create_event.dart @@ -1,3 +1,4 @@ +import 'package:evently/evently_provider.dart'; import 'package:evently/screens/detail_screen.dart'; import 'package:evently/screens/host_view_ticket_preview.dart'; import 'package:evently/screens/overview_screen.dart'; @@ -23,6 +24,10 @@ class _CreateEventState extends State { void initState() { super.initState(); + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + context.read().initStore(); + }); + createEventViewModel.init(); } diff --git a/evently/lib/screens/event_hub/event_hub_view_model.dart b/evently/lib/screens/event_hub/event_hub_view_model.dart index b35b17d1ee..2979e8a422 100644 --- a/evently/lib/screens/event_hub/event_hub_view_model.dart +++ b/evently/lib/screens/event_hub/event_hub_view_model.dart @@ -97,7 +97,7 @@ class EventHubViewModel extends ChangeNotifier { Future getDraftsList() async { final loading = LoadingProgress()..showLoadingWithProgress(message: LocaleKeys.loading.tr()); - final getEventResponse = await repository.getEvents(); + final getEventResponse = await repository.getAllEvents(); if (getEventResponse.isLeft()) { loading.dismiss(); diff --git a/evently/lib/services/datasources/local_datasource.dart b/evently/lib/services/datasources/local_datasource.dart index 0ba1334087..0cbbf985f1 100644 --- a/evently/lib/services/datasources/local_datasource.dart +++ b/evently/lib/services/datasources/local_datasource.dart @@ -41,7 +41,17 @@ abstract class LocalDataSource { /// This method will get the drafts List from the local database /// Output: [List][NFT] returns the List of drafts - Future> getEvents(); + Future> getAllEvents(); + + /// This method will delete draft from the local database + /// Input: [recipeID] the id of the draft which the user wants to delete + /// Output: [bool] returns whether the operation is successful or not + Future deleteEvents(int id); + + /// This method will save the draft of the NFT + /// Input: [Events] the draft that will will be saved in database + /// Output: [int] returns id of the inserted document + Future saveEvents(Events events); } @LazySingleton(as: LocalDataSource) @@ -101,11 +111,31 @@ class LocalDataSourceImpl extends LocalDataSource { } @override - Future> getEvents() async { + Future> getAllEvents() async { try { return await database.eventsDao.findAllEvents(); } catch (e) { throw CacheFailure(LocaleKeys.get_error.tr()); } } + + @override + Future deleteEvents(int id) async { + try { + await database.eventsDao.delete(id); + return true; + } catch (e) { + throw CacheFailure(LocaleKeys.delete_error.tr()); + } + } + + @override + Future saveEvents(Events events) async { + try { + final result = await database.eventsDao.insertEvents(events); + return result; + } catch (e) { + throw LocaleKeys.save_error.tr(); + } + } } diff --git a/evently/lib/services/third_party_services/database.g.dart b/evently/lib/services/third_party_services/database.g.dart index e00269f83d..8cecdbea4f 100644 --- a/evently/lib/services/third_party_services/database.g.dart +++ b/evently/lib/services/third_party_services/database.g.dart @@ -96,7 +96,7 @@ class _$AppDatabase extends AppDatabase { }, onCreate: (database, version) async { await database.execute( - 'CREATE TABLE IF NOT EXISTS `Events` (`id` INTEGER, `eventName` TEXT NOT NULL, `hostName` TEXT NOT NULL, `thumbnail` TEXT NOT NULL, `startDate` TEXT NOT NULL, `endDate` TEXT NOT NULL, `startTime` TEXT NOT NULL, `endTime` TEXT NOT NULL, `location` TEXT NOT NULL, `description` TEXT NOT NULL, `numberOfTickets` TEXT NOT NULL, `price` TEXT NOT NULL, `listOfPerks` TEXT NOT NULL, `isFreeDrops` TEXT NOT NULL, `cookbookID` TEXT NOT NULL, `recipeID` TEXT NOT NULL, PRIMARY KEY (`id`))'); + 'CREATE TABLE IF NOT EXISTS `Events` (`id` INTEGER, `recipeID` TEXT NOT NULL, `eventName` TEXT NOT NULL, `hostName` TEXT NOT NULL, `thumbnail` TEXT NOT NULL, `startDate` TEXT NOT NULL, `endDate` TEXT NOT NULL, `startTime` TEXT NOT NULL, `endTime` TEXT NOT NULL, `location` TEXT NOT NULL, `description` TEXT NOT NULL, `numberOfTickets` TEXT NOT NULL, `price` TEXT NOT NULL, `listOfPerks` TEXT NOT NULL, `isFreeDrops` TEXT NOT NULL, `cookbookID` TEXT NOT NULL, `step` TEXT NOT NULL, PRIMARY KEY (`id`))'); await callback?.onCreate?.call(database, version); }, @@ -120,6 +120,7 @@ class _$EventsDao extends EventsDao { 'Events', (Events item) => { 'id': item.id, + 'recipeID': item.recipeID, 'eventName': item.eventName, 'hostName': item.hostName, 'thumbnail': item.thumbnail, @@ -134,7 +135,7 @@ class _$EventsDao extends EventsDao { 'listOfPerks': item.listOfPerks, 'isFreeDrops': item.isFreeDrops, 'cookbookID': item.cookbookID, - 'recipeID': item.recipeID + 'step': item.step }); final sqflite.DatabaseExecutor database; @@ -147,9 +148,9 @@ class _$EventsDao extends EventsDao { @override Future> findAllEvents() async { - return _queryAdapter.queryList( - 'SELECT * FROM events ORDER BY dateTime DESC', + return _queryAdapter.queryList('SELECT * FROM events', mapper: (Map row) => Events( + id: row['id'] as int?, eventName: row['eventName'] as String, hostName: row['hostName'] as String, thumbnail: row['thumbnail'] as String, @@ -165,13 +166,14 @@ class _$EventsDao extends EventsDao { isFreeDrops: row['isFreeDrops'] as String, cookbookID: row['cookbookID'] as String, recipeID: row['recipeID'] as String, - id: row['id'] as int?)); + step: row['step'] as String)); } @override Future findEventsById(int id) async { return _queryAdapter.query('SELECT * FROM events WHERE id = ?1', mapper: (Map row) => Events( + id: row['id'] as int?, eventName: row['eventName'] as String, hostName: row['hostName'] as String, thumbnail: row['thumbnail'] as String, @@ -187,7 +189,7 @@ class _$EventsDao extends EventsDao { isFreeDrops: row['isFreeDrops'] as String, cookbookID: row['cookbookID'] as String, recipeID: row['recipeID'] as String, - id: row['id'] as int?), + step: row['step'] as String), arguments: [id]); } @@ -197,6 +199,51 @@ class _$EventsDao extends EventsDao { .queryNoReturn('DELETE FROM events WHERE id = ?1', arguments: [id]); } + @override + Future updateNFTFromDetail( + String startDate, + String endDate, + String startTime, + String endTime, + String location, + String description, + int id, + ) async { + await _queryAdapter.queryNoReturn( + 'UPDATE events SET startDate = ?1, endDate= ?2, startTime = ?3, endTime = ?4,location = ?5, description = ?6 WHERE id = ?7', + arguments: [ + startDate, + endDate, + startTime, + endTime, + location, + description, + id + ]); + } + + @override + Future updateNFTFromPerks( + String listOfPerks, + int id, + ) async { + await _queryAdapter.queryNoReturn( + 'UPDATE events SET listOfPerks = ?1 WHERE id = ?2', + arguments: [listOfPerks, id]); + } + + @override + Future updateNFTFromPrice( + int id, + String numberOfTickets, + String price, + String isFreeDrops, + ) async { + await _queryAdapter.queryNoReturn( + 'UPDATE events SET numberOfTickets = ?2, price = ?3, isFreeDrops = ?4 WHERE id = ?1', + arguments: [id, numberOfTickets, price, isFreeDrops]); + } + @override Future insertEvents(Events events) { return _eventsInsertionAdapter.insertAndReturnId( diff --git a/evently/lib/services/third_party_services/event_dao.dart b/evently/lib/services/third_party_services/event_dao.dart index 93e6f60b68..95bbcfe26e 100644 --- a/evently/lib/services/third_party_services/event_dao.dart +++ b/evently/lib/services/third_party_services/event_dao.dart @@ -3,7 +3,7 @@ import 'package:floor/floor.dart'; @dao abstract class EventsDao { - @Query('SELECT * FROM events ORDER BY dateTime DESC') + @Query('SELECT * FROM events') Future> findAllEvents(); @Query('SELECT * FROM events WHERE id = :id') @@ -14,4 +14,29 @@ abstract class EventsDao { @Query('DELETE FROM events WHERE id = :id') Future delete(int id); + + @Query('UPDATE events SET startDate = :startDate, endDate= :endDate, startTime = :startTime, endTime = :endTime,location = :location, description = :description WHERE id = :id') + Future updateNFTFromDetail( + String startDate, + String endDate, + String startTime, + String endTime, + String location, + String description, + int id, + ); + + @Query('UPDATE events SET listOfPerks = :listOfPerks WHERE id = :id') + Future updateNFTFromPerks( + String listOfPerks, + int id, + ); + + @Query('UPDATE events SET numberOfTickets = :numberOfTickets, price = :price, isFreeDrops = :isFreeDrops WHERE id = :id') + Future updateNFTFromPrice( + int id, + String numberOfTickets, + String price, + String isFreeDrops, + ); } diff --git a/evently/lib/utils/enums.dart b/evently/lib/utils/enums.dart new file mode 100644 index 0000000000..15f439515d --- /dev/null +++ b/evently/lib/utils/enums.dart @@ -0,0 +1 @@ +enum UploadStep { overView, detail, perks, price }