53 lines
1.9 KiB
Dart
53 lines
1.9 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';
|
|
import 'package:finlytic_app/features/trades/models/close_trade_request_dto.dart';
|
|
|
|
class TradeRepository {
|
|
final ApiClient apiClient;
|
|
|
|
const TradeRepository({required this.apiClient});
|
|
|
|
Future<List<TradeModel>> fetchTrades({String? isin, String? status}) async {
|
|
try {
|
|
final queryParams = <String, dynamic>{
|
|
'_t': DateTime.now().millisecondsSinceEpoch,
|
|
};
|
|
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) {
|
|
throw Exception('Trades konnten nicht geladen werden: $e');
|
|
}
|
|
}
|
|
|
|
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> rejectTrade(String tradeId) async {
|
|
final response = await apiClient.post('/api/v1/user/trades/$tradeId/reject');
|
|
if (response.statusCode != 200) {
|
|
throw Exception('Trade konnte nicht abgelehnt werden');
|
|
}
|
|
}
|
|
|
|
Future<void> closeTrade(String id, {CloseTradeRequestDto? dto}) async {
|
|
final response = await apiClient.post('/api/v1/user/trades/$id/close', data: dto?.toJson());
|
|
if (response.statusCode != 200) {
|
|
throw Exception('Trade konnte nicht geschlossen werden');
|
|
}
|
|
}
|
|
}
|