create promocode page
This commit is contained in:
25
lib/data/models/promo_code_response_dto.dart
Normal file
25
lib/data/models/promo_code_response_dto.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import '../../domain/entities/promo_code_result.dart';
|
||||
|
||||
class PromoCodeResponseDto {
|
||||
final bool success;
|
||||
final double balance;
|
||||
|
||||
PromoCodeResponseDto({
|
||||
required this.success,
|
||||
required this.balance,
|
||||
});
|
||||
|
||||
factory PromoCodeResponseDto.fromJson(Map<String, dynamic> json) {
|
||||
return PromoCodeResponseDto(
|
||||
success: json['success'] as bool,
|
||||
balance: (json['balance'] as num).toDouble(),
|
||||
);
|
||||
}
|
||||
|
||||
PromoCodeResult toEntity() {
|
||||
return PromoCodeResult(
|
||||
success: success,
|
||||
balance: balance,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1150,4 +1150,31 @@ class ApiService {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> applyPromoCode(String code) async {
|
||||
final token = await _securityService.getAccessToken();
|
||||
if (token == null) throw Exception('No access token');
|
||||
|
||||
final response = await _dio.post(
|
||||
'$baseUrl/promocode/apply',
|
||||
data: {'code': code},
|
||||
options: Options(
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer $token',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 201 || response.statusCode == 200) {
|
||||
return response.data;
|
||||
} else if (response.statusCode == 404) {
|
||||
throw PromoCodeNotFoundException();
|
||||
} else {
|
||||
throw Exception('API Error: ${response.statusCode}');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ✅ Добавь исключение (можно в отдельный файл)
|
||||
class PromoCodeNotFoundException implements Exception {}
|
||||
|
||||
23
lib/data/repositories/promo_code_repository_impl.dart
Normal file
23
lib/data/repositories/promo_code_repository_impl.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import '../../domain/entities/promo_code_result.dart';
|
||||
import '../../domain/repositories/promo_code_repository.dart';
|
||||
import '../models/promo_code_response_dto.dart';
|
||||
import '../network/api_service.dart';
|
||||
|
||||
class PromoCodeRepositoryImpl implements PromoCodeRepository {
|
||||
final ApiService apiService;
|
||||
|
||||
PromoCodeRepositoryImpl(this.apiService);
|
||||
|
||||
@override
|
||||
Future<PromoCodeResult> applyPromoCode(String code) async {
|
||||
try {
|
||||
final data = await apiService.applyPromoCode(code);
|
||||
final dto = PromoCodeResponseDto.fromJson(data);
|
||||
return dto.toEntity();
|
||||
} on PromoCodeNotFoundException {
|
||||
throw Exception('Промокод не найден');
|
||||
} catch (e) {
|
||||
throw Exception('Ошибка активации промокода: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user