45 lines
1.6 KiB
Dart
45 lines
1.6 KiB
Dart
import 'dart:async';
|
|
import 'package:finlytic_app/core/network/api_client.dart';
|
|
import 'package:finlytic_app/features/trades/models/trade_model.dart';
|
|
import 'package:finlytic_app/features/trades/models/trade_acceptance_dto.dart';
|
|
|
|
class TradeRepository {
|
|
final ApiClient apiClient;
|
|
|
|
TradeRepository({required this.apiClient});
|
|
|
|
Future<List<TradeModel>> fetchTrades({String? isin, String? status}) async {
|
|
try {
|
|
final queryParams = <String, dynamic>{};
|
|
if (isin != null && isin.isNotEmpty) queryParams['isin'] = isin;
|
|
if (status != null && status.isNotEmpty) queryParams['status'] = status;
|
|
|
|
final response = await apiClient.get('/api/v1/user/trades', queryParameters: queryParams);
|
|
|
|
if (response.statusCode == 200 && response.data != null) {
|
|
final List<dynamic> data = response.data;
|
|
return data.map((json) => TradeModel.fromJson(json)).toList();
|
|
}
|
|
return [];
|
|
} catch (e) {
|
|
print('Error fetching trades: $e');
|
|
throw Exception('Trades konnten nicht geladen werden');
|
|
}
|
|
}
|
|
|
|
Future<void> acceptTrade(TradeAcceptanceDto dto) async {
|
|
final response = await apiClient.post('/api/v1/user/trades/accept', data: dto.toJson());
|
|
if (response.statusCode != 200) {
|
|
throw Exception('Trade konnte nicht akzeptiert werden');
|
|
}
|
|
}
|
|
|
|
Future<void> closeTrade(String id, {double? exitPrice}) async {
|
|
final body = exitPrice != null ? {'userExitPrice': exitPrice} : null;
|
|
final response = await apiClient.post('/api/v1/user/trades/$id/close', data: body);
|
|
if (response.statusCode != 200) {
|
|
throw Exception('Trade konnte nicht geschlossen werden');
|
|
}
|
|
}
|
|
}
|