Files
flux/lib/features/customers/data/customer_repository.dart

57 lines
1.5 KiB
Dart
Raw Normal View History

import 'package:get_it/get_it.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import '../models/customer_model.dart';
class CustomerRepository {
final SupabaseClient _client = GetIt.I<SupabaseClient>();
// Crea un nuovo cliente
Future<CustomerModel> createCustomer(CustomerModel customer) async {
try {
final response = await _client
.from('customer')
.insert(customer.toJson())
.select()
.single();
return CustomerModel.fromJson(response);
} catch (e) {
throw 'Errore durante la creazione del cliente: $e';
}
}
// Recupera tutti i clienti dell'azienda
Future<List<CustomerModel>> getCustomers(String companyId) async {
try {
final response = await _client
.from('customer')
.select()
.eq('company_id', companyId)
.eq('is_active', true)
.order('nome');
return (response as List).map((c) => CustomerModel.fromJson(c)).toList();
} catch (e) {
throw 'Errore nel recupero clienti';
}
}
// Ricerca clienti per nome o telefono (fondamentale per la UX)
Future<List<CustomerModel>> searchCustomers(
String companyId,
String query,
) async {
try {
final response = await _client
.from('customer')
.select()
.eq('company_id', companyId)
.or('nome.ilike.%$query%,telefono.ilike.%$query%')
.limit(10);
return (response as List).map((c) => CustomerModel.fromJson(c)).toList();
} catch (e) {
return [];
}
}
}