6177214e-ce7c-49e3-99de-ff9721b26f63 — Commit 9588fa24

AuthorEdmir Suljic<esu@dwarf.dk>
Date2025-10-28 09:04:20 +0100
Resolved PR comments

Changed files

.../repository/hotel_information_repository.dart   | 47 ++++++------
 .../lib/profile/profile_repository.dart            | 86 +++++++++++++---------
 .../lib/up_sales/up_sales_repository.dart          | 46 ++++++------
 3 files changed, 102 insertions(+), 77 deletions(-)

Diff

diff --git a/comwell_key_app/lib/hotel_information/repository/hotel_information_repository.dart b/comwell_key_app/lib/hotel_information/repository/hotel_information_repository.dart
index 5a7398f4..a45687d8 100644
--- a/comwell_key_app/lib/hotel_information/repository/hotel_information_repository.dart
+++ b/comwell_key_app/lib/hotel_information/repository/hotel_information_repository.dart
@@ -9,34 +9,37 @@ class HotelInformationRepository {
final api = Api();
Future<Hotel> fetchHotelInformation(String hotelCode) async {
- try {
- final hotel = await _checkIfHotelInformationExists(hotelCode);
+ final hotel = await _checkIfHotelInformationExists(hotelCode);
+ if (hotel != null) {
debugPrint("Hotel information fetched from database: $hotel");
return hotel;
- } catch (e) {
- try {
- final newHotel = await _fetchAndSaveHotelInformationToDatabase(hotelCode);
- return newHotel;
- } catch (e) {
- debugPrint("Error fetching hotel information: $e");
- rethrow;
- }
}
+ final newHotel = await _fetchAndSaveHotelInformationToDatabase(hotelCode);
+ return newHotel;
}
- Future<Hotel> _fetchAndSaveHotelInformationToDatabase(String hotelCode) async {
- final response = await api.getHotelInfo(hotelCode);
- final data = response.data as Json;
- final hotelInfo = Hotel.fromJson(data);
- await locator<ComwellDatabase>().hotelInformationDAO.saveHotel(hotelInfo);
- debugPrint("Hotel information saved to database: $hotelInfo");
- return hotelInfo;
+ Future<Hotel> _fetchAndSaveHotelInformationToDatabase(
+ String hotelCode) async {
+ try {
+ final response = await api.getHotelInfo(hotelCode);
+ final data = response.data as Json;
+ final hotelInfo = Hotel.fromJson(data);
+ await locator<ComwellDatabase>().hotelInformationDAO.saveHotel(hotelInfo);
+ debugPrint("Hotel information saved to database: $hotelInfo");
+ return hotelInfo;
+ } on Exception catch (e) {
+ debugPrint("Error fetching hotel information: $e");
+ rethrow;
+ }
}
- Future<Hotel> _checkIfHotelInformationExists(String hotelCode) async {
- final hotel = await locator<ComwellDatabase>()
- .hotelInformationDAO
- .getHotelByCode(hotelCode);
- return hotel;
+ Future<Hotel?> _checkIfHotelInformationExists(String hotelCode) async {
+ try {
+ return await locator<ComwellDatabase>()
+ .hotelInformationDAO
+ .getHotelByCode(hotelCode);
+ } catch (e) {
+ return null;
+ }
}
}
diff --git a/comwell_key_app/lib/profile/profile_repository.dart b/comwell_key_app/lib/profile/profile_repository.dart
index c84cd78f..aa24e48a 100644
--- a/comwell_key_app/lib/profile/profile_repository.dart
+++ b/comwell_key_app/lib/profile/profile_repository.dart
@@ -8,7 +8,7 @@ import 'package:comwell_key_app/services/mappers/booking_mapper.dart';
import 'package:comwell_key_app/services/mappers/user_mapper.dart';
import 'package:comwell_key_app/services/models/bookings_dto.dart';
import 'package:comwell_key_app/services/models/user_dto.dart';
-import 'package:comwell_key_app/utils/json.dart';
+import 'package:comwell_key_app/utils/json.dart';
import 'package:comwell_key_app/utils/locator.dart';
import 'package:comwell_key_app/utils/secure_storage.dart';
import 'package:comwell_key_app/utils/seos_repository.dart';
@@ -54,55 +54,73 @@ class ProfileRepository {
Future<Booking> getBookingDetails(String bookingId) async {
user = await fetchProfileSettings();
- try {
- final booking = await _checkIfBookingDetailsExists(bookingId);
+
+ final booking = await _checkIfBookingDetailsExists(bookingId);
+ if (booking != null) {
return booking;
- } catch (e) {
- try {
- final newBooking =
- await _fetchAndSaveBookingDetailsToDatabase(bookingId);
- return newBooking;
- } catch (e) {
- debugPrint("Error fetching booking details: $e");
- rethrow;
- }
}
+
+ final newBooking = await _fetchAndSaveBookingDetailsToDatabase(bookingId);
+ return newBooking;
}
- Future<Booking> _checkIfBookingDetailsExists(String bookingId) async {
- final booking = await locator<ComwellDatabase>()
- .bookingsDao
- .getBookingDetails(bookingId, user.id, []);
- return booking;
+ Future<Booking?> _checkIfBookingDetailsExists(String bookingId) async {
+ try {
+ final booking = await locator<ComwellDatabase>()
+ .bookingsDao
+ .getBookingDetails(bookingId, user.id, []);
+ return booking;
+ } on Exception catch (e) {
+ debugPrint("Error checking if booking details exists: $e");
+ return null;
+ }
}
Future<Booking> _fetchAndSaveBookingDetailsToDatabase(
String bookingId) async {
- final response = await api.getBookingDetails(bookingId);
- await locator<ComwellDatabase>().bookingsDao.insertBookings(
- BookingsDTO(current: [response!], past: [], cancelled: []));
- final booking = response.toBooking(user.id, []);
- debugPrint("Booking saved to database: ${booking.confirmationId}");
- return booking;
+ try {
+ final response = await api.getBookingDetails(bookingId);
+ await locator<ComwellDatabase>().bookingsDao.insertBookings(
+ BookingsDTO(current: [response!], past: [], cancelled: []));
+ final booking = response.toBooking(user.id, []);
+ debugPrint("Booking saved to database: ${booking.confirmationId}");
+ return booking;
+ } on Exception catch (e) {
+ debugPrint("Error fetching booking details: $e");
+ rethrow;
+ }
}
- Future<User> fetchProfileSettings() async {
+ Future<User?> _checkIfProfileSettingsExists() async {
try {
final user = await locator<ComwellDatabase>().userDAO.getUser();
return user;
} catch (e) {
- try {
- final response = await api.fetchProfileSettings();
- final data = response.data as Json;
- final userDto = UserDto.fromJson(data);
- final user = userDto.toUser();
+ debugPrint("Error checking if profile settings exists: $e");
+ return null;
+ }
+ }
+ Future<User> _fetchAndSaveProfileSettingsToDatabase() async {
+ try {
+ final response = await api.fetchProfileSettings();
+ final data = response.data as Json;
+ final userDto = UserDto.fromJson(data);
+ final user = userDto.toUser();
+ await locator<ComwellDatabase>().userDAO.saveUser(userDto);
+ return user;
+ } catch (e) {
+ debugPrint("Error fetching profile settings: $e");
+ rethrow;
+ }
+ }
- return user;
- } catch (e) {
- debugPrint("Error fetching profile settings: $e");
- rethrow;
- }
+ Future<User> fetchProfileSettings() async {
+ final user = await _checkIfProfileSettingsExists();
+ if (user != null) {
+ return user;
}
+ final newUser = await _fetchAndSaveProfileSettingsToDatabase();
+ return newUser;
}
}
diff --git a/comwell_key_app/lib/up_sales/up_sales_repository.dart b/comwell_key_app/lib/up_sales/up_sales_repository.dart
index 6a209c3a..4debc3d6 100644
--- a/comwell_key_app/lib/up_sales/up_sales_repository.dart
+++ b/comwell_key_app/lib/up_sales/up_sales_repository.dart
@@ -18,36 +18,40 @@ class UpSalesRepository {
}
Future<UpSales> getUpSales(String confirmationId, String hotelCode) async {
- try {
- final upsales = await _checkIfUpSalesExists(confirmationId, hotelCode);
+ final upsales = await _checkIfUpSalesExists(confirmationId, hotelCode);
+ if (upsales != null) {
return upsales;
- } catch (e) {
- try {
- final newUpsales =
- await _fetchAndSaveUpSalesToDatabase(confirmationId, hotelCode);
- return newUpsales;
- } catch (e) {
- debugPrint("Error fetching up sales: $e");
- rethrow;
- }
}
+ final newUpsales =
+ await _fetchAndSaveUpSalesToDatabase(confirmationId, hotelCode);
+ return newUpsales;
}
Future<UpSales> _fetchAndSaveUpSalesToDatabase(
String confirmationId, String hotelCode) async {
- final response = await api.fetchUpSales(confirmationId, hotelCode);
- await locator<ComwellDatabase>().upsalesDAO.insertUpsale(response);
- final upsales = response.toUpSales();
- debugPrint("Up sales saved to database: $upsales");
- return upsales;
+ try {
+ final response = await api.fetchUpSales(confirmationId, hotelCode);
+ await locator<ComwellDatabase>().upsalesDAO.insertUpsale(response);
+ final upsales = response.toUpSales();
+ debugPrint("Up sales saved to database: $upsales");
+ return upsales;
+ } on Exception catch (e) {
+ debugPrint("Error fetching up sales: $e");
+ rethrow;
+ }
}
- Future<UpSales> _checkIfUpSalesExists(
+ Future<UpSales?> _checkIfUpSalesExists(
String confirmationId, String hotelCode) async {
- final upsales = await locator<ComwellDatabase>()
- .upsalesDAO
- .getUpsaleByConfirmationNumber(confirmationId);
- return upsales;
+ try {
+ final upsales = await locator<ComwellDatabase>()
+ .upsalesDAO
+ .getUpsaleByConfirmationNumber(confirmationId);
+ return upsales;
+ } on Exception catch (e) {
+ debugPrint("Error checking if up sales exists: $e");
+ return null;
+ }
}
Future<void> addUpSalesToBooking(String confirmationId, String hotelCode,