feat: refactor code
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../../core/locator/locator.dart';
|
||||
import '../../../../../core/network/api_provider_imp.dart';
|
||||
|
||||
class ShopApiProvider {
|
||||
ApiProviderImp get _api => locator<ApiProviderImp>();
|
||||
|
||||
Future<Response> getShop() => _api.get('/shop');
|
||||
|
||||
Future<Response> buyCard(String cardId) =>
|
||||
_api.post('/shop/buy-card', body: {'card_id': cardId});
|
||||
|
||||
Future<Response> selectCard(String cardId) =>
|
||||
_api.post('/shop/select-card', body: {'card_id': cardId});
|
||||
|
||||
Future<Response> purchase({
|
||||
required String store,
|
||||
required String kind,
|
||||
required String productId,
|
||||
required String token,
|
||||
}) =>
|
||||
_api.post('/shop/purchase', body: {
|
||||
'store': store,
|
||||
'kind': kind,
|
||||
'product_id': productId,
|
||||
'token': token,
|
||||
});
|
||||
|
||||
Future<Response> adReward(String token) =>
|
||||
_api.post('/rewards/ad', body: {'token': token});
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import '../../domain/entities/shop_entities.dart';
|
||||
|
||||
/// نگاشتِ JSON کاتالوگ فروشگاه به موجودیتها.
|
||||
class ShopMapper {
|
||||
static CoinPackage coin(Map<String, dynamic> j) => CoinPackage(
|
||||
id: j['id'] as String,
|
||||
title: j['title'] as String,
|
||||
coins: (j['coins'] ?? 0) as int,
|
||||
vipDays: (j['vip_days'] ?? 0) as int,
|
||||
priceToman: (j['price_toman'] ?? 0) as int,
|
||||
bonusPct: (j['bonus_pct'] ?? 0) as int,
|
||||
);
|
||||
|
||||
static TicketPackage ticket(Map<String, dynamic> j) => TicketPackage(
|
||||
id: j['id'] as String,
|
||||
title: j['title'] as String,
|
||||
tickets: (j['tickets'] ?? 0) as int,
|
||||
priceToman: (j['price_toman'] ?? 0) as int,
|
||||
);
|
||||
|
||||
static CardSkin card(Map<String, dynamic> j) => CardSkin(
|
||||
id: j['id'] as String,
|
||||
title: j['title'] as String,
|
||||
priceCoins: (j['price_coins'] ?? 0) as int,
|
||||
);
|
||||
|
||||
static Booster booster(Map<String, dynamic> j) => Booster(
|
||||
id: j['id'] as String,
|
||||
title: j['title'] as String,
|
||||
multiplier: (j['multiplier'] ?? 1) as int,
|
||||
hours: (j['hours'] ?? 0) as int,
|
||||
priceToman: (j['price_toman'] ?? 0) as int,
|
||||
);
|
||||
|
||||
static VipPackage vip(Map<String, dynamic> j) => VipPackage(
|
||||
id: j['id'] as String,
|
||||
title: j['title'] as String,
|
||||
months: (j['months'] ?? 1) as int,
|
||||
priceToman: (j['price_toman'] ?? 0) as int,
|
||||
);
|
||||
|
||||
static ShopData shopData(Map<String, dynamic> j) {
|
||||
final cat = Map<String, dynamic>.from(j['catalog'] as Map);
|
||||
List<T> parse<T>(String key, T Function(Map<String, dynamic>) f) =>
|
||||
((cat[key] as List?) ?? [])
|
||||
.map((e) => f(Map<String, dynamic>.from(e as Map)))
|
||||
.toList();
|
||||
return ShopData(
|
||||
coinPackages: parse('coin_packages', coin),
|
||||
ticketPackages: parse('ticket_packages', ticket),
|
||||
cardSkins: parse('card_skins', card),
|
||||
boosters: parse('boosters', booster),
|
||||
vipPackages: parse('vip_packages', vip),
|
||||
ownedCards:
|
||||
((j['owned_cards'] as List?) ?? []).map((e) => e as String).toList(),
|
||||
selectedCard: (j['selected_card'] ?? 'simple') as String,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../core/error/custom_error.dart';
|
||||
import '../../../../core/resources/data_state.dart';
|
||||
import '../../../../core/usecase/use_case.dart';
|
||||
import '../../domain/entities/shop_entities.dart';
|
||||
import '../../domain/repository/shop_repository.dart';
|
||||
import '../data_source/remote/shop_api_provider.dart';
|
||||
import '../model/shop_models.dart';
|
||||
|
||||
class ShopRepositoryImpl extends ShopRepository {
|
||||
final ShopApiProvider api;
|
||||
ShopRepositoryImpl(this.api);
|
||||
|
||||
@override
|
||||
Future<DataState<ShopData>> getShop() async {
|
||||
final Response res = await api.getShop();
|
||||
if (res.statusCode == 200) {
|
||||
return DataSuccess(
|
||||
ShopMapper.shopData(Map<String, dynamic>.from(res.data as Map)));
|
||||
}
|
||||
return DataError(errorConvertor(res.statusCode, _msg(res)));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<DataState<String>> buyCard(String cardId) async {
|
||||
final Response res = await api.buyCard(cardId);
|
||||
if (res.statusCode == 200) return const DataSuccess('کارت خریداری شد');
|
||||
return DataError(errorConvertor(res.statusCode, _msg(res)));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<DataState<String>> selectCard(String cardId) async {
|
||||
final Response res = await api.selectCard(cardId);
|
||||
if (res.statusCode == 200) return const DataSuccess('کارت انتخاب شد');
|
||||
return DataError(errorConvertor(res.statusCode, _msg(res)));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<DataState<String>> purchase(PurchaseParams params) async {
|
||||
final Response res = await api.purchase(
|
||||
store: params.store,
|
||||
kind: params.kind,
|
||||
productId: params.productId,
|
||||
token: params.token,
|
||||
);
|
||||
if (res.statusCode == 200) return const DataSuccess('خرید با موفقیت انجام شد');
|
||||
return DataError(errorConvertor(res.statusCode, _msg(res)));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<DataState<int>> adReward(String token) async {
|
||||
final Response res = await api.adReward(token);
|
||||
if (res.statusCode == 200) {
|
||||
return DataSuccess((res.data['amount'] ?? 0) as int);
|
||||
}
|
||||
return DataError(errorConvertor(res.statusCode, _msg(res)));
|
||||
}
|
||||
|
||||
String? _msg(Response res) {
|
||||
final d = res.data;
|
||||
if (d is Map && d['message'] != null) return d['message'].toString();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user