Files
flux/lib/features/store/data/store_repository.dart

37 lines
1.1 KiB
Dart
Raw Normal View History

import 'package:get_it/get_it.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import '../models/store_model.dart';
class StoreRepository {
final SupabaseClient _client = GetIt.I.get<SupabaseClient>();
/// Crea un nuovo negozio associato alla compagnia dell'utente
Future<void> createStore(StoreModel store) async {
try {
await _client.from('store').insert(store.toJson());
} on PostgrestException catch (e) {
// Intercettiamo errori specifici del database
throw e.message;
} catch (e) {
throw 'Errore imprevisto durante la creazione del negozio: $e';
}
}
/// Recupera tutti i negozi di una determinata compagnia
Future<List<StoreModel>> getStoresByCompany(String companyId) async {
try {
final response = await _client
.from('store')
.select()
.eq('company_id', companyId)
.order('created_at');
return (response as List)
.map((json) => StoreModel.fromJson(json))
.toList();
} catch (e) {
throw 'Errore nel recupero dei negozi';
}
}
}