feat: add an owner + sell screen
This commit is contained in:
parent
d2cbb43bcb
commit
073f8bd334
15 changed files with 354 additions and 82 deletions
1
lib/data/repositories/book_instance_repository.dart
Normal file
1
lib/data/repositories/book_instance_repository.dart
Normal file
|
|
@ -0,0 +1 @@
|
|||
class BookInstanceRepository {}
|
||||
1
lib/data/repositories/book_repository.dart
Normal file
1
lib/data/repositories/book_repository.dart
Normal file
|
|
@ -0,0 +1 @@
|
|||
class BookRepository {}
|
||||
|
|
@ -1,8 +1,5 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:seshat/data/services/api_client.dart';
|
||||
import 'package:seshat/data/services/websocket_client.dart';
|
||||
import 'package:seshat/domain/models/owner.dart';
|
||||
|
|
@ -17,22 +14,25 @@ class OwnerRepository {
|
|||
|
||||
final ApiClient _apiClient;
|
||||
final WebsocketClient _wsClient;
|
||||
final BehaviorSubject<Owner> _ownersController = BehaviorSubject<Owner>(
|
||||
sync: true,
|
||||
);
|
||||
late final StreamSubscription sub;
|
||||
List<Owner>? _cachedOwners;
|
||||
|
||||
Future<Result<Owner>> postOwner(
|
||||
/// Adds an [Owner] to the database, and gets the resulting [Owner].
|
||||
Future<Result<Owner>> addOwner(
|
||||
String firstName,
|
||||
String lastName,
|
||||
String contact,
|
||||
) async {
|
||||
return Result.ok(
|
||||
Owner(firstName: firstName, lastName: lastName, contact: contact, id: 50),
|
||||
);
|
||||
var response = await _apiClient.addOwner(firstName, lastName, contact);
|
||||
switch (response) {
|
||||
case Ok():
|
||||
return Result.ok(response.value);
|
||||
case Error():
|
||||
return Result.error(response.error);
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetches all the [Owner]s from the database, and subscribes to updates
|
||||
Future<Result<List<Owner>>> getOwners() async {
|
||||
if (_cachedOwners == null) {
|
||||
final result = await _apiClient.getOwners();
|
||||
|
|
@ -43,9 +43,7 @@ class OwnerRepository {
|
|||
}
|
||||
|
||||
sub = _wsClient.owners.listen((owner) {
|
||||
debugPrint("\n\n\n\n[3] Added : $owner\n\n\n\n");
|
||||
_cachedOwners!.add(owner);
|
||||
_ownersController.add(owner);
|
||||
});
|
||||
|
||||
return result;
|
||||
|
|
@ -54,8 +52,6 @@ class OwnerRepository {
|
|||
}
|
||||
}
|
||||
|
||||
Stream<Owner> get liveOwners => _ownersController.stream;
|
||||
|
||||
dispose() {
|
||||
sub.cancel();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:seshat/config/constants.dart';
|
||||
import 'package:seshat/data/services/auth_client.dart';
|
||||
import 'package:seshat/domain/models/owner.dart';
|
||||
import 'package:seshat/utils/command.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
|
@ -12,14 +10,8 @@ import 'package:seshat/utils/result.dart';
|
|||
typedef AuthHeaderProvider = String? Function();
|
||||
|
||||
class ApiClient {
|
||||
ApiClient({
|
||||
String? host,
|
||||
int? port,
|
||||
HttpClient Function()? clientFactory,
|
||||
required AuthClient authClient,
|
||||
}) : _authClient = authClient;
|
||||
ApiClient({String? host, int? port});
|
||||
|
||||
final AuthClient _authClient;
|
||||
late final Command0 load;
|
||||
String? token;
|
||||
bool isReady = false;
|
||||
|
|
@ -31,26 +23,65 @@ class ApiClient {
|
|||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* ====================
|
||||
* =====[ OWNERS ]=====
|
||||
* ====================
|
||||
*/
|
||||
|
||||
Future<Result<List<Owner>>> getOwners() async {
|
||||
final client = HttpClient();
|
||||
final client = Client();
|
||||
try {
|
||||
await _initStore();
|
||||
final request = await client.getUrl(
|
||||
Uri.parse("https://$apiBasePath/owners"),
|
||||
);
|
||||
final token = await _secureStorage!.read(key: "token");
|
||||
debugPrint("\n\n\n\nFOUND TOKEN : $token\n\n\n\n");
|
||||
// await _authHeader(request.headers);
|
||||
request.headers.add(HttpHeaders.authorizationHeader, "Bearer $token");
|
||||
final response = await request.close();
|
||||
final headers = {"Authorization": "Bearer $token"};
|
||||
final response = await client.get(
|
||||
Uri.parse("https://$apiBasePath/owners"),
|
||||
headers: headers,
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final stringData = await response.transform(Utf8Decoder()).join();
|
||||
final json = jsonDecode(stringData) as List<dynamic>;
|
||||
final json = jsonDecode(response.body) as List<dynamic>;
|
||||
return Result.ok(
|
||||
json.map((element) => Owner.fromJSON(element)).toList(),
|
||||
);
|
||||
} else {
|
||||
return const Result.error(HttpException("Invalid response"));
|
||||
return Result.error(Exception("Invalid request"));
|
||||
}
|
||||
} on Exception catch (error) {
|
||||
return Result.error(error);
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
Future<Result<Owner>> addOwner(
|
||||
String firstName,
|
||||
String lastName,
|
||||
String contact,
|
||||
) async {
|
||||
final client = Client();
|
||||
try {
|
||||
await _initStore();
|
||||
final token = await _secureStorage!.read(key: "token");
|
||||
final headers = {
|
||||
"Authorization": "Bearer $token",
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
final body = {
|
||||
"first_name": firstName,
|
||||
"last_name": lastName,
|
||||
"contact": contact,
|
||||
};
|
||||
final response = await client.post(
|
||||
Uri.parse("https://$apiBasePath/owner"),
|
||||
headers: headers,
|
||||
body: jsonEncode(body),
|
||||
);
|
||||
if (response.statusCode == 201) {
|
||||
final json = jsonDecode(response.body);
|
||||
return Result.ok(Owner.fromJSON(json));
|
||||
} else {
|
||||
return Result.error(Exception("Invalid request"));
|
||||
}
|
||||
} on Exception catch (error) {
|
||||
return Result.error(error);
|
||||
|
|
|
|||
Reference in a new issue