Skip to content

Commit

Permalink
Merge pull request #295 from AlexTelon/feature/#288-Adapt-code-to-bac…
Browse files Browse the repository at this point in the history
…kend-incoming-changes

Feature/#288 adapt code to backend incoming changes
  • Loading branch information
reeshy authored Oct 11, 2023
2 parents 4596914 + e03d45c commit 0ca3010
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 105 deletions.
15 changes: 10 additions & 5 deletions lib/models/charger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@ class Charger {
int id = -1;
int chargerPointId = 0;
String status = '';
LatLng coordinates = LatLng(0, 0);
String serialNumber = "";
LatLng location = LatLng(0, 0);
String capacity = '';
String cost = '';
int cost = 0;
String type = '';

Charger();
Charger.fromCharger({
required this.id,
required this.chargerPointId,
required this.status,
// required this.coordinates,
required this.location,
required serialNumber,
this.capacity = '',
required this.cost,
this.type = '',
});

Charger.fromJson(Map<String, dynamic> json) {
id = json['chargerID'];
chargerPointId = json['chargePointID'];
id = json['connectorID'];
location =
LatLng(json['location'][0].toDouble(), json['location'][1].toDouble());
serialNumber = json["serialNumber"] ?? "";
status = json['status'] ?? '';
chargerPointId = json['chargePointID'];
}
}
28 changes: 17 additions & 11 deletions lib/models/charger_point.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,31 @@ import 'package:google_maps_flutter/google_maps_flutter.dart';
class ChargerPoint {
int chargerPointId = -1;
String name = '';
LatLng coordinates = LatLng(0.0, 0.0);
String price = '';
String address = "";
LatLng location = LatLng(0.0, 0.0);
int price = 0;
int klarnaReservationAmount = 0;
List<Charger> chargers = [];

ChargerPoint();
ChargerPoint.fromCharger({
required this.chargerPointId,
required this.coordinates,
required this.chargers,
required this.name,
required this.price,
});
ChargerPoint.fromCharger(
{required this.chargerPointId,
required this.location,
required this.chargers,
required this.address,
required this.name,
required this.price,
required this.klarnaReservationAmount});

/// The `ChargerPoint.fromJson` method is a factory constructor that takes a `Map<String, dynamic>` as a
/// parameter. It is used to create a `ChargerPoint` object from a JSON representation.
ChargerPoint.fromJson(Map<String, dynamic> json) {
chargerPointId = json['chargePointID'];
coordinates =
location =
LatLng(json['location'][0].toDouble(), json['location'][1].toDouble());
address = json["address"];
name = json['name'] ?? '';
price = json['price'] ?? '';
price = json['price'] ?? null;
klarnaReservationAmount = json["klarnaReservationAmount"];
}
}
10 changes: 5 additions & 5 deletions lib/models/transaction.dart → lib/models/old_transaction.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// The class is a model for a transaction. It has a bunch of properties,
/// and a constructor that takes a map of strings and dynamic values
class Transaction {
class OldTransaction {
int transactionID = 0;
String userID = "";
int chargerID = 0;
Expand All @@ -14,9 +14,9 @@ class Transaction {
int currentChargePercentage = 0;
String paymentID = '';

Transaction();
OldTransaction();

Transaction.fromTransaction({
OldTransaction.fromTransaction({
required this.transactionID,
required this.userID,
required this.chargerID,
Expand All @@ -31,10 +31,10 @@ class Transaction {
required this.paymentID,
});

Transaction.fromJson(Map<String, dynamic> json) {
OldTransaction.fromJson(Map<String, dynamic> json) {
transactionID = json['transactionID'] ?? 0;
userID = json['userID'] ?? '';
chargerID = json['chargerID'] ?? 0;
chargerID = json['connectorID'] ?? 0;
pricePerKwh = double.parse(json['pricePerKwh'] ?? '0.0');
sessionID = json['session_id'] ?? "";
clientToken = json['klarna_consumer_token'] ?? "";
Expand Down
88 changes: 40 additions & 48 deletions lib/services/charger_api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,56 +43,48 @@ class ChargerApiService {
List<ChargerPoint> chargerPoints = [];

try {
var response = await client.get(Uri.parse('${API.url}/chargers'));
switch (response.statusCode) {
case 200:
List<dynamic> chargers = json.decode(response.body);
if (chargers.isEmpty)
return throw throw Exception('No Chargers Found');
for (var charger in chargers) {
var chargerPoint = chargerPoints
.where(
(value) => value.chargerPointId == charger['chargePointID'])
.toList();
if (chargerPoint.isNotEmpty) {
var firstChargerPoint = chargerPoint.first;
var chargers = await getChargers();
if (chargers.isEmpty) return throw throw Exception('No Chargers Found');
for (var charger in chargers) {
var chargerPoint = chargerPoints
.where((value) => value.chargerPointId == charger.chargerPointId)
.toList();
if (chargerPoint.isNotEmpty) {
chargerPoint.first.chargers.add(Charger.fromCharger(
id: charger.id,
location: charger.location,
serialNumber: charger.serialNumber,
cost: chargerPoint.first.price,
chargerPointId: charger.chargerPointId,
status: charger.status,
));
} else {
var chargerPoint = await getChargerPoint(charger.chargerPointId);

chargerPoint.first.chargers.add(Charger.fromCharger(
id: charger['chargerID'],
cost: firstChargerPoint.price,
chargerPointId: charger['chargePointID'],
status: charger['status'],
));
} else {
var chargerPoint =
await getChargerPoint(charger['chargePointID']);

chargerPoints.add(
ChargerPoint.fromCharger(
chargerPointId: charger['chargePointID'],
chargers: [
Charger.fromCharger(
id: charger['chargerID'],
cost: chargerPoint.price,
chargerPointId: charger['chargePointID'],
status: charger['status'],
)
],
coordinates: LatLng(charger['location'][0].toDouble(),
charger['location'][1].toDouble()),
price: chargerPoint.price,
name: chargerPoint.name,
),
);
}
}

return chargerPoints;
case 500:
throw Exception(ErrorCodes.internalError);
default:
throw Exception(ErrorCodes.internalError);
chargerPoints.add(
ChargerPoint.fromCharger(
chargerPointId: charger.chargerPointId,
chargers: [
Charger.fromCharger(
id: charger.id,
cost: chargerPoint.price,
location: charger.location,
serialNumber: charger.serialNumber,
chargerPointId: charger.chargerPointId,
status: charger.status,
)
],
location: LatLng(charger.location.latitude.toDouble(),
charger.location.longitude.toDouble()),
price: chargerPoint.price,
name: chargerPoint.name,
address: chargerPoint.address,
klarnaReservationAmount: chargerPoint.klarnaReservationAmount),
);
}
}

return chargerPoints;
} catch (e) {
print(e);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/services/local_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:async';
import 'package:flexicharge/enums/event_type.dart';
import 'package:flexicharge/models/charger.dart';
import 'package:flexicharge/models/charger_point.dart';
import 'package:flexicharge/models/transaction.dart';
import 'package:flexicharge/models/old_transaction.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

/// It's a class that holds data that is used by the app
Expand All @@ -13,7 +13,7 @@ class LocalData {
List<ChargerPoint> chargerPoints = [];
LatLng userLocation = LatLng(0, 0);
int chargingCharger = -1;
Transaction transactionSession = Transaction();
OldTransaction transactionSession = OldTransaction();
bool isButtonActive = true;
int chargingPercentage = 0;
//late Timer timer; This is commented out because a temporary
Expand Down
30 changes: 15 additions & 15 deletions lib/services/transaction_api_service.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:convert';
import 'package:flexicharge/enums/error_codes.dart';
import 'package:flexicharge/models/api.dart';
import 'package:flexicharge/models/transaction.dart';
import 'package:flexicharge/models/old_transaction.dart';
import 'package:http/http.dart' as http;

/// It's a class that makes requests to the server and returns a Transaction object
Expand All @@ -16,13 +16,13 @@ class TransactionApiService {
///
/// Returns:
/// A Future<Transaction>
Future<Transaction> getTransactionById(int id) async {
Future<OldTransaction> getTransactionById(int id) async {
var response = await client.get(Uri.parse('${API.url}/transactions/$id'));

switch (response.statusCode) {
case 200:
var parsedTransaction = json.decode(response.body);
var transactionFromJson = Transaction.fromJson(parsedTransaction);
var transactionFromJson = OldTransaction.fromJson(parsedTransaction);
return transactionFromJson;
case 404:
throw Exception(ErrorCodes.notFound);
Expand All @@ -41,16 +41,16 @@ class TransactionApiService {
///
/// Returns:
/// A list of transactions.
Future<List<Transaction>> getTransactionsByUserId(int id) async {
var transactions = <Transaction>[];
Future<List<OldTransaction>> getTransactionsByUserId(int id) async {
var transactions = <OldTransaction>[];
var response = await client
.get(Uri.parse('${API.url}/transactions/userTransactions/$id'));

switch (response.statusCode) {
case 200:
var parsedTransactions = json.decode(response.body) as List<dynamic>;
for (var trans in parsedTransactions) {
transactions.add(Transaction.fromJson(trans));
transactions.add(OldTransaction.fromJson(trans));
}
return transactions;
case 404:
Expand All @@ -70,16 +70,16 @@ class TransactionApiService {
///
/// Returns:
/// A list of transactions.
Future<List<Transaction>> getTransactionsByChargerId(int id) async {
var transactions = <Transaction>[];
Future<List<OldTransaction>> getTransactionsByChargerId(int id) async {
var transactions = <OldTransaction>[];
var response = await client
.get(Uri.parse('${API.url}/transactions/chargerTransactions/$id'));

switch (response.statusCode) {
case 200:
var parsedTransactions = json.decode(response.body) as List<dynamic>;
for (var trans in parsedTransactions) {
transactions.add(Transaction.fromJson(trans));
transactions.add(OldTransaction.fromJson(trans));
}
return transactions;
case 404:
Expand Down Expand Up @@ -160,7 +160,7 @@ class TransactionApiService {
///
/// Returns:
/// The response is a JSON object containing the following:
Future<Transaction> createKlarnaPaymentSession(
Future<OldTransaction> createKlarnaPaymentSession(
int? userId, int chargerId) async {
var response = await client.post(Uri.parse('${API.url}/transactions'),
headers: API.defaultRequestHeaders,
Expand All @@ -174,7 +174,7 @@ class TransactionApiService {
switch (response.statusCode) {
case 201:
var transaction = json.decode(response.body) as Map<String, dynamic>;
var parsedSession = Transaction.fromJson(transaction);
var parsedSession = OldTransaction.fromJson(transaction);
return parsedSession;
case 400:
throw Exception(ErrorCodes.badRequest);
Expand All @@ -189,7 +189,7 @@ class TransactionApiService {

/// The request returns the updated transaction object,
/// If everything goes as expected, it will contain a paymentId.
Future<Transaction> createKlarnaOrder(
Future<OldTransaction> createKlarnaOrder(
int transactionId,
String authToken,
) async {
Expand All @@ -205,7 +205,7 @@ class TransactionApiService {
case 200:
var transaction = json.decode(response.body) as Map<String, dynamic>;
if (transaction.isEmpty) throw Exception(ErrorCodes.emptyResponse);
var parsedSession = Transaction.fromJson(transaction);
var parsedSession = OldTransaction.fromJson(transaction);

print("Klarna updatedSession paymentID: " +
parsedSession.paymentID.toString());
Expand All @@ -226,7 +226,7 @@ class TransactionApiService {

/// The request will return an updated transaction object which contains
/// paymentConfirmed == true.
Future<Transaction> stopCharging(int transactionId) async {
Future<OldTransaction> stopCharging(int transactionId) async {
var response = await client.put(
Uri.parse('${API.url}/transactions/stop/$transactionId'),
headers: API.defaultRequestHeaders);
Expand All @@ -236,7 +236,7 @@ class TransactionApiService {
// We get a List with a single Transaction object in response
var list = json.decode(response.body) as List<dynamic>;
if (list.isEmpty) throw Exception(ErrorCodes.emptyResponse);
var parsedSession = Transaction.fromJson(list.first);
var parsedSession = OldTransaction.fromJson(list.first);
print("Klarna updatedSession paymentConfirmed : " +
parsedSession.paymentConfirmed.toString());
return parsedSession;
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/screens/home_page/home_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:async';
import 'package:flexicharge/models/transaction.dart';
import 'package:flexicharge/models/old_transaction.dart';
import 'package:flexicharge/services/transaction_api_service.dart';
import 'package:flexicharge/app/app.router.dart';
import 'package:flexicharge/models/charger_point.dart';
Expand Down Expand Up @@ -36,7 +36,7 @@ class HomeViewModel extends BaseViewModel {
? localData.redMarkerIcon
: localData.greenMarkerIcon,
onTap: () => openFindCharger(chargerPointId: chargingPoint),
position: chargingPoint.coordinates,
position: chargingPoint.location,
consumeTapEvents: true,
),
),
Expand Down Expand Up @@ -121,7 +121,7 @@ class HomeViewModel extends BaseViewModel {
/// The current charging percentage of the car.
Future<int> fetchChargingPercentage() async {
try {
Transaction currentTransaction = await _transactionAPI
OldTransaction currentTransaction = await _transactionAPI
.getTransactionById(localData.transactionSession.transactionID);

int currentChargingPercentage =
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/sheets/map_bottom_sheet/select_charger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BeginCharging extends ViewModelWidget<CustomSnappingSheetViewModel> {
ChargingStation(
onTap: () => model.isFirstView = true,
address: model.selectedChargerPoint.name,
currentLocation: model.userStreetLocation,
currentLocation: model.selectedChargerPoint.address,
),
SizedBox(height: 20),
Plugs(
Expand Down
Loading

0 comments on commit 0ca3010

Please sign in to comment.