98 lines
		
	
	
	
		
			2.4 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			98 lines
		
	
	
	
		
			2.4 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:seshat/data/services/api_client.dart';
 | |
| import 'package:seshat/domain/models/bal.dart';
 | |
| import 'package:seshat/utils/result.dart';
 | |
| 
 | |
| class BalRepository {
 | |
|   BalRepository({required ApiClient apiClient}) : _apiClient = apiClient;
 | |
| 
 | |
|   final ApiClient _apiClient;
 | |
|   List<Bal>? _bals;
 | |
| 
 | |
|   Future<Result<List<Bal>>> getBals() async {
 | |
|     if (_bals != null) {
 | |
|       return Result.ok(_bals!);
 | |
|     }
 | |
|     final result = await _apiClient.getBals();
 | |
|     switch (result) {
 | |
|       case Ok():
 | |
|         _bals = result.value;
 | |
|         return Result.ok(result.value);
 | |
|       case Error():
 | |
|         return result;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   Future<Result<List<Bal>>> _getBalsNoCache() async {
 | |
|     final result = await _apiClient.getBals();
 | |
|     switch (result) {
 | |
|       case Ok():
 | |
|         _bals = result.value;
 | |
|         return Result.ok(result.value);
 | |
|       case Error():
 | |
|         return result;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   Future<Result<Bal>> balById(int id) async {
 | |
|     if (_bals == null) {
 | |
|       await getBals();
 | |
|     }
 | |
|     Bal? bal = _bals!.where((bal) => bal.id == id).firstOrNull;
 | |
|     if (bal != null) {
 | |
|       return Result.ok(bal);
 | |
|     }
 | |
|     final result = await _apiClient.getBalById(id);
 | |
|     switch (result) {
 | |
|       case Ok():
 | |
|         return Result.ok(result.value);
 | |
|       case Error():
 | |
|         return result;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   bool isABalOngoing() {
 | |
|     return _bals?.where((bal) => bal.state == BalState.ongoing).isNotEmpty ??
 | |
|         false;
 | |
|   }
 | |
| 
 | |
|   Future<Bal?> ongoingBal() async {
 | |
|     if (_bals == null) {
 | |
|       await _getBalsNoCache();
 | |
|     }
 | |
|     return _bals!.where((bal) => bal.state == BalState.ongoing).firstOrNull;
 | |
|   }
 | |
| 
 | |
|   Future<Result<Bal>> stopBal(int id) async {
 | |
|     final result = await _apiClient.stopBal(id);
 | |
|     _getBalsNoCache();
 | |
|     return result;
 | |
|   }
 | |
| 
 | |
|   Future<Result<Bal>> startBal(int id) async {
 | |
|     if (isABalOngoing()) {
 | |
|       return Result.error(
 | |
|         Exception("Cannot have multiple BAL ongoing at the same time !"),
 | |
|       );
 | |
|     }
 | |
|     final result = await _apiClient.startBal(id);
 | |
|     _getBalsNoCache();
 | |
|     return result;
 | |
|   }
 | |
| 
 | |
|   Future<Result<Bal>> editBal(
 | |
|     int id,
 | |
|     String name,
 | |
|     DateTime start,
 | |
|     DateTime end,
 | |
|   ) async {
 | |
|     final result = await _apiClient.editBal(id, name, start, end);
 | |
|     await _getBalsNoCache();
 | |
|     return result;
 | |
|   }
 | |
| 
 | |
|   Future<Result<void>> addBal(String name, DateTime start, DateTime end) async {
 | |
|     final result = await _apiClient.addBal(name, start, end);
 | |
|     await _getBalsNoCache();
 | |
|     return result;
 | |
|   }
 | |
| }
 | 
