feat: add carpet
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import '../../../../../core/locator/locator.dart';
|
||||
import '../../../../../core/network/api_provider_imp.dart';
|
||||
import '../../../domain/entities/carpet.dart';
|
||||
|
||||
/// دادهٔ فرشها از backend (دریافت، خرید، انتخاب).
|
||||
class CarpetApiProvider {
|
||||
ApiProviderImp get _api => locator<ApiProviderImp>();
|
||||
|
||||
Future<CarpetCatalog> getCarpets() async {
|
||||
final res = await _api.get('/carpets');
|
||||
if (res.statusCode != 200) {
|
||||
throw Exception('carpets: ${res.statusCode}');
|
||||
}
|
||||
final list = (res.data['carpets'] as List?) ?? const [];
|
||||
return CarpetCatalog(
|
||||
carpets: list
|
||||
.map((e) => Carpet.fromJson(Map<String, dynamic>.from(e as Map)))
|
||||
.toList(),
|
||||
selected: (res.data['selected'] ?? 'classic') as String,
|
||||
);
|
||||
}
|
||||
|
||||
/// خرید؛ null یعنی موفق، وگرنه پیامِ خطای فارسی.
|
||||
Future<String?> buyCarpet(String id) =>
|
||||
_post('/shop/buy-carpet', id);
|
||||
|
||||
/// انتخاب؛ null یعنی موفق، وگرنه پیامِ خطای فارسی.
|
||||
Future<String?> selectCarpet(String id) =>
|
||||
_post('/shop/select-carpet', id);
|
||||
|
||||
Future<String?> _post(String path, String id) async {
|
||||
final res = await _api.post(path, body: {'carpet_id': id});
|
||||
if (res.statusCode == 200) return null;
|
||||
switch (res.statusCode) {
|
||||
case 402:
|
||||
return 'سکهٔ کافی ندارید';
|
||||
case 403:
|
||||
return 'این فرش را ندارید';
|
||||
case 409:
|
||||
return 'این فرش را دارید';
|
||||
default:
|
||||
return 'عملیات ناموفق بود';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user