Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mannybatth committed Sep 8, 2023
1 parent 9d1e1e0 commit 28e35fa
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# carrier_nest_flutter
# Carrier Nest Flutter App

A new Flutter project.

Expand Down
41 changes: 41 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const apiUrl = 'http://localhost:3000/api';

class JSONResponse<T> {
final T data;
final List<Error> errors;

JSONResponse({required this.data, required this.errors});
}

class PaginationMetadata {
final int total;
final int currentOffset;
final int currentLimit;
final PaginationPointer? prev;
final PaginationPointer? next;

PaginationMetadata({
required this.total,
required this.currentOffset,
required this.currentLimit,
this.prev,
this.next,
});
}

class PaginationPointer {
final int offset;
final int limit;

PaginationPointer({
required this.offset,
required this.limit,
});
}

class Sort {
final String? key;
final String? order;

Sort({this.key, this.order});
}
3 changes: 3 additions & 0 deletions lib/models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ExpandedLoad {
// Your fields and constructors here
}
82 changes: 82 additions & 0 deletions lib/rest/loads.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Dart file. Contains code that loads data from the server.

import 'package:carrier_nest_flutter/constants.dart';
import 'package:carrier_nest_flutter/models.dart';
import 'dart:convert';
import 'dart:async';
import 'package:http/http.dart' as http;

class Loads {
static Future<Map<String, dynamic>> getLoadsExpanded({
Sort? sort,
String? customerId,
String? driverId,
int? limit,
int? offset,
bool? currentOnly,
String? expand,
}) async {
final Map<String, String> params = {
'expand': expand ?? 'customer,shipper,receiver',
if (sort?.key != null) 'sortBy': sort!.key!,
if (sort?.order != null) 'sortDir': sort!.order!,
if (customerId != null) 'customerId': customerId,
if (driverId != null) 'driverId': driverId,
if (limit != null) 'limit': limit.toString(),
if (offset != null) 'offset': offset.toString(),
if (currentOnly != null) 'currentOnly': currentOnly ? '1' : '0',
};

final Uri uri = Uri.parse('$apiUrl/loads').replace(queryParameters: params);
final http.Response response = await http.get(uri);

if (response.statusCode == 200) {
final Map<String, dynamic> jsonResponse = jsonDecode(response.body);
final List<dynamic> errors =
jsonResponse['errors']; // Assuming errors are a list

if (errors.isNotEmpty) {
throw Exception(errors.map((e) => e.toString()).join(', '));
}

final List<ExpandedLoad> loads = [];
final PaginationMetadata metadata = PaginationMetadata();

return {'loads': loads, 'metadata': metadata};
} else {
throw Exception('Failed to load data');
}
}

static Future<ExpandedLoad> getLoadById(String id,
{String? driverId, bool expandCarrier = false}) async {
String expand = 'customer,shipper,receiver,stops,invoice,driver,documents';
if (expandCarrier) {
expand += ',carrier';
}

final Map<String, String> params = {
'expand': expand,
if (driverId != null) 'driverId': driverId,
};

final Uri uri =
Uri.parse('$apiUrl/loads/$id').replace(queryParameters: params);
final http.Response response = await http.get(uri);

if (response.statusCode == 200) {
final Map<String, dynamic> jsonResponse = jsonDecode(response.body);
final List<dynamic> errors =
jsonResponse['errors']; // Assuming errors are a list

if (errors.isNotEmpty) {
throw Exception(errors.map((e) => e.toString()).join(', '));
}

final ExpandedLoad load = ExpandedLoad();
return load;
} else {
throw Exception('Failed to load data');
}
}
}
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: "direct main"
description:
name: http
sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
http_parser:
dependency: transitive
description:
name: http_parser
sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b"
url: "https://pub.dev"
source: hosted
version: "4.0.2"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -168,6 +184,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.0"
typed_data:
dependency: transitive
description:
name: typed_data
sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c
url: "https://pub.dev"
source: hosted
version: "1.3.2"
vector_math:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
http: ^1.1.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 28e35fa

Please sign in to comment.